forked from TradeMe/ReviewMe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.js
186 lines (162 loc) · 5.42 KB
/
worker.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
'use strict'
const async = require('async')
const moment = require('moment')
let startApp = function (app) {
const interval = this.container.make('config').interval
const ignoreFirstRun = !this.container.make('config').publishFirstRun
return new Promise((resolve, reject) => {
executeAppTask.call(this, app, ignoreFirstRun)
.then(() => {
this.container.make('freak').notify(app.shortName(), interval * 1.5)
resolve()
})
.then(err => reject(err))
setInterval(() => {
this.container.make('logger').info(`Fetching reviews from ${app.shortName()}...`)
executeAppTask.call(this, app, ignoreFirstRun)
.then(totalReviewsProcessed => {
this.container.make('logger').info(`Processed ${totalReviewsProcessed} reviews from ${app.shortName()}.`)
this.container.make('freak').notify(app.shortName(), interval * 1.5)
})
.catch(err => {
this.container.make('logger').error(`Error processing reviews from ${app.shortName()}:`)
this.container.make('logger').error(err)
})
}, interval)
})
}
let executeAppTask = function (app, ignoreFirstRun) {
return new Promise((resolve, reject) => {
app.fetchRecentReviews()
.then(reviews => {
return filterUnpublishedReviews(app, reviews, this.container.make('database'))
})
.then(reviews => {
if (ignoreFirstRun && this.isFirstRun(app.shortName())) {
return Promise.resolve(reviews)
}
return publishReviews(reviews, this.container.make('slack'))
})
.then(reviews => {
return markReviewsAsPublished(app, reviews, this.container.make('database'))
})
.then(totalReviewsProcessed => {
this.markAppAsRan(app.shortName())
resolve(totalReviewsProcessed)
})
.catch(err => {
reject(err)
})
})
}
let filterUnpublishedReviews = function (app, reviews, database) {
return new Promise((resolve, reject) => {
let bindings = []
const values = reviews
.map(review => {
bindings.push(review.id)
return `?`
})
.join(',')
bindings.push(app.shortName())
const sql = `SELECT review_id, UNIX_TIMESTAMP(created_at) as created_at FROM app_reviews WHERE review_id IN (${values}) AND app = ?`
database.query(sql, bindings)
.then(results => {
let alreadyPublished = {}
results.forEach(record => {
alreadyPublished[record['review_id']] = record['created_at']
})
const newReviews = reviews.filter(review => {
if (typeof alreadyPublished[review.id] === 'undefined') {
return true
}
if (typeof review.lastModified !== 'undefined') {
return review.lastModified - parseInt(alreadyPublished[review.id]) >= 7200
}
return false
})
resolve(newReviews)
})
.catch(err => {
reject(err)
})
})
}
let publishReviews = function (reviews, slack) {
return new Promise((resolve, reject) => {
let tasks = reviews.map(review => {
return callback => {
publishSingleReview(review, slack)
.then(() => {
callback()
})
.catch(err => {
callback(err)
})
}
})
async.series(tasks, err => {
if (err) {
return reject(err)
}
resolve(reviews)
})
})
}
let publishSingleReview = function (review, slack) {
return new Promise((resolve, reject) => {
slack.notifyReview(review).then(() => resolve()).catch(err => reject(err))
})
}
let markReviewsAsPublished = function (app, reviews, database) {
return new Promise((resolve, reject) => {
if (reviews.length === 0) {
return resolve(reviews.length)
}
const defaultCreatedAt = moment().format('YYYY-MM-DD HH:mm:ss')
let bindings = []
const values = reviews
.map(review => {
let createdAt = defaultCreatedAt
if (typeof review.lastModified !== 'undefined') {
createdAt = moment.unix(review.lastModified).format('YYYY-MM-DD HH:mm:ss')
}
bindings.push(review.id, app.shortName(), review.version, review.title, review.text, review.rating, review.author, createdAt)
return `(?, ?, ?, ?, ?, ?, ?, ?)`
})
.join(',')
const sql = `
INSERT INTO app_reviews (review_id, app, version, title, content, rating, author, created_at)
VALUES ${values}
ON DUPLICATE KEY UPDATE version = VALUES(version), title = VALUES(title), content = VALUES(content), rating = VALUES(rating), created_at = VALUES(created_at)`
database.query(sql, bindings)
.then(() => resolve(reviews.length))
.catch(err => reject(err))
})
}
class Worker {
constructor (apps, container) {
this.apps = apps
this.container = container
this.appExecutedOnce = {}
}
start () {
this.apps.forEach(app => {
startApp.call(this, app)
.then(() => {
this.container.make('logger').info(`Worker for ${app.name()} started...`)
})
.catch(err => {
this.container.make('logger').error(`Error starting worker for ${app.name()}`)
this.container.make('logger').error(err)
})
})
}
isFirstRun (appName) {
return typeof this.appExecutedOnce[appName] === 'undefined'
}
markAppAsRan (appName) {
return this.appExecutedOnce[appName] = true
}
}
module.exports = Worker