-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler-function.js
52 lines (49 loc) · 1.35 KB
/
scheduler-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
50
51
52
const fli = require('fli-webtask');
const m = require('moment');
const cron = require('cron-converter');
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const worker = (context) => (params, cb) => as.map(
params.tasks,
(task, next) => {
loader({
url: context.secrets[task],
qs: {token: context.secrets.token, alarm: false, publish: 1}
}, () => {});
next();
},
() => cb()
);
const cronHandler = (context) => (params, next) => {
var tasks = [];
_.keys(params.storage.tasks)
.filter((key) => {
var cronInstance = new cron();
cronInstance.fromString(key);
return cronInstance.schedule(params.now).next().isBetween(params.now, params.tick, null, '[)');
})
.map((key) => {
tasks.push.apply(tasks, params.storage.tasks[key]);
});
worker(context)({tasks: tasks}, () => {});
return next(null, tasks);
};
/**
* @param context {WebtaskContext}
*/
module.exports = (context, cb) => {
const now = m().add(2, 'h').startOf('m');
const tick = m(now).add(1, 'm');
if(context.secrets.token !== _.get(context, 'query.token')) {
return cb('No container token.');
}
return as.waterfall([
(next) => context.storage.get(next),
(storage, next) => cronHandler(context)({
storage: storage,
now: now,
tick: tick
}, () => next())
], () => cb(null, now));
};