This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
forked from gfosco/parse-resque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse-rita.js
212 lines (191 loc) · 5.28 KB
/
parse-rita.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
/**
* ParseRita
*
* An implementation of Resque for Parse Cloud Code
*
* Uses background jobs as workers, and Parse Data classes for storage
*
* Author: Fosco Marotto <[email protected]>
*/
var ResqueQueue = Parse.Object.extend("ResqueQueue");
var ResqueLog = Parse.Object.extend("ResqueLog");
var jobs = [];
var queues = [];
var startTime;
var delayTime = 15000; // default 15s
var runTime = 14.5 * 60 * 1000; // default 14m30s
var statusObject; // the jobs status object, set on execution
var setDelayOnEmptyQueue = function(timeInMs) {
delayTime = timeInMs;
};
var setRunLimit = function(timeInMs) {
runTime = timeInMs;
};
var job = function(jobName, func) {
jobs[jobName] = func;
};
var enqueue = function(queue, jobName, scalarArgs, objectArgs) {
if (!queue || !jobName) {
return log(
'Failed to queue invalid job, missing jobName or queue',
{
'queue' : queue,
'jobName' : jobName,
'scalarArgs' : (scalarArgs ? scalarArgs : []),
'objectArgs' : (objectArgs ? objectArgs : [])
}
);
}
var job = new ResqueQueue();
job.set('queue', queue);
job.set('jobName', jobName);
job.set('scalarArgs', scalarArgs);
job.set('objectArgs', objectArgs);
job.set('processed', 0);
job.set('status', 'new');
job.set('result', '');
job.set('ACL', {});
return job.save(null, { useMasterKey : true }).then(null, createHandler());
};
var worker = function(status, queuesParam) {
statusObject = status;
status.message('Worker starting.');
startTime = Date.now();
queues = queuesParam;
if (!queues || !queues.length) {
return log('Failed to start worker, queues not provided', {}).then(null, createHandler());
}
return log(
'Worker started at ' + startTime,
{
'queues' : queues
}
).then(poll, createHandler());
};
var poll = function() {
return timeLimitCheck().then(function() {
var query = new Parse.Query(ResqueQueue);
query.containedIn('queue', queues);
query.equalTo('processed', 0);
query.include('objectArgs');
var jobCount = 0;
return query.each(function(job) {
jobCount++;
return perform(job).then(function() {
return Parse.Promise.as();
}, function(err) {
log(
'Failed to perform job ' + job.id, { error: err }
).then(function() {
return Parse.Promise.as();
});
});
}, {useMasterKey:true}).then(function() {
if (jobCount) {
return log(
'Worker cycle completed after ' + jobCount + ' jobs processed',
{ jobCount : jobCount }
);
} else {
return Parse.Promise.as();
}
});
}).then(delay).then(poll);
};
var perform = function(job) {
job.increment('processed');
return job.save(null, { useMasterKey : true }).then(function(job) {
if (job.get('processed') != 1) {
return Parse.Promise.as();
}
statusObject.message('Running job ' + job.id);
var startTime = Date.now();
var jobName = job.get('jobName');
if (!jobs[jobName]) {
return log(
'Undefined jobName, ' + jobName,
{ job : job }
).then(function() {
return job.save({
'status' : 'error',
'result' : 'Undefined jobName',
'elapsedTime' : (Date.now() - startTime)
}, { useMasterKey : true });
});
}
var scalarArgs = job.get('scalarArgs');
var objectArgs = job.get('objectArgs');
return jobs[jobName](scalarArgs, objectArgs).then(function (result) {
// job completed
return job.save({
'status' : 'completed',
'result' : JSON.stringify(result),
'elapsedTime' : (Date.now() - startTime)
}, { useMasterKey : true });
}, function (error) {
// job failed
return job.save({
'status' : 'failed',
'result' : JSON.stringify(error),
'elapsedTime' : (Date.now() - startTime)
}, { useMasterKey : true });
}).then(timeLimitCheck);
});
};
var log = function(message, data) {
var entry = new ResqueLog();
entry.set('message', message);
entry.set('data', data);
entry.set('ACL', {});
return entry.save(null, {useMasterKey:true});
};
var delay = function() {
var delayUntil = Date.now() + delayTime;
var delayPromise = new Parse.Promise();
var _delay = function () {
if (Date.now() > delayUntil) {
delayPromise.resolve();
return;
}
process.nextTick(_delay);
};
_delay();
return delayPromise;
};
function timeLimitCheck() {
if (Date.now() > (startTime + runTime)) {
return log(
'Worker closing at end of time limit.', {
startTime: startTime,
tunTime: runTime,
time: Date.now()
}
).then(function () {
statusObject.success('Worker exiting after run limit.');
});
}
return Parse.Promise.as();
}
function createHandler(response) {
/*
* Default error handling behaviour for async requests
*/
function errorHandler(result) {
if (result && result.message) {
var msg = 'Rita error: ' + result.message;
console.error(msg);
// afterSave doesnt have the response object available
if (response) {
response.error(msg);
}
}
}
return errorHandler;
}
module.exports = {
job : job,
enqueue : enqueue,
worker : worker,
setDelayOnEmptyQueue : setDelayOnEmptyQueue,
setRunLimit : setRunLimit
};