Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for reloading scripts into redis on "NOSCRIPT" errors. #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions lib/scripto.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,27 @@ function Scripto (redisClient) {

this.run = function run(scriptName, keys, args, callback) {

if(scripts[scriptName]) {
if(scripts[scriptName]) {
if(scriptShas[scriptName]) {
var sha = scriptShas[scriptName];
evalShaScript(redisClient, sha, keys, args, callback);
evalShaScript(redisClient, sha, keys, args, function(err, result) {
if(err && isScriptNotLoadedError(err)) {
/**
* The script was once loaded but is no longer loaded.
* We may have switched redis instances using sentinel
*/
loadScriptIntoRedis(redisClient, scripts[scriptName], function(err, sha) {
if(err) return callback(err);

scriptShas[scriptName] = sha;
evalShaScript(redisClient, sha, keys, args, callback);
});

return;
}

callback(err, result);
});
} else {
var script = scripts[scriptName];
evalScript(redisClient, script, keys, args, callback);
Expand Down Expand Up @@ -125,7 +142,7 @@ function loadScriptsIntoRedis (redisClient, scripts, callback) {
if(cnt < keys.length) {
var key = keys[cnt++];

redisClient.send_command('script', ['load', scripts[key]], function(err, sha) {
loadScriptIntoRedis(redisClient, scripts[key], function(err, sha) {

if(err) {
callback(err);
Expand All @@ -141,6 +158,14 @@ function loadScriptsIntoRedis (redisClient, scripts, callback) {
})();
}

function isScriptNotLoadedError(err) {
return err.message && err.message.indexOf('NOSCRIPT No matching script') === 0;
}

function loadScriptIntoRedis(redisClient, script, callback) {
redisClient.send_command('script', ['load', script], callback);
}

function evalScript(redisClient, script, keys, args, callback) {

var keysLength= keys.length || 0;
Expand Down
15 changes: 15 additions & 0 deletions test/scripto.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ suite('Scripto', function() {

}));

test('reload a script if its no longer in the script cache', _clean(function(done) {

var s = new Scripto(redisClient);
s.loadFromFile('read-write', path.resolve(scriptDir, 'read-write.lua'));
redisClient.script('flush', function() {
s.run('read-write', ['helloKey'], [200], function(err, result) {

assert.equal(err, null);
assert.equal(result, 200);
done();
});
});

}));

});

function _clean(callback) {
Expand Down