This repository has been archived by the owner on May 16, 2023. It is now read-only.
/
jobs.js
343 lines (300 loc) · 9.54 KB
/
jobs.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
/*jshint node: true*/
'use strict';
var notifier = require('./../../notifier'),
harvest = require('./../../harvest')('default'),
_ = require('lodash'),
tools = require('./../../tools.js'),
logger = require('./../../logger.js')('default'),
CronJob = require('cron').CronJob,
consts = require('./../../../../consts.json'),
reminder = require('./../../reminder/index.js')
;
/**
* Constructs a JobsHolder instance
*
* @returns {undefined}
* @constructor
* @author Maciej Garycki <maciej@neverbland.com>
*/
function JobsHolder ()
{
this.jobs = [];
}
JobsHolder.prototype = {
/**
* Adds a job
*
* @param {Object} job
* @param {Object} jobConfig This particular job config
* @return {JobsHolder} This instance
*/
addJob : function (job, jobConfig)
{
this.jobs.push({
job : job,
config : jobConfig
});
return this;
},
/**
* Runs the jobs
*
* @return {JobsHolder} This instance
*/
run : function ()
{
_.each(this.jobs, function (jobObject) {
var job = jobObject.job,
config = jobObject.config,
cronTime = job.getCronTime(config),
handler = job.getJob(config),
description = job.getDescription(),
autoRun = job.shouldRunNow()
;
logger.info('Setting up cron job for: "' + description + '" with cron time: ', cronTime, {});
var job = new CronJob(cronTime, handler);
job.start();
if (autoRun) {
logger.info('Immediately executing cron job for: "' + description + '".', {});
handler();
}
});
return this;
}
};
JobsHolder.prototype.constructor = JobsHolder;
var defaultJobs = {
notify : {
/**
* Returns the job function
*
* @param {Object} config
* @returns {Function}
*/
getJob : function (config)
{
return function ()
{
_.each(harvest.fromUserMap(harvest.users), function (userId) {
logger.info('Trying to send notifications to user: ' + userId, {});
harvest.getUserTimeTrack(userId, new Date(), new Date(), function (err, harvestResponse) {
if (err === null) {
notifier.notify('users', {
harvestUserId : userId,
harvestResponse : harvestResponse
});
} else {
logger.error("Failed fetching user timeline from Harvest API for user " + userId, err, {});
}
});
});
};
},
/**
* Formats the cron time according to given config
*
* @param {Object} config
* @returns {String} The cron time format string
*/
getCronTime : function (config)
{
/*
* Every work day at XX:XX send slack notification to all users
*/
var cronTime;
if (!config.cronTime) {
cronTime = '00 ' + config.minutes + ' ' + config.hour + ' * * 1-5';
} else {
cronTime = config.cronTime;
}
return cronTime;
},
/**
* Defines if given job should be ran independently from setting it up
* with cron
*
* @returns {Boolean}
*/
shouldRunNow : function ()
{
return false;
},
/**
* Returns description of the task
*
* @returns {String}
*/
getDescription : function ()
{
return 'Automatic notifications of harvest users on their Slack channels';
}
},
preload : {
/**
* Returns the job function
*
* @param {Object} config
* @returns {Function}
*/
getJob : function (config)
{
return function ()
{
logger.info('Loading projects from Harvest API...', {});
harvest.doGetProjects();
logger.info('Loading clients from Harvest API...', {});
harvest.doGetClients();
};
},
/**
* Formats the cron time according to given config
*
* @param {Object} config
* @returns {String} The cron time format string
*/
getCronTime : function (config)
{
return !!config.cronTime
? config.cronTime
: consts.preload.CRON_TIME;
},
/**
* Defines if given job should be ran independently from setting it up
* with cron
*
* @returns {Boolean}
*/
shouldRunNow : function ()
{
return true;
},
/**
* Returns description of the task
*
* @returns {String}
*/
getDescription : function ()
{
return 'Automatic periodical fetching clients and projects from Harvest API';
}
},
report : {
/**
* Returns the job function
*
* @param {Object} config
* @returns {Function}
*/
getJob : function (config)
{
var dateFrom = config.dateFromText || consts.report.DATE_FROM_TEXT,
dateTo = config.dateToText || consts.report.DATE_TO_TEXT;
return function ()
{
var dateFromObject = tools.dateFromString(dateFrom),
dateToObject = tools.dateFromString(dateTo),
reportTitle = config.reportTitle || consts.report.DEFAULT_REPORT_TITLE;
logger.info('Preparing management report from: ' + dateFromObject + ' to ' + dateToObject, {});
notifier.notify('management', {
reportTitle : reportTitle,
channel : config.channel,
fromDate : dateFromObject,
toDate : dateToObject
});
};
},
/**
* Formats the cron time according to given config
*
* @param {Object} config
* @returns {String} The cron time format string
*/
getCronTime : function (config)
{
return !!config.cronTime
? config.cronTime
: consts.report.CRON_TIME; // by default every hour, every week day, from 7am to 8pm;
},
/**
* Defines if given job should be ran independently from setting it up
* with cron
*
* @returns {Boolean}
*/
shouldRunNow : function ()
{
return false;
},
/**
* Returns description of the task
*
* @returns {String}
*/
getDescription : function ()
{
return 'Automatic management channel notifications';
}
},
remind : {
/**
* Returns the job function
*
* @param {Object} config
* @returns {Function}
*/
getJob : function (config)
{
return function ()
{
reminder.remind(harvest.users, null, function (results) {
_.each(results.notified, function (slackName) {
logger.info('Successfully notified user ' + slackName + ' about empty time tracker.', {});
});
});
};
},
/**
* Formats the cron time according to given config
*
* @param {Object} config
* @returns {String} The cron time format string
*/
getCronTime : function (config)
{
return !!config.cronTime
? config.cronTime
: consts.remind.CRON_TIME; // by default every midday of working day;
},
/**
* Defines if given job should be ran independently from setting it up
* with cron
*
* @returns {Boolean}
*/
shouldRunNow : function ()
{
return false;
},
/**
* Returns description of the task
*
* @returns {String}
*/
getDescription : function ()
{
return 'User notifications about empty tracker.';
}
}
};
module.exports = function (config, additionalJobs)
{
var jobsHolder = new JobsHolder();
var jobs = _.assign(defaultJobs, additionalJobs);
_.each(config, function (configValues, jobName) {
var job = jobs[jobName];
if (!!job) {
jobsHolder.addJob(job, configValues);
}
});
return jobsHolder;
};