Skip to content

Commit b866938

Browse files
committed
Added support for async functions, made job success implicit, and automatically storing function result to database
1 parent 06361bf commit b866938

File tree

5 files changed

+32
-50
lines changed

5 files changed

+32
-50
lines changed

DOCUMENTATION.md

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,7 @@ Jobs.register({
8080
sendEmail: function (to, content) {
8181
var send = Magic.sendEmail(to, content);
8282

83-
if (send) {
84-
this.success()
85-
} else {
83+
if (!send) {
8684
this.reschedule({
8785
in: {
8886
minutes: 5
@@ -97,7 +95,6 @@ Jobs.register({
9795
})
9896

9997
if (doc) {
100-
this.success(doc);
10198
this.remove();
10299
} else {
103100
this.reschedule({
@@ -106,15 +103,14 @@ Jobs.register({
106103
}
107104
})
108105
}
109-
}
106+
110107
})
111108
```
112109
113110
Each job is binded with a set of functions to give you maximum control over how the job runs:
114111
- `this.document` - access job document
115112
- `this.set(key, value)` - set a persistent key/value pair
116113
- `this.get(key)` - get a persistent value from key
117-
- `this.success(result)` - tell the queue the job is completed, and attach an optional result
118114
- `this.failure(result)` - tell the queue the job failed, and attach an optional result
119115
- `this.reschedule(config)` - tell the queue to schedule the job for a future date
120116
- `this.remove()` - remove the job from the queue

README.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ Run scheduled tasks with Steve Jobs, the simple jobs queue made just for Meteor.
88

99
- Jobs run on one server at a time
1010
- Jobs run predictably and consecutively
11-
- Jobs and their history are stored in MongoDB
11+
- Jobs, their history and returned data are stored in MongoDB
1212
- Failed jobs are retried on server restart
1313
- No third party dependencies
1414

15-
**The new 3.1 features repeating jobs and more improvements.** It can run hundreds of jobs in seconds with minimal CPU impact, making it a reasonable choice for many applications. To get started, check out the <a href="https://github.com/msavin/SteveJobs..meteor.jobs.scheduler.queue.background.tasks/blob/master/DOCUMENTATION.md">**documentation**</a> and the <a href="#quick-start">**quick start**</a> below.
15+
**The new 4.0 features repeating jobs, async support and more!** It can run hundreds of jobs in seconds with minimal CPU impact, making it a reasonable choice for many applications. To get started, check out the <a href="https://github.com/msavin/SteveJobs..meteor.jobs.scheduler.queue.background.tasks/blob/master/DOCUMENTATION.md">**documentation**</a> and the <a href="#quick-start">**quick start**</a> below.
1616

1717
## Developer Friendly GUI and API
1818

@@ -43,21 +43,22 @@ Then, write your background jobs like you would write your methods:
4343
```javascript
4444
Jobs.register({
4545
"sendReminder": function (to, message) {
46-
var instance = this;
46+
const instance = this;
4747

48-
var call = HTTP.put("http://www.magic.com/sendEmail", {
48+
const call = HTTP.put("http://www.magic.com/email/send", {
4949
to: to,
50-
message: message
50+
message: message,
51+
subject: "You've Got Mail!",
5152
})
5253

53-
if (call.statusCode === 200) {
54-
instance.success(call.result);
55-
} else {
54+
if (call.statusCode !== 200) {
5655
instance.reschedule({
5756
in: {
5857
minutes: 5
5958
}
6059
});
60+
} else {
61+
return call.data;
6162
}
6263
}
6364
});
@@ -93,9 +94,8 @@ Compared to a CRON Job, the Steve Jobs package gives you much more control over
9394
```javascript
9495
Jobs.register({
9596
"syncData": function () {
96-
var instance = this;
97-
98-
var call = HTTP.put("http://www.magic.com/syncData")
97+
const instance = this;
98+
const call = HTTP.put("http://www.magic.com/syncData")
9999

100100
if (call.statusCode === 200) {
101101
instance.replicate({
@@ -104,8 +104,8 @@ Jobs.register({
104104
}
105105
});
106106

107-
// alternatively, you can use instance.remove to save storage
108-
instance.success(call.result);
107+
// to save storage, you can remove the document
108+
instance.remove();
109109
} else {
110110
instance.reschedule({
111111
in: {

package/server/imports/actions/execute/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
import { Utilities } from "../../utilities"
22
import { process } from "./process.js"
33

4-
var execute = function (job, callback) {
4+
var execute = async function (job, callback) {
55
var jobDoc = Utilities.helpers.getJob(job, {
66
allow: ["pending", "failure"],
77
message: "Job is not valid or not found, or is already resolved:"
88
});
99

1010
if (typeof jobDoc === "object") {
1111
if (typeof Utilities.registry.data[jobDoc.name]) {
12-
var result = process(jobDoc, callback);
12+
var result = await process(jobDoc, callback);
1313
return result;
1414
} else {
1515
Utilities.logger("Jobs: Job not found in registry: " + jobDoc.name);

package/server/imports/actions/execute/process.js

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,26 @@ import { Utilities } from "../../utilities"
22
import { toolbelt } from "./toolbelt.js"
33
import { reschedule } from "../reschedule/"
44

5-
var process = function (doc, callback) {
5+
var process = async function (doc, callback) {
66
// Goals:
77
// 1- Execute the job
88
// 2- Update the document in database
99
// 3- Capture the result (if any)
1010

1111
var Toolbelt = new toolbelt(doc);
12+
var jobResult;
13+
var jobFunc = Utilities.registry.data[doc.name];
14+
var isAsync = jobFunc.constructor.name === "AsyncFunction";
1215

1316
try {
14-
var jobResult = Utilities.registry.data[doc.name].apply(Toolbelt, doc.arguments);
15-
var resolution = Toolbelt.checkForResolution();
17+
if (isAsync) {
18+
jobResult = await jobFunc.apply(Toolbelt, doc.arguments);
19+
} else {
20+
jobResult = jobFunc.apply(Toolbelt, doc.arguments);
21+
}
22+
23+
24+
var resolution = Toolbelt.checkForResolution(jobResult);
1625

1726
if (typeof callback === "function") {
1827
return callback(undefined, jobResult);

package/server/imports/actions/execute/toolbelt.js

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -223,35 +223,12 @@ var toolbelt = function (jobDoc) {
223223
return update;
224224
}
225225

226-
this.checkForResolution = function () {
226+
this.checkForResolution = function (result) {
227227
var docId = this.document._id;
228228
var queueName = this.document.name;
229229
var resolution = this.resolved;
230-
231-
if (!resolution) {
232-
Utilities.logger([
233-
"Job was not successfully terminated: " + queueName + ", " + docId,
234-
"Every job must be resolved with this.success(), this.failure(), this.reschedule(), or this.remove()",
235-
"Queue was stopped; please re-write your function and re-start the server"
236-
]);
237-
238-
Operator.manager.queues[queueName].stop();
239-
240-
var update = Utilities.collection.update(docId, {
241-
$set: {
242-
state: "failure",
243-
},
244-
$push: {
245-
history: {
246-
date: new Date(),
247-
state: "unresolved",
248-
serverId: Utilities.config.getServerId()
249-
}
250-
}
251-
})
252-
253-
return false;
254-
}
230+
231+
if (!resolution) this.success(result)
255232
}
256233
}
257234

0 commit comments

Comments
 (0)