Skip to content

Commit 6aaf9ac

Browse files
committed
Steve Jobs 4.2.0 - es6, codebase improvements, minor enhancements
1 parent 4ec04c2 commit 6aaf9ac

File tree

35 files changed

+298
-301
lines changed

35 files changed

+298
-301
lines changed

.DS_Store

6 KB
Binary file not shown.

package/.DS_Store

6 KB
Binary file not shown.

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: "4.0.0",
4+
version: "4.2.0",
55
documentation: "README.md",
66
git: "https://github.com/msavin/SteveJobs.git",
77
});

package/server/api.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { Utilities } from './imports/utilities'
33
import { Operator } from './imports/operator'
44
import './imports/startup'
55

6-
var Jobs = {}
6+
const Jobs = {}
77

88
// Configure the package (optional)
99

@@ -54,8 +54,8 @@ Jobs.register = function (jobs) {
5454
Jobs.run = function () {
5555
check(arguments[0], String)
5656

57-
var lastArg = arguments[arguments.length - 1];
58-
var remote = typeof lastArg === "object" && lastArg.remote ;
57+
const lastArg = arguments[arguments.length - 1];
58+
const remote = typeof lastArg === "object" && lastArg.remote;
5959

6060
if (Utilities.registry.data[arguments[0]] || remote) {
6161
return Actions.add.apply(null, arguments);
@@ -111,7 +111,7 @@ Jobs.execute = function (jobId, callback, force) {
111111
check(force, Match.Optional(Boolean))
112112

113113
// 1. Get the job
114-
var doc = Utilities.collection.findOne({
114+
const doc = Utilities.collection.findOne({
115115
_id: jobId,
116116
state: {
117117
$nin: ["success", "cancelled"]
@@ -204,7 +204,7 @@ Meteor.startup(function () {
204204

205205
// Internals for debugging
206206

207-
var JobsInternal = {
207+
const JobsInternal = {
208208
Actions: Actions,
209209
Utilities: Utilities,
210210
Operator: Operator

package/server/imports/.DS_Store

6 KB
Binary file not shown.

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

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

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

77
// 1. Process arguments + prepare the data
8-
var input = Utilities.helpers.processJobArguments(arguments);
8+
const input = Utilities.helpers.processJobArguments(arguments);
99

1010
// 2 - check if job needs to blocked from being added
1111

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

2525
// 2-2. check if job is unique
2626
if (input.config && input.config.unique) {
27-
var doc = Utilities.collection.findOne({
27+
const doc = Utilities.collection.findOne({
2828
name: input.name,
2929
arguments: input.arguments
3030
})
@@ -35,20 +35,22 @@ var add = function () {
3535
// 2-3. Cancel the job if a block condition is met
3636
if (blockAdd) {
3737
error = true;
38+
3839
if (input.config && typeof input.config.callback === "function") {
3940
return input.config.callback(error, result);
4041
}
42+
4143
return result;
4244
}
4345

4446
// 3. Generate job document
45-
var jobDoc = Utilities.helpers.generateJobDoc(input);
47+
const jobDoc = Utilities.helpers.generateJobDoc(input);
4648

4749
// 4. Insert the job document into the database OR update it
48-
var jobId;
50+
let jobId;
4951

5052
if (input.config && input.config.override) {
51-
var doc = Utilities.collection.findOne({
53+
const doc = Utilities.collection.findOne({
5254
name: input.name,
5355
arguments: input.arguments,
5456
state: {
@@ -57,8 +59,7 @@ var add = function () {
5759
})
5860

5961
if (doc) {
60-
console.log(doc)
61-
var initDate = jobDoc.due || new Date;
62+
const initDate = jobDoc.due || new Date;
6263

6364
jobId = Utilities.collection.update(doc._id, {
6465
$set: {

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

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

3-
var cancel = function (job, callback) {
4-
var error,
5-
result,
6-
jobDoc = Utilities.helpers.getJob(job, {
7-
allow: ["pending", "failure"],
8-
message: "Unable to cancel job - not found or is resolved: "
9-
})
3+
const cancel = function (job, callback) {
4+
let error;
5+
let result;
6+
7+
const jobDoc = Utilities.helpers.getJob(job, {
8+
allow: ["pending", "failure"],
9+
message: "Unable to cancel job - not found or is resolved: "
10+
})
1011

1112
if (typeof jobDoc === "object") {
1213
result = Utilities.collection.update(jobDoc._id, {

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

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

3-
var clear = function (state, name, callback) {
3+
const clear = function (state, name, callback) {
44
if (typeof state === "string" && state !== "*") {
55
state = [state]
66
}
77

8-
var action = {
8+
let action = {
99
state: {
1010
$in: state || ["cancelled", "success"]
1111
}
@@ -27,7 +27,7 @@ var clear = function (state, name, callback) {
2727
}
2828
}
2929

30-
var result = Utilities.collection.remove(action)
30+
const result = Utilities.collection.remove(action)
3131

3232
if (callback) {
3333
callback(undefined, result);

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

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

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

10+
console.log(`Jobs.execute`, jobDoc.name, jobDoc._id, jobDoc.due)
11+
1012
if (typeof jobDoc === "object") {
11-
if (typeof Utilities.registry.data[jobDoc.name]) {
12-
var result = await process(jobDoc, callback);
13+
if (typeof Utilities.registry.data[jobDoc.name]) {
14+
const result = await process(jobDoc, callback);
1315
return result;
1416
} else {
1517
Utilities.logger("Jobs: Job not found in registry: " + jobDoc.name);

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

Lines changed: 30 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,55 @@ import { Utilities } from "../../utilities"
22
import { toolbelt } from "./toolbelt.js"
33
import { reschedule } from "../reschedule/"
44

5-
6-
var process = async function (doc, callback) {
5+
const process = async function (doc, callback) {
76
// Goals:
87
// 1- Execute the job
98
// 2- Update the document in database
109
// 3- Capture the result (if any)
1110

12-
var Toolbelt = new toolbelt(doc);
13-
var jobFunc = Utilities.registry.data[doc.name];
14-
var isAsync = jobFunc.constructor.name === "AsyncFunction";
11+
const Toolbelt = new toolbelt(doc);
12+
const jobFunc = Utilities.registry.data[doc.name];
13+
const isAsync = jobFunc.constructor.name === "AsyncFunction";
1514

1615
if (isAsync) {
17-
jobResult = await jobFunc.apply(Toolbelt, doc.arguments).catch(function (error) {
18-
var failure = Toolbelt.failure(error);
19-
20-
Utilities.logger("Job failed to run due to code error: " + doc.name)
21-
console.log(error);
22-
23-
if (typeof callback === "function") {
24-
return callback(true, undefined);
25-
}
26-
}).then(function (jobResult) {
27-
var resolution = Toolbelt.checkForResolution(jobResult);
28-
29-
if (typeof callback === "function") {
30-
return callback(undefined, jobResult);
31-
} else {
32-
return jobResult;
33-
}
34-
});
35-
16+
await jobFunc.apply(Toolbelt, doc.arguments)
17+
.catch(function (error) {
18+
const failure = Toolbelt.failure(error);
19+
20+
Utilities.logger(`Job failed to run due to code error: ${doc.name}`)
21+
console.log(error);
22+
23+
if (typeof callback === "function") {
24+
return callback(error, undefined);
25+
}
26+
})
27+
.then(function (jobResult) {
28+
const resolution = Toolbelt.checkForResolution(jobResult);
29+
30+
if (typeof callback === "function") {
31+
return callback(undefined, jobResult);
32+
} else {
33+
return jobResult;
34+
}
35+
});
3636
} else {
3737
try {
3838
const jobResult = jobFunc.apply(Toolbelt, doc.arguments);
39-
var resolution = Toolbelt.checkForResolution(jobResult);
39+
const resolution = Toolbelt.checkForResolution(jobResult);
4040

4141
if (typeof callback === "function") {
4242
return callback(undefined, jobResult);
4343
} else {
4444
return jobResult;
4545
}
46-
}
47-
48-
catch (e) {
49-
var failure = Toolbelt.failure(e.stack);
46+
} catch (error) {
47+
const failure = Toolbelt.failure(error.stack);
5048

51-
Utilities.logger("Job failed to run due to code error: " + doc.name)
52-
console.log(e);
49+
Utilities.logger(`Job failed to run due to code error: ${doc.name}`)
50+
console.log(error);
5351

5452
if (typeof callback === "function") {
55-
return callback(true, undefined);
53+
return callback(error, undefined);
5654
}
5755
}
5856
}

0 commit comments

Comments
 (0)