forked from OptimalBits/bull
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
57 lines (51 loc) · 1.34 KB
/
Copy pathscripts.js
File metadata and controls
57 lines (51 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/**
* Includes all the scripts needed by the queue and jobs.
*
*
*/
/*eslint-env node */
/*global Promise:true */
'use strict';
var cache = {};
function execScript(client, hash, script){
var sha = cache[hash];
var args;
var cached = Promise.resolved(sha);
if(!sha){
cached = cacheScript(client, hash, script);
}
cached.then(function(sha){
args.shift(sha);
return client.evalshaAsync.apply(client, args).catch(function(err){
// if ERR is that script is missing we need to re-cache and test again.
// delete cache[hash];
// return execScript(client, hash, script)
})
});
}
function cacheScript(client, hash, script){
return client.scriptAsync('LOAD', script).then(function(sha){
cache[hash] = sha;
return sha;
})
}
var scripts = {
isJobInList: function(client, listKey, jobId){
var script = [
'local function item_in_list (list, item)',
' for _, v in pairs(list) do',
' if v == item then',
' return 1',
' end',
' end',
' return nil',
'end',
'local items = redis.call("LRANGE", KEYS[1], 0, -1)',
'return item_in_list(items, ARGV[1])'
].join('\n');
return scripts._execScript(client, 'isJobInList', 1, listKey, jobId).then(function(result){
return result === 1;
});
}
}
module.exports.scripts = scripts;