forked from Hylozoic/hylo-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron.js
101 lines (86 loc) · 2.81 KB
/
cron.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
/* globals Nexudus */
require("@babel/register")
var skiff = require('./lib/skiff') // this must be required first
var moment = require('moment-timezone')
var rollbar = require('./lib/rollbar')
var sails = skiff.sails
var digest2 = require('./lib/group/digest2')
var Promise = require('bluebird')
var { red } = require('chalk')
const savedSearches = require('./lib/group/digest2/savedSearches')
const sendAndLogDigests = type =>
digest2.sendAllDigests(type)
.then(results => { sails.log.debug(`Sent digests to: ${results}`); return results })
const sendSavedSearchDigests = userId =>
savedSearches.sendAllDigests(userId)
const resendInvites = () =>
Invitation.resendAllReady()
.then(results => { sails.log.debug(`Resent the following invites: ${results}`); return results })
// Currently Nexudus updates are disabled
// const updateFromNexudus = opts =>
// Nexudus.updateAllCommunities(opts)
// .then(report => sails.log.debug('Updated users from Nexudus:', report))
const daily = now => {
const tasks = []
sails.log.debug('Removing old kue jobs')
tasks.push(Queue.removeOldJobs('complete', 20000))
sails.log.debug('Removing old notifications')
tasks.push(Notification.removeOldNotifications())
switch (now.day()) {
case 3:
sails.log.debug('Sending weekly digests')
tasks.push(sendAndLogDigests('weekly'))
tasks.push(sendSavedSearchDigests('weekly'))
break
}
return tasks
}
const hourly = now => {
// Currently nexudus updates are disabled. To enable, uncomment here and definition at top of this file.
// const tasks = [
// updateFromNexudus({dryRun: false})
// ]
const tasks = []
switch (now.hour()) {
case 12:
sails.log.debug('Sending daily digests')
tasks.push(sendAndLogDigests('daily'))
tasks.push(sendSavedSearchDigests('daily'))
break
case 13:
sails.log.debug('Resending invites')
tasks.push(resendInvites())
break
}
return tasks
}
const every10minutes = now => {
sails.log.debug('Refreshing full-text search index, sending comment digests, updating member counts')
return [
FullTextSearch.refreshView(),
Comment.sendDigests().then(count => sails.log.debug(`Sent ${count} comment/message digests`)),
Group.updateAllMemberCounts()
]
}
var runJob = Promise.method(name => {
const job = {hourly, daily, every10minutes}[name]
if (typeof job !== 'function') {
throw new Error(`Unknown job name: "${name}"`)
}
sails.log.debug(`Running ${name} job`)
const now = moment.tz('America/Los_Angeles')
return Promise.all(job(now))
})
skiff.lift({
start: function (argv) {
runJob(argv.interval)
.then(function () {
skiff.lower()
})
.catch(function (err) {
sails.log.error(red(err.message))
sails.log.error(err)
rollbar.error(err, () => skiff.lower())
})
}
})