Skip to content

Commit 4fe7c76

Browse files
committed
Added and documented unique and singular features
1 parent 07a5a2a commit 4fe7c76

File tree

4 files changed

+44
-21
lines changed

4 files changed

+44
-21
lines changed

DOCUMENTATION.md

100644100755
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ Jobs.run("sendReminder", "[email protected]", "The future is here!", {
9898
hour: 9,
9999
minute: 42
100100
},
101-
priority: 9999999999
101+
priority: 9999999999,
102+
singular: true
102103
});
103104
```
104105

@@ -118,8 +119,12 @@ The configuration object supports the following inputs:
118119
- If you set it to a positive integer, it will run ahead of other jobs.
119120
- If you set it to a negative integer, it will only run after all the zero or positive jobs have completed.
120121
- **`date`** - Function
121-
- Provide your own date. This stacks with the `in` and `on` operator, and will be applied before they run.
122-
- **callback** - Function
122+
- Provide your own date. This stacks with the `in` and `on` operator, and will be applied before they perform their operations.
123+
* **`unique`** - Boolean
124+
- If a job is marked as unique, it will only be scheduled if no other job exists with the same arguments
125+
* **`singular`** - Boolean
126+
- If a job is marked as singular, it will only be scheduled if no other job is pending (or failed, which in effect, is the same as pending) with the same arguments
127+
- **`callback`** - Function
123128
- Run a callback function after scheduling the job
124129

125130
### Jobs.execute

package/package.js

Lines changed: 1 addition & 1 deletion
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.0.7",
4+
version: "3.0.8",
55
documentation: "README.md",
66
git: "https://github.com/msavin/SteveJobs.git",
77
});

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

100644100755
Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,44 @@ import { processArguments } from "./processArguments.js"
33

44
var add = function () {
55
// 0. Prepare variables
6-
var error, result, existingDoc;
6+
var error, result, blockAdd;
77

88
// 1. Process arguments + prepare the data
99
var input = processArguments(arguments);
1010

11-
// 2-1. check if the job is singular
12-
if (input.config && input.config.singular) {
13-
existingDoc = Utilities.collection.findOne({
14-
name: input.name,
15-
arguments: input.arguments,
16-
state: {
17-
$in: ["pending", "failure"]
18-
}
19-
})
20-
}
11+
// 2 - check if job needs to blocked from being added
2112

22-
// 2-2. Cancel the job if it exists
23-
if (existingDoc) {
24-
return;
25-
}
13+
// 2-1. check if the job is singular
14+
if (input.config && input.config.singular) {
15+
var doc = Utilities.collection.findOne({
16+
name: input.name,
17+
arguments: input.arguments,
18+
state: {
19+
$in: ["pending", "failure"]
20+
}
21+
})
22+
23+
if (doc) blockAdd = true
24+
}
25+
26+
// 2-2. check if job is unique
27+
if (input.config && input.config.unique) {
28+
var doc = Utilities.collection.findOne({
29+
name: input.name,
30+
arguments: input.arguments,
31+
})
32+
33+
if (doc) blockAdd = true
34+
}
35+
36+
// 2-3. Cancel the job if a block condition is met
37+
if (blockAdd) {
38+
error = true;
39+
if (input.config && typeof input.config.callback === "function") {
40+
return input.config.callback(error, result);
41+
}
42+
return result;
43+
}
2644

2745
// 3. Generate job document
2846
var jobDoc = Utilities.helpers.generateJobDoc(input);
@@ -40,7 +58,7 @@ var add = function () {
4058
}
4159

4260
// 6. Mission accomplished
43-
if (typeof input.config.callback === "function") {
61+
if (input.config && typeof input.config.callback === "function") {
4462
input.config.callback(error, result);
4563
}
4664

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

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
var checkConfig = function (input) {
22
var result = false,
33
lastArgument = input[input.length - 1],
4-
keywords = ["in", "on", "priority", "date", "data", "callback", "singular"];
4+
keywords = ["in", "on", "priority", "date", "data", "callback", "singular", "unique"];
55

66
if (typeof lastArgument === "object") {
77
keywords.forEach(function (keyword) {

0 commit comments

Comments
 (0)