Skip to content

Commit bb7c349

Browse files
StorytellerCZBastienRodz
authored andcommitted
Async for all DB calls
1 parent 3a0f555 commit bb7c349

File tree

17 files changed

+150
-148
lines changed

17 files changed

+150
-148
lines changed

package/package.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
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: "4.3.2",
4+
version: "5.0.0",
55
documentation: "README.md",
66
git: "https://github.com/msavin/SteveJobs.git",
77
});
88

99
Package.onUse(function(api) {
10-
api.versionsFrom('1.3');
10+
api.versionsFrom('2.8.1');
1111
api.use(["mongo", "random", "ecmascript", "check"], "server");
1212
api.mainModule("server/api.js", "server");
1313
api.export(["Jobs", "JobsInternal"]);
14-
});
14+
});

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Utilities } from "../../utilities"
22

3-
const add = function () {
3+
const add = async function () {
44
// 0. Prepare variables
55
let error, result, blockAdd;
66

@@ -11,7 +11,7 @@ const add = function () {
1111

1212
// 2-1. check if the job is singular
1313
if (input.config && input.config.singular) {
14-
const doc = Utilities.collection.findOne({
14+
const doc = await Utilities.collection.findOneAsync({
1515
name: input.name,
1616
arguments: input.arguments
1717
})
@@ -21,7 +21,7 @@ const add = function () {
2121

2222
// 2-2. check if job is unique
2323
if (input.config && input.config.unique) {
24-
const doc = Utilities.collection.findOne({
24+
const doc = await Utilities.collection.findOneAsync({
2525
name: input.name,
2626
arguments: input.arguments,
2727
state: {
@@ -42,15 +42,15 @@ const add = function () {
4242

4343
return result;
4444
}
45-
45+
4646
// 3. Generate job document
4747
const jobDoc = Utilities.helpers.generateJobDoc(input);
4848

4949
// 4. Insert the job document into the database OR update it
5050
let jobId;
5151

52-
if (input.config && input.config.override) {
53-
const doc = Utilities.collection.findOne({
52+
if (input.config && input.config.override) {
53+
const doc = await Utilities.collection.findOneAsync({
5454
name: input.name,
5555
arguments: input.arguments,
5656
state: {
@@ -61,7 +61,7 @@ const add = function () {
6161
if (doc) {
6262
const initDate = jobDoc.due || new Date;
6363

64-
jobId = Utilities.collection.update(doc._id, {
64+
jobId = await Utilities.collection.updateAsync(doc._id, {
6565
$set: {
6666
due: initDate,
6767
priority: jobDoc.priority,
@@ -79,13 +79,13 @@ const add = function () {
7979
});
8080

8181
} else {
82-
jobId = Utilities.collection.insert(jobDoc);
82+
jobId = await Utilities.collection.insertAsync(jobDoc);
8383
}
8484
} else {
85-
jobId = Utilities.collection.insert(jobDoc);
85+
jobId = await Utilities.collection.insertAsync(jobDoc);
8686
}
87-
88-
// 5. Simulate the document (this might save us a database request in some places)
87+
88+
// 5. Simulate the document (this might save us a database request in some places)
8989
if (jobId) {
9090
result = jobDoc;
9191
result._id = jobId;
@@ -98,8 +98,8 @@ const add = function () {
9898
if (input.config && typeof input.config.callback === "function") {
9999
input.config.callback(error, result);
100100
}
101-
101+
102102
return result;
103103
}
104104

105-
export { add }
105+
export { add }

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Utilities } from "../../utilities"
22

3-
const cancel = function (job, callback) {
3+
const cancel = async function (job, callback) {
44
let error;
55
let result;
66

@@ -10,7 +10,7 @@ const cancel = function (job, callback) {
1010
})
1111

1212
if (typeof jobDoc === "object") {
13-
result = Utilities.collection.update(jobDoc._id, {
13+
result = await Utilities.collection.updateAsync(jobDoc._id, {
1414
$set: {
1515
state: "cancelled"
1616
},
@@ -33,4 +33,4 @@ const cancel = function (job, callback) {
3333
return result;
3434
}
3535

36-
export { cancel }
36+
export { cancel }

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Utilities } from "../../utilities"
22

3-
const clear = function (state, name, callback) {
3+
const clear = async function (state, name, callback) {
44
if (typeof state === "string" && state !== "*") {
55
state = [state]
66
}
@@ -27,13 +27,13 @@ const clear = function (state, name, callback) {
2727
}
2828
}
2929

30-
const result = Utilities.collection.remove(action)
31-
30+
const result = await Utilities.collection.removeAsync(action)
31+
3232
if (callback) {
3333
callback(undefined, result);
3434
}
35-
35+
3636
return result;
3737
}
3838

39-
export { clear }
39+
export { clear }

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

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

44
const execute = async function (job, callback) {
5-
const jobDoc = Utilities.helpers.getJob(job, {
5+
const jobDoc = await 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") {
11-
if (typeof Utilities.registry.data[jobDoc.name]) {
11+
if (typeof Utilities.registry.data[jobDoc.name]) {
1212
const result = await process(jobDoc, callback);
1313
return result;
1414
} else {
@@ -18,4 +18,4 @@ const execute = async function (job, callback) {
1818
}
1919
}
2020

21-
export { execute }
21+
export { execute }

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

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ const toolbelt = function (jobDoc) {
99
instance.document = jobDoc;
1010
instance.resolved = false;
1111

12-
instance.set = (key, value) => {
12+
instance.set = async (key, value) => {
1313
check(key, String)
1414

1515
let docId = instance.document._id;
1616
let patch = {}
1717
patch["data." + key] = value;
1818

1919
// first, update the document
20-
let update = Utilities.collection.update(docId, {
20+
let update = await Utilities.collection.updateAsync(docId, {
2121
$set: patch
2222
})
2323

@@ -30,30 +30,30 @@ const toolbelt = function (jobDoc) {
3030
return update;
3131
}
3232

33-
instance.get = (key, getLatestFromDatabase) => {
33+
instance.get = async (key, getLatestFromDatabase) => {
3434
check(key, String)
3535

3636
let docId = instance.document._id
3737

3838
if (getLatestFromDatabase) {
3939
// Get the latest doc
40-
let doc = Utilities.collection.findOne(docId);
41-
40+
let doc = await Utilities.collection.findOneAsync(docId);
41+
4242
// Update the cached doc with the fresh copy
4343
if (doc) instance.document = doc;
4444
}
45-
45+
4646
if (instance.document.data && instance.document.data[key]) {
47-
return instance.document.data && instance.document.data[key];
47+
return instance.document.data && instance.document.data[key];
4848
}
4949
}
5050

51-
instance.push = (key, value) => {
51+
instance.push = async (key, value) => {
5252
check(key, String)
5353

5454
let docId = instance.document._id;
5555

56-
let update = Utilities.collection.update(docId, {
56+
let update = await Utilities.collection.updateAsync(docId, {
5757
$push: {
5858
["data." + key]: value
5959
}
@@ -62,76 +62,76 @@ const toolbelt = function (jobDoc) {
6262

6363
}
6464

65-
instance.pull = (key, value) => {
65+
instance.pull = async (key, value) => {
6666
check(key, String)
6767

6868
let docId = instance.document._id;
6969

70-
let update = Utilities.collection.update(docId, {
70+
let update = await Utilities.collection.updateAsync(docId, {
7171
$pull: {
7272
["data." + key]: value
7373
}
7474
})
7575
}
7676

77-
instance.pullAll = (key, value) => {
77+
instance.pullAll = async (key, value) => {
7878
check(key, String)
7979

8080
let docId = instance.document._id;
8181

82-
let update = Utilities.collection.update(docId, {
82+
let update = await Utilities.collection.updateAsync(docId, {
8383
$pullAll: {
8484
["data." + key]: value
8585
}
8686
})
8787
}
8888

89-
instance.inc = (key, value) => {
89+
instance.inc = async (key, value) => {
9090
check(key, String)
9191
check(value, Number)
9292
value = value || 1
9393

9494
let docId = instance.document._id;
9595

96-
let update = Utilities.collection.update(docId, {
96+
let update = await Utilities.collection.updateAsync(docId, {
9797
$inc: {
9898
["data." + key]: value
9999
}
100100
})
101101
}
102102

103-
instance.dec = (key, value) => {
103+
instance.dec = async (key, value) => {
104104
check(key, String)
105105
check(value, Number)
106106
value = value || 1
107107

108108
let docId = instance.document._id;
109109

110-
let update = Utilities.collection.update(docId, {
110+
let update = await Utilities.collection.updateAsync(docId, {
111111
$dec: {
112112
["data." + key]: value
113113
}
114114
})
115115
}
116116

117-
instance.addToSet = (key, value) => {
117+
instance.addToSet = async (key, value) => {
118118
check(key, String)
119119
check(value, Number)
120120
value = value || 1
121121

122122
let docId = instance.document._id;
123123

124-
let update = Utilities.collection.update(docId, {
124+
let update = await Utilities.collection.updateAsync(docId, {
125125
$addToSet: {
126126
["data." + key]: value
127127
}
128128
})
129129
}
130130

131-
instance.success = (result) => {
131+
instance.success = async (result) => {
132132
let docId = instance.document._id;
133133

134-
let update = Utilities.collection.update(docId, {
134+
let update = await Utilities.collection.updateAsync(docId, {
135135
$set: {
136136
state: "success",
137137
},
@@ -150,12 +150,12 @@ const toolbelt = function (jobDoc) {
150150
return update;
151151
}
152152

153-
instance.failure = (result) => {
153+
instance.failure = async (result) => {
154154
let docId = instance.document._id;
155155
let queueName = instance.document.name;
156156

157157
// Update the document
158-
let update = Utilities.collection.update(docId, {
158+
let update = await Utilities.collection.updateAsync(docId, {
159159
$set: {
160160
state: "failure",
161161
},
@@ -182,9 +182,9 @@ const toolbelt = function (jobDoc) {
182182
return update;
183183
}
184184

185-
instance.reschedule = (config) => {
185+
instance.reschedule = async (config) => {
186186
const doc = instance.document;
187-
let newDate = reschedule(doc._id, config);
187+
let newDate = await reschedule(doc._id, config);
188188

189189
if (!newDate) {
190190
Utilities.logger(["Error rescheduling job: " + doc.name + "/" + doc._id, config]);
@@ -194,9 +194,9 @@ const toolbelt = function (jobDoc) {
194194
return newDate;
195195
}
196196

197-
instance.replicate = (config) => {
197+
instance.replicate = async (config) => {
198198
const doc = instance.document;
199-
const newCopy = replicate(doc, config)
199+
const newCopy = await replicate(doc, config)
200200

201201
if (!newCopy) {
202202
Utilities.logger(["Error cloning job: " + doc.name + "/" + doc._id, config]);
@@ -205,9 +205,9 @@ const toolbelt = function (jobDoc) {
205205
return newCopy;
206206
}
207207

208-
instance.remove = () => {
208+
instance.remove = async () => {
209209
const doc = instance.document;
210-
let removeDoc = remove(doc._id)
210+
let removeDoc = await remove(doc._id)
211211

212212
if (!removeDoc) {
213213
Utilities.logger(["Error removing job: " + doc.name + "/" + doc._id]);
@@ -217,10 +217,10 @@ const toolbelt = function (jobDoc) {
217217
return removeDoc;
218218
}
219219

220-
instance.clearHistory = () => {
220+
instance.clearHistory = async () => {
221221
let docId = instance.document._id;
222222

223-
let update = Utilities.collection.update(docId, {
223+
let update = await Utilities.collection.updateAsync(docId, {
224224
$set: {
225225
history: [{
226226
date: new Date(),

0 commit comments

Comments
 (0)