|
1 | 1 | /** |
2 | | -* crontabmanager.js - post callbacks to cronjobs |
3 | | -* - create, update, delete cronjobs via the net |
4 | | -* please see https://github.com/ncb000gt/node-cron and http://crontab.org/ for crontab info |
5 | | -* Synopsis: |
6 | | -* var CronTabManager = require('crontab_manager'), |
7 | | -* myCronTabManager = new CronTab(key, '* 2/2 13-16 * *', function, options ) // or |
8 | | -* myCronTabManager = new CronTab() |
9 | | -* in the first form, a new job is created on key 'key' with onTick set to 'function', cronTime is set to '* 2/2 13-16 * *', and options can include stuff like start, onComplete, and timeZone |
10 | | -* the second form just a new manager is created to which you can add jobs. Remember jobs do not start automatically, you have to set options to {start: true} or call start() after the job is created. |
11 | | -* |
12 | | -* Other methods include: |
13 | | -* myCronTabManager.update(key, newCronString) //or |
14 | | -* myCronTabManager.upadte(key, tabOrfunction) // or |
15 | | -* myCronTabManager.update(key, tab,function) |
16 | | -* myCronTabManager.stop(key) |
17 | | -* myCronTabManager.start(key) |
18 | | -* myCronTabManager.deleteJob(key) |
19 | | -* myCronTabManager.add(key, cronString, function, options) |
20 | | -* |
21 | | -* currently you cannot set context for any job. |
22 | | -*/ |
| 2 | + * crontabmanager.js - post callbacks to cronjobs |
| 3 | + * - create, update, delete cronjobs via the net |
| 4 | + * please see https://github.com/ncb000gt/node-cron and http://crontab.org/ for crontab info |
| 5 | + * Synopsis: |
| 6 | + * var CronTabManager = require('crontab_manager'), |
| 7 | + * myCronTabManager = new CronTab(key, '* 2/2 13-16 * *', function, options ) // or |
| 8 | + * myCronTabManager = new CronTab() |
| 9 | + * in the first form, a new job is created on key 'key' with onTick set to 'function', cronTime is set to '* 2/2 13-16 * *', and options can include stuff like start, onComplete, and timeZone |
| 10 | + * the second form just a new manager is created to which you can add jobs. Remember jobs do not start automatically, you have to set options to {start: true} or call start() after the job is created. |
| 11 | + * |
| 12 | + * Other methods include: |
| 13 | + * myCronTabManager.update(key, newCronString) //or |
| 14 | + * myCronTabManager.upadte(key, tabOrfunction) // or |
| 15 | + * myCronTabManager.update(key, tab,function) |
| 16 | + * myCronTabManager.stop(key) |
| 17 | + * myCronTabManager.start(key) |
| 18 | + * myCronTabManager.deleteJob(key) |
| 19 | + * myCronTabManager.add(key, cronString, function, options) |
| 20 | + * |
| 21 | + * currently you cannot set context for any job. |
| 22 | + */ |
23 | 23 |
|
24 | 24 | var CronJob = require('cron').CronJob; |
| 25 | + |
25 | 26 | function CrontabManager(key, tab, task, options) { |
26 | | - this.jobs = {}; |
27 | | - if (key && tab && task) |
28 | | - this.add(key, tab,task,options); |
29 | | - |
30 | | -} |
31 | | - |
32 | | -CrontabManager.prototype.add = function(key, tab, task, options) { |
33 | | - |
34 | | - if ((typeof tab === 'string' || tab instanceof Date) && typeof key === 'string' && task instanceof Function) { |
35 | | - options = combineOptions(tab, task, options); |
36 | | - try { |
37 | | - if (this.jobs[key]) { |
38 | | - this.deleteJob(key); |
39 | | - console.warn(`${key} already existed and was deleted from the manager...`); |
40 | | - } |
41 | | - this.jobs[key] = new CronJob(options); |
42 | | - } catch(fooBaredByUser) { |
43 | | - console.error(`crontab: ${tab} possibly not valid, job ${key} not started...${fooBaredByUser.message}`); |
44 | | - } |
45 | | - } |
46 | | - else |
47 | | - console.warn(`couldn't add: ${key} improper arguments`); |
48 | | -} |
49 | | - |
50 | | -CrontabManager.prototype.update = function() { |
51 | | - if (arguments.length === 2) { |
52 | | - //console.log("arguments are 2 units long." ) |
53 | | - //console.log(arguments) |
54 | | - if (typeof arguments[1] === 'string' || arguments[1] instanceof Date) |
55 | | - updateTab.call(this, arguments[0], arguments[1]); |
56 | | - else if (arguments[1] instanceof Function) |
57 | | - updateTask.call(this, arguments[0], arguments[1]); |
58 | | - } else if (arguments.length === 3) { |
59 | | - updateTab.call(this, arguments[0], arguments[1]) |
60 | | - updateTask.call(this, arguments[0], arguments[2]) |
61 | | - } else |
62 | | - console.error(`incorrect number of arguents passed to update.. won't update.. ${arguments[0]}`); |
63 | | -} |
64 | | - |
65 | | -CrontabManager.prototype.deleteJob = function(key) { |
66 | | - try { |
67 | | - this.jobs[key].stop(); |
68 | | - delete this.jobs[key]; |
69 | | - } catch (err) { console.error(`error in trying to stop job: ${key}: ${err}`) } |
70 | | -} |
71 | | - |
72 | | -CrontabManager.prototype.start = function(key) { |
73 | | - try { |
74 | | - if (this.jobs[key].running) |
75 | | - console.warn(`${key} job already running`); |
76 | | - else |
77 | | - this.jobs[key].start(); |
78 | | - }catch (err) { |
79 | | - console.error(`couldn't start job: ${key}: ${err}`); |
80 | | - } |
81 | | -} |
82 | | - |
83 | | -CrontabManager.prototype.stop = function(key) { |
84 | | - try { |
85 | | - if (! this.jobs[key].running ) |
86 | | - console.warn(`${key} job already stopped`); |
87 | | - else |
88 | | - this.jobs[key].stop(); |
89 | | - |
90 | | - } |
91 | | - catch(err) { |
92 | | - console.error(`couldn't stop job: ${key}: ${err}`) |
93 | | - } |
94 | | -} |
95 | | - |
96 | | -CrontabManager.prototype.stopAll = function() { |
97 | | - for (jobKey in this.jobs) { |
98 | | - try { |
99 | | - this.jobs[jobKey].stop() |
100 | | - }catch(err) { |
101 | | - |
102 | | - } |
103 | | - } |
104 | | -} |
105 | | - |
106 | | -CrontabManager.prototype.toString = function() { |
107 | | - var manString = "{\n"; |
108 | | - for (jobKey in this.jobs) { |
109 | | - |
110 | | - manString += `'${jobKey}': ${this.jobs[jobKey].cronTime.source}: ${this.jobs[jobKey]._callbacks[0]}: ${this.jobs[jobKey].running ? "Running" : "Stopped"}` |
111 | | - } |
112 | | - manString += "\n}"; |
113 | | - return manString; |
114 | | -} |
115 | | - |
116 | | -CrontabManager.prototype.listCrons = function() { |
117 | | - var manString = "{\n"; |
118 | | - for (jobKey in this.jobs) { |
119 | | - manString += `'${jobKey}': ${this.jobs[jobKey].cronTime.source} status: ${this.jobs[jobKey].running ? "Running" : "Stopped"} \n`; |
120 | | - } |
121 | | - manString += "\n}"; |
122 | | - return manString; |
123 | | -} |
124 | | - |
125 | | -CrontabManager.prototype.exists = function(tabKey) { |
126 | | - if (this.jobs[tabKey]) |
127 | | - return true; |
128 | | - return false; |
| 27 | + this.jobs = {}; |
| 28 | + if (key && tab && task) { |
| 29 | + this.add(key, tab, task, options); |
| 30 | + } |
129 | 31 | } |
130 | 32 |
|
131 | | -function combineOptions(tab, task, options) { |
132 | | - var newOpts = {}; |
133 | | - newOpts.cronTime = tab; newOpts.onTick = task |
| 33 | +CrontabManager.prototype.add = function (key, tab, task, options) { |
| 34 | + if ((typeof tab === 'string' || tab instanceof Date) && typeof key === 'string' && task instanceof Function) { |
| 35 | + options = combineOptions(tab, task, options); |
| 36 | + try { |
| 37 | + if (this.jobs[key]) { |
| 38 | + this.deleteJob(key); |
| 39 | + console.warn(`${key} already existed and was deleted from the manager...`); |
| 40 | + } |
| 41 | + this.jobs[key] = new CronJob(options); |
| 42 | + } catch (fooBaredByUser) { |
| 43 | + console.error(`crontab: ${tab} possibly not valid, job ${key} not started...${fooBaredByUser.message}`); |
| 44 | + } |
| 45 | + } else { |
| 46 | + console.warn(`couldn't add: ${key} improper arguments`); |
| 47 | + } |
| 48 | +} |
134 | 49 |
|
135 | | - if (options instanceof Object) { |
136 | | - // might overwrite... please be careful. |
137 | | - for (optionKey in options) |
138 | | - newOpts[optionKey] = options[optionKey]; |
139 | | - } |
140 | | - return newOpts; |
| 50 | +CrontabManager.prototype.update = function () { |
| 51 | + if (arguments.length === 2) { |
| 52 | + //console.log("arguments are 2 units long." ) |
| 53 | + //console.log(arguments) |
| 54 | + if (typeof arguments[1] === 'string' || arguments[1] instanceof Date) { |
| 55 | + updateTab.call(this, arguments[0], arguments[1]); |
| 56 | + } else if (arguments[1] instanceof Function) { |
| 57 | + updateTask.call(this, arguments[0], arguments[1]); |
| 58 | + } |
| 59 | + } else if (arguments.length === 3) { |
| 60 | + updateTab.call(this, arguments[0], arguments[1]) |
| 61 | + updateTask.call(this, arguments[0], arguments[2]) |
| 62 | + } else { |
| 63 | + console.error(`incorrect number of arguents passed to update.. won't update.. ${arguments[0]}`); |
| 64 | + } |
141 | 65 | } |
142 | 66 |
|
143 | | -function updateTab(key, cronstring) { |
| 67 | +CrontabManager.prototype.deleteJob = function (key) { |
| 68 | + try { |
| 69 | + this.jobs[key].stop(); |
| 70 | + delete this.jobs[key]; |
| 71 | + } catch (err) { |
| 72 | + console.error(`error in trying to stop job: ${key}: ${err}`) |
| 73 | + } |
| 74 | +} |
144 | 75 |
|
145 | | - try { |
146 | | - var running = this.jobs[key].running; |
147 | | - this.jobs[key].stop() |
| 76 | +CrontabManager.prototype.deleteAll = function () { |
| 77 | + for (jobKey in this.jobs) { |
| 78 | + try { |
| 79 | + this.jobs[jobKey].stop(); |
| 80 | + delete this.jobs[jobKey]; |
| 81 | + } catch (err) { |
| 82 | + } |
| 83 | + } |
| 84 | +} |
148 | 85 |
|
149 | | - if (typeof cronstring === 'string' || cronstring instanceof Date) |
150 | | - this.jobs[key] = new CronJob(cronstring, this.jobs[key]._callbacks[0], this.jobs[key].onComplete, running, this.jobs[key].zone) |
151 | | - else |
152 | | - throw new Error(`The cron definition passed was not a string or a Date object! ${key} was stopped and not updated`); |
153 | | - } catch (tabErr) { |
154 | | - console.error(`error updating tab: ${key} - ${tabErr.message}`); |
155 | | - } |
| 86 | +CrontabManager.prototype.start = function (key) { |
| 87 | + try { |
| 88 | + if (this.jobs[key].running) { |
| 89 | + console.warn(`${key} job already running`); |
| 90 | + } else { |
| 91 | + this.jobs[key].start(); |
| 92 | + } |
| 93 | + } catch (err) { |
| 94 | + console.error(`couldn't start job: ${key}: ${err}`); |
| 95 | + } |
156 | 96 | } |
157 | 97 |
|
158 | | -function updateTask(key, task) { |
159 | | - try { |
160 | | - var running = this.jobs[key].running; |
161 | | - this.jobs[key].stop() |
| 98 | +CrontabManager.prototype.startAll = function () { |
| 99 | + for (jobKey in this.jobs) { |
| 100 | + try { |
| 101 | + this.jobs[jobKey].start() |
| 102 | + } catch (err) { |
| 103 | + } |
| 104 | + } |
| 105 | +} |
| 106 | + |
| 107 | +CrontabManager.prototype.stop = function (key) { |
| 108 | + try { |
| 109 | + if (!this.jobs[key].running) { |
| 110 | + console.warn(`${key} job already stopped`); |
| 111 | + } else { |
| 112 | + this.jobs[key].stop(); |
| 113 | + } |
| 114 | + } catch (err) { |
| 115 | + console.error(`couldn't stop job: ${key}: ${err}`) |
| 116 | + } |
| 117 | +} |
162 | 118 |
|
163 | | - if ( ! ( task instanceof Function) ) { |
164 | | - console.error(`can't update with something that is not a function: ${typeof(task)}`) |
165 | | - return |
166 | | - } |
| 119 | +CrontabManager.prototype.stopAll = function () { |
| 120 | + for (jobKey in this.jobs) { |
| 121 | + try { |
| 122 | + this.jobs[jobKey].stop() |
| 123 | + } catch (err) { |
167 | 124 |
|
168 | | - this.jobs[key] = new CronJob(this.jobs[key].cronTime.source, task, this.jobs[key].onComplete, running, this.jobs[key].zone) |
| 125 | + } |
| 126 | + } |
| 127 | +} |
169 | 128 |
|
| 129 | +CrontabManager.prototype.toString = function () { |
| 130 | + var manString = "{\n"; |
| 131 | + for (jobKey in this.jobs) { |
| 132 | + manString += `'${jobKey}': ${this.jobs[jobKey].cronTime.source}: ${this.jobs[jobKey]._callbacks[0]}: ${this.jobs[jobKey].running ? "Running" : "Stopped"}` |
| 133 | + } |
| 134 | + manString += "\n}"; |
| 135 | + return manString; |
| 136 | +} |
170 | 137 |
|
171 | | - } catch (tabErr) { |
172 | | - console.error(`error updating task: ${key} - ${tabErr.message}`); |
173 | | - } |
| 138 | +CrontabManager.prototype.listCrons = function () { |
| 139 | + var manString = "{\n"; |
| 140 | + for (jobKey in this.jobs) { |
| 141 | + manString += `'${jobKey}': ${this.jobs[jobKey].cronTime.source} status: ${this.jobs[jobKey].running ? "Running" : "Stopped"} \n`; |
| 142 | + } |
| 143 | + manString += "\n}"; |
| 144 | + return manString; |
| 145 | +} |
| 146 | + |
| 147 | +CrontabManager.prototype.exists = function (tabKey) { |
| 148 | + if (this.jobs[tabKey]) { |
| 149 | + return true; |
| 150 | + } |
| 151 | + return false; |
| 152 | +} |
| 153 | + |
| 154 | +CrontabManager.prototype.fireOnTick = function (tabKey) { |
| 155 | + if (this.jobs[tabKey]) { |
| 156 | + return this.jobs[tabKey].fireOnTick() |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +function combineOptions(tab, task, options) { |
| 161 | + var newOpts = {}; |
| 162 | + newOpts.cronTime = tab; |
| 163 | + newOpts.onTick = task |
| 164 | + if (options instanceof Object) { |
| 165 | + // might overwrite... please be careful. |
| 166 | + for (optionKey in options) { |
| 167 | + newOpts[optionKey] = options[optionKey]; |
| 168 | + } |
| 169 | + } |
| 170 | + return newOpts; |
| 171 | +} |
| 172 | + |
| 173 | +function updateTab(key, cronstring) { |
| 174 | + try { |
| 175 | + var running = this.jobs[key].running; |
| 176 | + this.jobs[key].stop() |
| 177 | + if (typeof cronstring === 'string' || cronstring instanceof Date) { |
| 178 | + this.jobs[key] = new CronJob(cronstring, this.jobs[key]._callbacks[0], this.jobs[key].onComplete, running, this.jobs[key].zone) |
| 179 | + } else { |
| 180 | + throw new Error(`The cron definition passed was not a string or a Date object! ${key} was stopped and not updated`); |
| 181 | + } |
| 182 | + } catch (tabErr) { |
| 183 | + console.error(`error updating tab: ${key} - ${tabErr.message}`); |
| 184 | + } |
| 185 | +} |
| 186 | + |
| 187 | +function updateTask(key, task) { |
| 188 | + try { |
| 189 | + var running = this.jobs[key].running; |
| 190 | + this.jobs[key].stop() |
| 191 | + if (!(task instanceof Function)) { |
| 192 | + console.error(`can't update with something that is not a function: ${typeof(task)}`) |
| 193 | + return |
| 194 | + } |
| 195 | + this.jobs[key] = new CronJob(this.jobs[key].cronTime.source, task, this.jobs[key].onComplete, running, this.jobs[key].zone) |
| 196 | + } catch (tabErr) { |
| 197 | + console.error(`error updating task: ${key} - ${tabErr.message}`); |
| 198 | + } |
174 | 199 | } |
175 | 200 |
|
176 | 201 | module.exports = CrontabManager; |
0 commit comments