Skip to content

Commit

Permalink
Using catch/then for async jobs / bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
msavin committed Apr 19, 2020
1 parent b866938 commit 3db5623
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 28 deletions.
64 changes: 40 additions & 24 deletions package/server/imports/actions/execute/process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,44 +2,60 @@ import { Utilities } from "../../utilities"
import { toolbelt } from "./toolbelt.js"
import { reschedule } from "../reschedule/"


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

var Toolbelt = new toolbelt(doc);
var jobResult;
var jobFunc = Utilities.registry.data[doc.name];
var isAsync = jobFunc.constructor.name === "AsyncFunction";

try {
if (isAsync) {
jobResult = await jobFunc.apply(Toolbelt, doc.arguments);
} else {
jobResult = jobFunc.apply(Toolbelt, doc.arguments);
}


var resolution = Toolbelt.checkForResolution(jobResult);

if (typeof callback === "function") {
return callback(undefined, jobResult);
} else {
return jobResult;
if (isAsync) {
jobResult = await jobFunc.apply(Toolbelt, doc.arguments).catch(function (error) {
var failure = Toolbelt.failure(error);

Utilities.logger("Job failed to run due to code error: " + doc.name)
console.log(error);

if (typeof callback === "function") {
return callback(true, undefined);
}
}).then(function (jobResult) {
var resolution = Toolbelt.checkForResolution(jobResult);

if (typeof callback === "function") {
return callback(undefined, jobResult);
} else {
return jobResult;
}
});

} else {
try {
const jobResult = jobFunc.apply(Toolbelt, doc.arguments);
var resolution = Toolbelt.checkForResolution(jobResult);

if (typeof callback === "function") {
return callback(undefined, jobResult);
} else {
return jobResult;
}
}
}

catch (e) {
var failure = Toolbelt.failure(e.stack);

Utilities.logger("Job failed to run due to code error: " + doc.name)
console.log(e);
catch (e) {
var failure = Toolbelt.failure(e.stack);
Utilities.logger("Job failed to run due to code error: " + doc.name)
console.log(e);

if (typeof callback === "function") {
return callback(true, undefined);
if (typeof callback === "function") {
return callback(true, undefined);
}
}
}
}
}

export { process }
17 changes: 13 additions & 4 deletions package/server/imports/actions/execute/toolbelt.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { remove } from "../remove/"

var toolbelt = function (jobDoc) {
this.document = jobDoc;

this.resolved = false;

this.set = function (key, value) {
Expand Down Expand Up @@ -152,7 +151,9 @@ var toolbelt = function (jobDoc) {

this.failure = function (result) {
var docId = this.document._id;

var queueName = this.document.name;

// Update the document
var update = Utilities.collection.update(docId, {
$set: {
state: "failure",
Expand All @@ -167,6 +168,14 @@ var toolbelt = function (jobDoc) {
}
})

// Stop the queue
Utilities.logger([
"Job has failed: " + queueName + ", " + docId,
"Queue was stopped; please correct your job function and restart the server"
]);

Operator.manager.queues[queueName].stop();

this.resolved = true;

return update;
Expand Down Expand Up @@ -225,9 +234,9 @@ var toolbelt = function (jobDoc) {

this.checkForResolution = function (result) {
var docId = this.document._id;
var queueName = this.document.name;
var resolution = this.resolved;



if (!resolution) this.success(result)
}
}
Expand Down

0 comments on commit 3db5623

Please sign in to comment.