-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcron-node.js
35 lines (34 loc) · 1.17 KB
/
cron-node.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
module.exports = function (RED) {
function CronIn(n) {
var CronJob = require('cron').CronJob;
var parser = require('cron-parser');
RED.nodes.createNode(this, n);
this.topic = n.topic;
this.name = n.name;
this.payload = n.payload;
this.crontab = n.crontab;
var node = this;
try {
parser.parseExpression(n.crontab);
var job = new CronJob(n.crontab, function () {
node.status({fill: "green", shape: "dot", text: "Job started"});
var msg = {payload: Date.now()};
node.send(msg);
node.status({});
});
this.on('close', function (done) {
node.status({fill: "red", shape: "dot", text: "Job stopped"});
job.stop();
done();
});
node.status({fill: "blue", shape: "dot", text: "Job deployed"});
setTimeout(function () {
node.status({});
}, 500);
job.start();
} catch (err) {
node.error("Invalid Expression");
}
}
RED.nodes.registerType("cron", CronIn);
};