@@ -2,44 +2,60 @@ import { Utilities } from "../../utilities"
22import { toolbelt } from "./toolbelt.js"
33import { reschedule } from "../reschedule/"
44
5+
56var process = async function ( doc , callback ) {
67 // Goals:
78 // 1- Execute the job
89 // 2- Update the document in database
910 // 3- Capture the result (if any)
1011
1112 var Toolbelt = new toolbelt ( doc ) ;
12- var jobResult ;
1313 var jobFunc = Utilities . registry . data [ doc . name ] ;
1414 var isAsync = jobFunc . constructor . name === "AsyncFunction" ;
1515
16- try {
17- if ( isAsync ) {
18- jobResult = await jobFunc . apply ( Toolbelt , doc . arguments ) ;
19- } else {
20- jobResult = jobFunc . apply ( Toolbelt , doc . arguments ) ;
21- }
22-
23-
24- var resolution = Toolbelt . checkForResolution ( jobResult ) ;
25-
26- if ( typeof callback === "function" ) {
27- return callback ( undefined , jobResult ) ;
28- } else {
29- return jobResult ;
16+ 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+
36+ } else {
37+ try {
38+ const jobResult = jobFunc . apply ( Toolbelt , doc . arguments ) ;
39+ var resolution = Toolbelt . checkForResolution ( jobResult ) ;
40+
41+ if ( typeof callback === "function" ) {
42+ return callback ( undefined , jobResult ) ;
43+ } else {
44+ return jobResult ;
45+ }
3046 }
31- }
3247
33- catch ( e ) {
34- var failure = Toolbelt . failure ( e . stack ) ;
35-
36- Utilities . logger ( "Job failed to run due to code error: " + doc . name )
37- console . log ( e ) ;
48+ catch ( e ) {
49+ var failure = Toolbelt . failure ( e . stack ) ;
50+
51+ Utilities . logger ( "Job failed to run due to code error: " + doc . name )
52+ console . log ( e ) ;
3853
39- if ( typeof callback === "function" ) {
40- return callback ( true , undefined ) ;
54+ if ( typeof callback === "function" ) {
55+ return callback ( true , undefined ) ;
56+ }
4157 }
42- }
58+ }
4359}
4460
4561export { process }
0 commit comments