-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathscripttask.js
733 lines (703 loc) · 32.1 KB
/
scripttask.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
/**
* @description MeshCentral ScriptTask
* @author Ryan Blenis
* @copyright
* @license Apache-2.0
*/
"use strict";
module.exports.scripttask = function (parent) {
var obj = {};
obj.parent = parent;
obj.meshServer = parent.parent;
obj.db = null;
obj.intervalTimer = null;
obj.debug = obj.meshServer.debug;
obj.VIEWS = __dirname + '/views/';
obj.exports = [
'onDeviceRefreshEnd',
'resizeContent',
'historyData',
'variableData',
'malix_triggerOption'
];
obj.malix_triggerOption = function(selectElem) {
selectElem.options.add(new Option("ScriptTask - Run Script", "scripttask_runscript"));
}
obj.malix_triggerFields_scripttask_runscript = function() {
}
obj.resetQueueTimer = function() {
clearTimeout(obj.intervalTimer);
obj.intervalTimer = setInterval(obj.queueRun, 1 * 60 * 1000); // every minute
};
obj.server_startup = function() {
obj.meshServer.pluginHandler.scripttask_db = require (__dirname + '/db.js').CreateDB(obj.meshServer);
obj.db = obj.meshServer.pluginHandler.scripttask_db;
obj.resetQueueTimer();
};
obj.onDeviceRefreshEnd = function() {
pluginHandler.registerPluginTab({
tabTitle: 'ScriptTask',
tabId: 'pluginScriptTask'
});
QA('pluginScriptTask', '<iframe id="pluginIframeScriptTask" style="width: 100%; height: 700px; overflow: auto" scrolling="yes" frameBorder=0 src="/pluginadmin.ashx?pin=scripttask&user=1" />');
};
// may not be needed, saving for later. Can be called to resize iFrame
obj.resizeContent = function() {
var iFrame = document.getElementById('pluginIframeScriptTask');
var newHeight = 700;
//var sHeight = iFrame.contentWindow.document.body.scrollHeight;
//if (sHeight > newHeight) newHeight = sHeight;
//if (newHeight > 1600) newHeight = 1600;
iFrame.style.height = newHeight + 'px';
};
obj.queueRun = async function() {
var onlineAgents = Object.keys(obj.meshServer.webserver.wsagents);
//obj.debug('ScriptTask', 'Queue Running', Date().toLocaleString(), 'Online agents: ', onlineAgents);
obj.db.getPendingJobs(onlineAgents)
.then((jobs) => {
if (jobs.length == 0) return;
//@TODO check for a large number and use taskLimiter to queue the jobs
jobs.forEach(job => {
obj.db.get(job.scriptId)
.then(async (script) => {
script = script[0];
var foundVars = script.content.match(/#(.*?)#/g);
var replaceVars = {};
if (foundVars != null && foundVars.length > 0) {
var foundVarNames = [];
foundVars.forEach(fv => {
foundVarNames.push(fv.replace(/^#+|#+$/g, ''));
});
var limiters = {
scriptId: job.scriptId,
nodeId: job.node,
meshId: obj.meshServer.webserver.wsagents[job.node]['dbMeshKey'],
names: foundVarNames
};
var finvals = await obj.db.getVariables(limiters);
var ordering = { 'global': 0, 'script': 1, 'mesh': 2, 'node': 3 }
finvals.sort((a, b) => {
return (ordering[a.scope] - ordering[b.scope])
|| a.name.localeCompare(b.name);
});
finvals.forEach(fv => {
replaceVars[fv.name] = fv.value;
});
replaceVars['GBL:meshId'] = obj.meshServer.webserver.wsagents[job.node]['dbMeshKey'];
replaceVars['GBL:nodeId'] = job.node;
//console.log('FV IS', finvals);
//console.log('RV IS', replaceVars);
}
var dispatchTime = Math.floor(new Date() / 1000);
var jObj = {
action: 'plugin',
plugin: 'scripttask',
pluginaction: 'triggerJob',
jobId: job._id,
scriptId: job.scriptId,
replaceVars: replaceVars,
scriptHash: script.contentHash,
dispatchTime: dispatchTime
};
//obj.debug('ScriptTask', 'Sending job to agent');
try {
obj.meshServer.webserver.wsagents[job.node].send(JSON.stringify(jObj));
obj.db.update(job._id, { dispatchTime: dispatchTime });
} catch (e) { }
})
.catch(e => console.log('PLUGIN: ScriptTask: Could not dispatch job.', e));
});
})
.then(() => {
obj.makeJobsFromSchedules();
obj.cleanHistory();
})
.catch(e => { console.log('PLUGIN: ScriptTask: Queue Run Error: ', e); });
};
obj.cleanHistory = function() {
if (Math.round(Math.random() * 100) == 99) {
//obj.debug('Plugin', 'ScriptTask', 'Running history cleanup');
obj.db.deleteOldHistory();
}
};
obj.downloadFile = function(req, res, user) {
var id = req.query.dl;
obj.db.get(id)
.then(found => {
if (found.length != 1) { res.sendStatus(401); return; }
var file = found[0];
res.setHeader('Content-disposition', 'attachment; filename=' + file.name);
res.setHeader('Content-type', 'text/plain');
//var fs = require('fs');
res.send(file.content);
});
};
obj.updateFrontEnd = async function(ids){
if (ids.scriptId != null) {
var scriptHistory = null;
obj.db.getJobScriptHistory(ids.scriptId)
.then((sh) => {
scriptHistory = sh;
return obj.db.getJobSchedulesForScript(ids.scriptId);
})
.then((scriptSchedule) => {
var targets = ['*', 'server-users'];
obj.meshServer.DispatchEvent(targets, obj, { nolog: true, action: 'plugin', plugin: 'scripttask', pluginaction: 'historyData', scriptId: ids.scriptId, nodeId: null, scriptHistory: scriptHistory, nodeHistory: null, scriptSchedule: scriptSchedule });
});
}
if (ids.nodeId != null) {
var nodeHistory = null;
obj.db.getJobNodeHistory(ids.nodeId)
.then((nh) => {
nodeHistory = nh;
return obj.db.getJobSchedulesForNode(ids.nodeId);
})
.then((nodeSchedule) => {
var targets = ['*', 'server-users'];
obj.meshServer.DispatchEvent(targets, obj, { nolog: true, action: 'plugin', plugin: 'scripttask', pluginaction: 'historyData', scriptId: null, nodeId: ids.nodeId, scriptHistory: null, nodeHistory: nodeHistory, nodeSchedule: nodeSchedule });
});
}
if (ids.tree === true) {
obj.db.getScriptTree()
.then((tree) => {
var targets = ['*', 'server-users'];
obj.meshServer.DispatchEvent(targets, obj, { nolog: true, action: 'plugin', plugin: 'scripttask', pluginaction: 'newScriptTree', tree: tree });
});
}
if (ids.variables === true) {
obj.db.getVariables()
.then((vars) => {
var targets = ['*', 'server-users'];
obj.meshServer.DispatchEvent(targets, obj, { nolog: true, action: 'plugin', plugin: 'scripttask', pluginaction: 'variableData', vars: vars });
});
}
};
obj.handleAdminReq = function(req, res, user) {
if ((user.siteadmin & 0xFFFFFFFF) == 1 && req.query.admin == 1)
{
// admin wants admin, grant
var vars = {};
res.render(obj.VIEWS + 'admin', vars);
return;
} else if (req.query.admin == 1 && (user.siteadmin & 0xFFFFFFFF) == 0) {
// regular user wants admin
res.sendStatus(401);
return;
} else if (req.query.user == 1) {
// regular user wants regular access, grant
if (req.query.dl != null) return obj.downloadFile(req, res, user);
var vars = {};
if (req.query.edit == 1) { // edit script
if (req.query.id == null) return res.sendStatus(401);
obj.db.get(req.query.id)
.then((scripts) => {
if (scripts[0].filetype == 'proc') {
vars.procData = JSON.stringify(scripts[0]);
res.render(obj.VIEWS + 'procedit', vars);
} else {
vars.scriptData = JSON.stringify(scripts[0]);
res.render(obj.VIEWS + 'scriptedit', vars);
}
});
return;
} else if (req.query.schedule == 1) {
var vars = {};
res.render(obj.VIEWS + 'schedule', vars);
return;
}
// default user view (tree)
vars.scriptTree = 'null';
obj.db.getScriptTree()
.then(tree => {
vars.scriptTree = JSON.stringify(tree);
res.render(obj.VIEWS + 'user', vars);
});
return;
} else if (req.query.include == 1) {
switch (req.query.path.split('/').pop().split('.').pop()) {
case 'css': res.contentType('text/css'); break;
case 'js': res.contentType('text/javascript'); break;
}
res.sendFile(__dirname + '/includes/' + req.query.path); // don't freak out. Express covers any path issues.
return;
}
res.sendStatus(401);
return;
};
obj.historyData = function (message) {
if (typeof pluginHandler.scripttask.loadHistory == 'function') pluginHandler.scripttask.loadHistory(message);
if (typeof pluginHandler.scripttask.loadSchedule == 'function') pluginHandler.scripttask.loadSchedule(message);
};
obj.variableData = function (message) {
if (typeof pluginHandler.scripttask.loadVariables == 'function') pluginHandler.scripttask.loadVariables(message);
};
obj.determineNextJobTime = function(s) {
var nextTime = null;
var nowTime = Math.floor(new Date() / 1000);
// special case: we've reached the end of our run
if (s.endAt !== null && s.endAt <= nowTime) {
return nextTime;
}
switch (s.recur) {
case 'once':
if (s.nextRun == null) nextTime = s.startAt;
else nextTime = null;
break;
case 'minutes':
/*var lRun = s.nextRun || nowTime;
if (lRun == null) lRun = nowTime;
nextTime = lRun + (s.interval * 60);
if (s.startAt > nextTime) nextTime = s.startAt;*/
if (s.nextRun == null) { // hasn't run yet, set to start time
nextTime = s.startAt;
break;
}
nextTime = s.nextRun + (s.interval * 60);
// this prevents "catch-up" tasks being scheduled if an endpoint is offline for a long period of time
// e.g. always make sure the next scheduled time is relevant to the scheduled interval, but in the future
if (nextTime < nowTime) {
// initially I was worried about this causing event loop lockups
// if there was a long enough time gap. Testing over 50 years of backlog for a 3 min interval
// still ran under a fraction of a second. Safe to say this approach is safe! (~8.5 million times)
while (nextTime < nowTime) {
nextTime = nextTime + (s.interval * 60);
}
}
if (s.startAt > nextTime) nextTime = s.startAt;
break;
case 'hourly':
if (s.nextRun == null) { // hasn't run yet, set to start time
nextTime = s.startAt;
break;
}
nextTime = s.nextRun + (s.interval * 60 * 60);
if (nextTime < nowTime) {
while (nextTime < nowTime) {
nextTime = nextTime + (s.interval * 60 * 60);
}
}
if (s.startAt > nextTime) nextTime = s.startAt;
break;
case 'daily':
if (s.nextRun == null) { // hasn't run yet, set to start time
nextTime = s.startAt;
break;
}
nextTime = s.nextRun + (s.interval * 60 * 60 * 24);
if (nextTime < nowTime) {
while (nextTime < nowTime) {
nextTime = nextTime + (s.interval * 60 * 60 * 24);
}
}
if (s.startAt > nextTime) nextTime = s.startAt;
break;
case 'weekly':
var tempDate = new Date();
var nowDate = new Date(tempDate.getFullYear(), tempDate.getMonth(), tempDate.getDate());
if (s.daysOfWeek.length == 0) {
nextTime = null;
break;
}
s.daysOfWeek = s.daysOfWeek.map(el => Number(el));
var baseTime = s.startAt;
//console.log('dow is ', s.daysOfWeek);
var lastDayOfWeek = Math.max(...s.daysOfWeek);
var startX = 0;
//console.log('ldow is ', lastDayOfWeek);
if (s.nextRun != null) {
baseTime = s.nextRun;
//console.log('basetime 2: ', baseTime);
if (nowDate.getDay() == lastDayOfWeek) {
baseTime = baseTime + ( s.interval * 604800 ) - (lastDayOfWeek * 86400);
//console.log('basetime 3: ', baseTime);
}
startX = 0;
} else if (s.startAt < nowTime) {
baseTime = Math.floor(nowDate.getTime() / 1000);
//console.log('basetime 4: ', baseTime);
}
//console.log('startX is: ', startX);
//var secondsFromMidnight = nowTimeDate.getSeconds() + (nowTimeDate.getMinutes() * 60) + (nowTimeDate.getHours() * 60 * 60);
//console.log('seconds from midnight: ', secondsFromMidnight);
//var dBaseTime = new Date(0); dBaseTime.setUTCSeconds(baseTime);
//var dMidnight = new Date(dBaseTime.getFullYear(), dBaseTime.getMonth(), dBaseTime.getDate());
//baseTime = Math.floor(dMidnight.getTime() / 1000);
for (var x = startX; x <= 7; x++){
var checkDate = baseTime + (86400 * x);
var d = new Date(0); d.setUTCSeconds(checkDate);
var dm = new Date(d.getFullYear(), d.getMonth(), d.getDate());
console.log('testing date: ', dm.toLocaleString()); // dMidnight.toLocaleString());
//console.log('if break check :', (s.daysOfWeek.indexOf(d.getDay()) !== -1 && checkDate >= nowTime));
//console.log('checkDate vs nowTime: ', (checkDate - nowTime), ' if positive, nowTime is less than checkDate');
if (s.nextRun == null && s.daysOfWeek.indexOf(dm.getDay()) !== -1 && dm.getTime() >= nowDate.getTime()) break;
if (s.daysOfWeek.indexOf(dm.getDay()) !== -1 && dm.getTime() > nowDate.getTime()) break;
//if (s.daysOfWeek.indexOf(d.getDay()) !== -1 && Math.floor(d.getTime() / 1000) >= nowTime) break;
}
var sa = new Date(0); sa.setUTCSeconds(s.startAt);
var sad = new Date(sa.getFullYear(), sa.getMonth(), sa.getDate());
var diff = (sa.getTime() - sad.getTime()) / 1000;
nextTime = Math.floor(dm.getTime() / 1000) + diff;
//console.log('next schedule is ' + d.toLocaleString());
break;
default:
nextTime = null;
break;
}
if (s.endAt != null && nextTime > s.endAt) nextTime = null; // if the next time reaches the bound of the endAt time, nullify
return nextTime;
};
obj.makeJobsFromSchedules = function(scheduleId) {
//obj.debug('ScriptTask', 'makeJobsFromSchedules starting');
return obj.db.getSchedulesDueForJob(scheduleId)
.then(schedules => {
//obj.debug('ScriptTask', 'Found ' + schedules.length + ' schedules to process. Current time is: ' + Math.floor(new Date() / 1000));
if (schedules.length) {
schedules.forEach(s => {
var nextJobTime = obj.determineNextJobTime(s);
var nextJobScheduled = false;
if (nextJobTime === null) {
//obj.debug('ScriptTask', 'Removing Job Schedule for', JSON.stringify(s));
obj.db.removeJobSchedule(s._id);
} else {
//obj.debug('ScriptTask', 'Scheduling Job for', JSON.stringify(s));
obj.db.get(s.scriptId)
.then(scripts => {
// if a script is scheduled to run, but a previous run hasn't completed,
// don't schedule another job for the same (device probably offline).
// results in the minimum jobs running once an agent comes back online.
return obj.db.getIncompleteJobsForSchedule(s._id)
.then((jobs) => {
if (jobs.length > 0) { /* obj.debug('Plugin', 'ScriptTask', 'Skipping job creation'); */ return Promise.resolve(); }
else { /* obj.debug('Plugin', 'ScriptTask', 'Creating new job'); */ nextJobScheduled = true; return obj.db.addJob( { scriptId: s.scriptId, scriptName: scripts[0].name, node: s.node, runBy: s.scheduledBy, dontQueueUntil: nextJobTime, jobSchedule: s._id } ); }
});
})
.then(() => {
if (nextJobScheduled) { /* obj.debug('Plugin', 'ScriptTask', 'Updating nextRun time'); */ return obj.db.update(s._id, { nextRun: nextJobTime }); }
else { /* obj.debug('Plugin', 'ScriptTask', 'NOT updating nextRun time'); */ return Promise.resolve(); }
})
.then(() => {
obj.updateFrontEnd( { scriptId: s.scriptId, nodeId: s.node } );
})
.catch((e) => { console.log('PLUGIN: ScriptTask: Error managing job schedules: ', e); });
}
});
}
});
};
obj.deleteElement = function (command) {
var delObj = null;
obj.db.get(command.id)
.then((found) => {
var file = found[0];
delObj = {...{}, ...found[0]};
return file;
})
.then((file) => {
if (file.type == 'folder') return obj.db.deleteByPath(file.path); //@TODO delete schedules for scripts within folders
if (file.type == 'script') return obj.db.deleteSchedulesForScript(file._id);
if (file.type == 'jobSchedule') return obj.db.deletePendingJobsForSchedule(file._id);
})
.then(() => {
return obj.db.delete(command.id)
})
.then(() => {
var updateObj = { tree: true };
if (delObj.type == 'jobSchedule') {
updateObj.scriptId = delObj.scriptId;
updateObj.nodeId = delObj.node;
}
return obj.updateFrontEnd( updateObj );
})
.catch(e => { console.log('PLUGIN: ScriptTask: Error deleting ', e.stack); });
};
obj.serveraction = function(command, myparent, grandparent) {
switch (command.pluginaction) {
case 'addScript':
obj.db.addScript(command.name, command.content, command.path, command.filetype)
.then(() => {
obj.updateFrontEnd( { tree: true } );
});
break;
case 'new':
var parent_path = '';
var new_path = '';
obj.db.get(command.parent_id)
.then(found => {
if (found.length > 0) {
var file = found[0];
parent_path = file.path;
} else {
parent_path = 'Shared';
}
})
.then(() => {
obj.db.addScript(command.name, '', parent_path, command.filetype)
})
.then(() => {
obj.updateFrontEnd( { tree: true } );
});
break;
case 'rename':
obj.db.get(command.id)
.then((docs) => {
var doc = docs[0];
if (doc.type == 'folder') {
console.log('old', doc.path, 'new', doc.path.replace(doc.path, command.name));
return obj.db.update(command.id, { path: doc.path.replace(doc.name, command.name) })
.then(() => { // update sub-items
return obj.db.getByPath(doc.path)
})
.then((found) => {
if (found.length > 0) {
var proms = [];
found.forEach(f => {
proms.push(obj.db.update(f._id, { path: doc.path.replace(doc.name, command.name) } ));
})
return Promise.all(proms);
}
})
} else {
return Promise.resolve();
}
})
.then(() => {
obj.db.update(command.id, { name: command.name })
})
.then(() => {
return obj.db.updateScriptJobName(command.id, command.name);
})
.then(() => {
obj.updateFrontEnd( { scriptId: command.id, nodeId: command.currentNodeId, tree: true } );
});
break;
case 'move':
var toPath = null, fromPath = null, parentType = null;
obj.db.get(command.to)
.then(found => { // get target data
if (found.length > 0) {
var file = found[0];
toPath = file.path;
} else throw Error('Target destination not found');
})
.then(() => { // get item to be moved
return obj.db.get(command.id);
})
.then((found) => { // set item to new location
var file = found[0];
if (file.type == 'folder') {
fromPath = file.path;
toPath += '/' + file.name;
parentType = 'folder';
if (file.name == 'Shared' && file.path == 'Shared') throw Error('Cannot move top level directory: Shared');
}
return obj.db.update(command.id, { path: toPath } );
})
.then(() => { // update sub-items
return obj.db.getByPath(fromPath)
})
.then((found) => {
if (found.length > 0) {
var proms = [];
found.forEach(f => {
proms.push(obj.db.update(f._id, { path: toPath } ));
})
return Promise.all(proms);
}
})
.then(() => {
return obj.updateFrontEnd( { tree: true } );
})
.catch(e => { console.log('PLUGIN: ScriptTask: Error moving ', e.stack); });
break;
case 'newFolder':
var parent_path = '';
var new_path = '';
obj.db.get(command.parent_id)
.then(found => {
if (found.length > 0) {
var file = found[0];
parent_path = file.path;
} else {
parent_path = 'Shared';
}
})
.then(() => {
new_path = parent_path + '/' + command.name;
})
.then(() => {
return obj.db.addFolder(command.name, new_path);
})
.then(() => {
return obj.updateFrontEnd( { tree: true } );
})
.catch(e => { console.log('PLUGIN: ScriptTask: Error creating new folder ', e.stack); });
break;
case 'delete':
obj.deleteElement(command);
break;
case 'addScheduledJob':
/* {
scriptId: scriptId,
node: s,
scheduledBy: myparent.user.name,
recur: command.recur, // [once, minutes, hourly, daily, weekly, monthly]
interval: x,
daysOfWeek: x, // only used for weekly recur val
// onTheXDay: x, // only used for monthly
startAt: x,
endAt: x,
runCountLimit: x,
lastRun: x,
nextRun: x,
type: "scheduledJob"
} */
var sj = command.schedule;
var sched = {
scriptId: command.scriptId,
node: null,
scheduledBy: myparent.user.name,
recur: sj.recur,
interval: sj.interval,
daysOfWeek: sj.dayVals,
startAt: sj.startAt,
endAt: sj.endAt,
lastRun: null,
nextRun: null,
type: "jobSchedule"
};
var sel = command.nodes;
var proms = [];
if (Array.isArray(sel)) {
sel.forEach((s) => {
var sObj = {...sched, ...{
node: s
}};
proms.push(obj.db.addJobSchedule( sObj ));
});
} else { test.push(sObj);
proms.push(obj.db.addJobSchedule( sObj ));
}
Promise.all(proms)
.then(() => {
obj.makeJobsFromSchedules();
return Promise.resolve();
})
.catch(e => { console.log('PLUGIN: ScriptTask: Error adding schedules. The error was: ', e); });
break;
case 'runScript':
var scriptId = command.scriptId;
var sel = command.nodes;
var proms = [];
if (Array.isArray(sel)) {
sel.forEach((s) => {
proms.push(obj.db.addJob( { scriptId: scriptId, node: s, runBy: myparent.user.name } ));
});
} else {
proms.push(obj.db.addJob( { scriptId: scriptId, node: sel, runBy: myparent.user.name } ));
}
Promise.all(proms)
.then(() => {
return obj.db.get(scriptId);
})
.then(scripts => {
return obj.db.updateScriptJobName(scriptId, scripts[0].name);
})
.then(() => {
obj.resetQueueTimer();
obj.queueRun();
obj.updateFrontEnd( { scriptId: scriptId, nodeId: command.currentNodeId } );
});
break;
case 'getScript':
//obj.debug('ScriptTask', 'getScript Triggered', JSON.stringify(command));
obj.db.get(command.scriptId)
.then(script => {
myparent.send(JSON.stringify({
action: 'plugin',
plugin: 'scripttask',
pluginaction: 'cacheScript',
nodeid: myparent.dbNodeKey,
rights: true,
sessionid: true,
script: script[0]
}));
});
break;
case 'jobComplete':
//obj.debug('ScriptTask', 'jobComplete Triggered', JSON.stringify(command));
var jobNodeHistory = null, scriptHistory = null;
var jobId = command.jobId, retVal = command.retVal, errVal = command.errVal, dispatchTime = command.dispatchTime;
var completeTime = Math.floor(new Date() / 1000);
obj.db.update(jobId, {
completeTime: completeTime,
returnVal: retVal,
errorVal: errVal,
dispatchTime: dispatchTime
})
.then(() => {
return obj.db.get(jobId)
.then(jobs => {
return Promise.resolve(jobs[0].jobSchedule);
})
.then(sId => {
if (sId == null) return Promise.resolve();
return obj.db.update(sId, { lastRun: completeTime } )
.then(() => {
obj.makeJobsFromSchedules(sId);
});
});
})
.then(() => {
obj.updateFrontEnd( { scriptId: command.scriptId, nodeId: myparent.dbNodeKey } );
})
.catch(e => { console.log('PLUGIN: ScriptTask: Failed to complete job. ', e); });
// update front end by eventing
break;
case 'loadNodeHistory':
obj.updateFrontEnd( { nodeId: command.nodeId } );
break;
case 'loadScriptHistory':
obj.updateFrontEnd( { scriptId: command.scriptId } );
break;
case 'editScript':
obj.db.update(command.scriptId, { type: command.scriptType, name: command.scriptName, content: command.scriptContent })
.then(() => {
obj.updateFrontEnd( { scriptId: command.scriptId, tree: true } );
});
break;
case 'clearAllPendingJobs':
obj.db.deletePendingJobsForNode(myparent.dbNodeKey);
break;
case 'loadVariables':
obj.updateFrontEnd( { variables: true } );
break;
case 'newVar':
obj.db.addVariable(command.name, command.scope, command.scopeTarget, command.value)
.then(() => {
obj.updateFrontEnd( { variables: true } );
})
break;
case 'editVar':
obj.db.update(command.id, {
name: command.name,
scope: command.scope,
scopeTarget: command.scopeTarget,
value: command.value
})
.then(() => {
obj.updateFrontEnd( { variables: true } );
})
break;
case 'deleteVar':
obj.db.delete(command.id)
.then(() => {
obj.updateFrontEnd( { variables: true } );
})
break;
default:
console.log('PLUGIN: ScriptTask: unknown action');
break;
}
};
return obj;
}