Skip to content

Commit 521d70f

Browse files
committed
4.0 beta
1 parent 2bae603 commit 521d70f

File tree

10 files changed

+398
-16
lines changed

10 files changed

+398
-16
lines changed

package/package.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package.describe({
22
name: "msavin:sjobs",
33
summary: "The simple jobs queue that just works [synced, schedule, tasks, background, later, worker, cron]",
4-
version: "3.2.0",
4+
version: "4.0.0",
55
documentation: "README.md",
66
git: "https://github.com/msavin/SteveJobs.git",
77
});
@@ -11,4 +11,4 @@ Package.onUse(function(api) {
1111
api.use(["mongo", "random", "ecmascript", "check"], "server");
1212
api.mainModule("server/api.js", "server");
1313
api.export(["Jobs", "JobsInternal"]);
14-
});
14+
});

package/server/api.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,30 @@ Jobs.register = function (jobs) {
5454
Jobs.run = function () {
5555
check(arguments[0], String)
5656

57-
if (Utilities.registry.data[arguments[0]]) {
57+
var lastArg = arguments[arguments.length - 1];
58+
var remote = typeof lastArg === "object" && lastArg.remote ;
59+
60+
if (Utilities.registry.data[arguments[0]] || remote) {
5861
return Actions.add.apply(null, arguments);
5962
} else {
6063
Utilities.logger("invalid job name: " + arguments[0] || "not specified");
6164
return false;
6265
}
6366
}
6467

68+
// Update / manage a job even though it has not run
69+
70+
Jobs.manage = function () {
71+
check(arguments[0], String)
72+
73+
if (Utilities.registry.data[arguments[0]]) {
74+
return Actions.manage.apply(null, arguments);
75+
} else {
76+
Utilities.logger("invalid job name: " + arguments[0] || "not specified");
77+
return false;
78+
}
79+
}
80+
6581
// Cancel a job without removing it from MongoDB
6682

6783
Jobs.cancel = function (jobId) {

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Utilities } from "../../utilities"
2-
import { processArguments } from "./processArguments.js"
32

43
var add = function () {
54
// 0. Prepare variables
65
var error, result, blockAdd;
76

87
// 1. Process arguments + prepare the data
9-
var input = processArguments(arguments);
8+
var input = Utilities.helpers.processJobArguments(arguments);
109

1110
// 2 - check if job needs to blocked from being added
1211

@@ -27,7 +26,7 @@ var add = function () {
2726
if (input.config && input.config.unique) {
2827
var doc = Utilities.collection.findOne({
2928
name: input.name,
30-
arguments: input.arguments,
29+
arguments: input.arguments
3130
})
3231

3332

@@ -99,4 +98,4 @@ var add = function () {
9998
return result;
10099
}
101100

102-
export { add }
101+
export { add }

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

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ var toolbelt = function (jobDoc) {
3434
check(key, String)
3535
var docId = this.document._id
3636

37+
3738
if (getLatestFromDatabase) {
3839
// Get the latest doc
3940
doc = Utilities.collection.findOne(docId);
@@ -47,6 +48,86 @@ var toolbelt = function (jobDoc) {
4748
return this.document.data[key] || null;
4849
}
4950

51+
this.push = function (key, value) {
52+
check(key, String)
53+
54+
var docId = this.document._id;
55+
56+
var update = Utilities.collection.update(docId, {
57+
$push: {
58+
["data." + key]: value
59+
}
60+
})
61+
62+
63+
}
64+
65+
this.pull = function (key, value) {
66+
check(key, String)
67+
68+
var docId = this.document._id;
69+
70+
var update = Utilities.collection.update(docId, {
71+
$pull: {
72+
["data." + key]: value
73+
}
74+
})
75+
}
76+
77+
this.pullAll = function (key, value) {
78+
check(key, String)
79+
80+
var docId = this.document._id;
81+
82+
var update = Utilities.collection.update(docId, {
83+
$pullAll: {
84+
["data." + key]: value
85+
}
86+
})
87+
}
88+
89+
this.inc = function (key, value) {
90+
check(key, String)
91+
check(value, Number)
92+
value = value || 1
93+
94+
var docId = this.document._id;
95+
96+
var update = Utilities.collection.update(docId, {
97+
$inc: {
98+
["data." + key]: value
99+
}
100+
})
101+
}
102+
103+
this.dec = function (key, value) {
104+
check(key, String)
105+
check(value, Number)
106+
value = value || 1
107+
108+
var docId = this.document._id;
109+
110+
var update = Utilities.collection.update(docId, {
111+
$dec: {
112+
["data." + key]: value
113+
}
114+
})
115+
}
116+
117+
this.addToSet = function (key, value) {
118+
check(key, String)
119+
check(value, Number)
120+
value = value || 1
121+
122+
var docId = this.document._id;
123+
124+
var update = Utilities.collection.update(docId, {
125+
$addToSet: {
126+
["data." + key]: value
127+
}
128+
})
129+
}
130+
50131
this.success = function (result) {
51132
var docId = this.document._id;
52133

@@ -126,6 +207,22 @@ var toolbelt = function (jobDoc) {
126207
return removeDoc;
127208
}
128209

210+
this.clearHistory = function () {
211+
var docId = this.document._id;
212+
213+
var update = Utilities.collection.update(docId, {
214+
$set: {
215+
history: [{
216+
date: new Date(),
217+
state: "cleared",
218+
serverId: Utilities.config.getServerId()
219+
}]
220+
}
221+
})
222+
223+
return update;
224+
}
225+
129226
this.checkForResolution = function () {
130227
var docId = this.document._id;
131228
var queueName = this.document.name;

package/server/imports/actions/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { get } from './get'
33
import { clear } from './clear'
44
import { cancel } from './cancel'
55
import { execute } from './execute'
6+
import { manage } from './manage'
67
import { remove } from './remove'
78
import { replicate } from './replicate'
89
import { reschedule } from './reschedule'
@@ -13,6 +14,7 @@ var Actions = {
1314
clear: clear,
1415
cancel: cancel,
1516
execute: execute,
17+
manage: manage,
1618
remove: remove,
1719
replicate: replicate,
1820
reschedule: reschedule
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { Utilities } from "../../utilities"
2+
import { process } from "./process.js"
3+
4+
var manage = function () {
5+
// First, process the arguments
6+
var input = Utilities.helpers.processJobArguments(arguments);
7+
8+
// Second, find an active job
9+
var jobDoc = Utilities.collection.findOne({
10+
name: input.name,
11+
arguments: input.arguments,
12+
state: {
13+
$in: ["pending", "failure"]
14+
}
15+
})
16+
17+
// Third, run the described callback
18+
if (typeof input.callback === "function") {
19+
process(jobDoc, input.callback);
20+
}
21+
}
22+
23+
export { manage }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { Utilities } from "../../utilities"
2+
import { toolbelt } from "./toolbelt.js"
3+
4+
var process = function (doc, callback) {
5+
var Toolbelt = new toolbelt(doc);
6+
7+
try {
8+
var jobResult = callback.apply(Toolbelt, [doc]);
9+
}
10+
11+
catch (e) {
12+
Utilities.logger("Jobs.manage to run due to code error: " + doc.name)
13+
console.log(e);
14+
}
15+
}
16+
17+
export { process }

0 commit comments

Comments
 (0)