1 | var CRON = require('cron').CronJob; |
||
2 | |||
3 | // ------------------------------------------ |
||
4 | // CONSTRUCTOR |
||
5 | // ------------------------------------------ |
||
6 | |||
7 | var init = function(){ |
||
8 | info('Starting CRONManager ...'); |
||
9 | return CRONManager; |
||
10 | } |
||
11 | |||
12 | /** |
||
13 | * Starting all jobs from properties |
||
14 | */ |
||
15 | var start = function(){ |
||
16 | var plugins = SARAH.PluginManager.getList(); |
||
17 | for (var i = 0 ; i < plugins.length ; i++){ |
||
18 | var plugin = plugins[i]; |
||
19 | if (!plugin.cron) continue; |
||
0 ignored issues
–
show
|
|||
20 | job(plugin); |
||
21 | } |
||
22 | } |
||
23 | |||
24 | var job = function(plugin) { |
||
25 | if (!plugin.cron.time){ return warn('Missing cron time table');} |
||
26 | info("Starting new job %s with cron %s", plugin.name, plugin.cron.time); |
||
27 | |||
28 | // Build callback |
||
29 | var next = function(data){ |
||
30 | if (!data){ return; } |
||
31 | if (data.error){ SARAH.speak(tts); } |
||
32 | |||
33 | var tts = SARAH.ScriptManager.speak(data.tts); |
||
34 | if (tts){ SARAH.speak(tts); } |
||
35 | |||
36 | SARAH.RuleEngine.dispatch(plugin.name, data); |
||
37 | } |
||
38 | |||
39 | // Create job |
||
40 | var job = new CRON({ |
||
41 | cronTime: plugin.cron.time, |
||
42 | onTick: function() { |
||
43 | info('Cron: %s', plugin.name); |
||
44 | plugin.getInstance().cron(next, plugin.cron, SARAH); |
||
45 | }, |
||
46 | start: true |
||
47 | }); |
||
48 | |||
49 | // Run once |
||
50 | plugin.getInstance().cron(next, plugin.cron, SARAH); |
||
51 | } |
||
52 | |||
53 | // ------------------------------------------ |
||
54 | // PUBLIC |
||
55 | // ------------------------------------------ |
||
56 | |||
57 | var CRONManager = { |
||
58 | 'init' : init, |
||
59 | 'start': start |
||
60 | } |
||
61 | |||
62 | // Exports Manager |
||
63 | exports.init = CRONManager.init; |
Consider adding curly braces around all statements when they are executed conditionally. This is optional if there is only one statement, but leaving them out can lead to unexpected behaviour if another statement is added later.
Consider:
If you or someone else later decides to put another statement in, only the first statement will be executed.
In this case the statement
b = 42
will always be executed, while the logging statement will be executed conditionally.ensures that the proper code will be executed conditionally no matter how many statements are added or removed.