-
Notifications
You must be signed in to change notification settings - Fork 9
/
timerScheduler.js
69 lines (62 loc) · 2.48 KB
/
timerScheduler.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"use strict";
module.exports = {
componentLib: require('./lib/workspace_component_lib.js'),
workspaceLib: require('./lib/workspace_lib.js'),
jwt: require('jwt-simple'),
moment: require('moment'),
http: require('http'),
url: require('url'),
request : require('request'),
config: require("../timer/config.json"),
runTimers: function(amqpConnection) {
// console.log('----- Timer Cron')
this.componentLib.get_all_withConsomation({
module: 'timer'
}).then(components => {
components.forEach(c => {
// console.log(`inspect ${c.workspaceId}-${c._id}`);
this.workspaceLib.getWorkspace(c.workspaceId).then(workspace=>{
// console.log(`check ${workspace._id}-${c._id} status:${workspace.status} name:${workspace.name}`);
if (workspace.status!="running"){
let now = new Date();
let nextExec = c.specificData.next == undefined ? undefined : new Date(c.specificData.next);
// console.log('nextExec',nextExec);
// console.log(c.specificData.interval)
if (c.specificData.interval != undefined) {
let interval = c.specificData.interval;
if (nextExec == undefined || nextExec<now) {
// console.log('Timer dedidated!', c._id,c.workspaceId);
console.log(`--------------- execution ${workspace._id}-${c._id} status:${workspace.status} name:${workspace.name}`);
const workParams={
id : c._id
}
amqpConnection.sendToQueue(
'work-ask',
Buffer.from(JSON.stringify(workParams)),
null,
(err, ok) => {
if (err !== null) {
console.error('Erreur lors de l\'envoi du message :', err);
} else {
console.log(`Message envoyé à la file `);
// res.send(workParams);
}
}
)
}
}
}
})
.catch(e=>{
console.error(c,e)
// console.error(`orchan timer ${c.workspaceId}-${c._id}`);
})
});
})
},
run: function(amqpConnection) {
//this.address = address;
this.runTimers(amqpConnection);
setInterval(this.runTimers.bind(this,amqpConnection), 60000);
}
}