-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduled-function.js
49 lines (48 loc) · 1.59 KB
/
scheduled-function.js
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
const fli = require('fli-webtask');
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
/**
* @param context {WebtaskContext}
*/
module.exports = function(context, cb) {
if(context.secrets.token !== context.query.token) {
return cb('No token.');
}
if(!!context.query.reset) {
return as.waterfall([
(next) => context.storage.get(next),
(storage, next) => context.storage.set({last: {}}, next)
], ()=>cb());
}
return as.waterfall([
(next) => context.storage.get(next),
(storage, next) => loader({
url: `${context.secrets.queueFunction}/get`,
qs: {token: context.secrets.token}
}, (err, msg) => next(null, {storage:storage, msg:msg})),
(params, next) => {
var current = _.get(params.msg, 'payload');
if(!current) {
return next('No item payload provided.', params);
}
var last = _.get(params.storage, current.db);
if(last && current.id && _.isEqual(last, current.id)) {
return next('Item still in progress.', params);
}
params.storage[current.db] = current.id;
return next(null, params);
},
(params, next) => context.storage.set(params.storage, () => next(null, params)),
(params, next) => loader({
method: 'put',
url: `${context.secrets.storeFunction}/${params.msg.payload.db}/${params.msg.payload.id}`,
qs: {token: context.secrets.token},
json: {
state: params.msg.payload.notification || 'scheduled'
}
}, () => next(null, params))
], (err, params) => {
return cb(null, {err: err, params: params});
});
};