1+ import { Promise } from "meteor/promise"
12import { Utilities } from "../../utilities"
23import { toolbelt } from "./toolbelt.js"
34import { reschedule } from "../reschedule/"
@@ -9,51 +10,27 @@ const process = async function (doc, callback) {
910 // 3- Capture the result (if any)
1011
1112 const Toolbelt = new toolbelt ( doc ) ;
12- const jobFunc = Utilities . registry . data [ doc . name ] ;
13- const isAsync = jobFunc . constructor . name === "AsyncFunction" ;
1413
15- if ( isAsync ) {
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 ) ;
14+ try {
15+ const res = Utilities . registry . data [ doc . name ] . apply ( Toolbelt , doc . arguments ) ;
16+ const jobResult = Promise . await ( Promise . resolve ( res ) ) ;
17+ const resolution = Toolbelt . checkForResolution ( jobResult ) ;
2218
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- } ) ;
36- } else {
37- try {
38- const jobResult = jobFunc . apply ( Toolbelt , doc . arguments ) ;
39- const resolution = Toolbelt . checkForResolution ( jobResult ) ;
40-
41- if ( typeof callback === "function" ) {
42- return callback ( undefined , jobResult ) ;
43- } else {
44- return jobResult ;
45- }
46- } catch ( error ) {
47- const failure = Toolbelt . failure ( error . stack ) ;
48-
49- Utilities . logger ( `Job failed to run due to code error: ${ doc . name } ` )
50- console . log ( error ) ;
51-
52- if ( typeof callback === "function" ) {
53- return callback ( error , undefined ) ;
54- }
19+ if ( typeof callback === "function" ) {
20+ return callback ( undefined , jobResult ) ;
21+ } else {
22+ return jobResult ;
23+ }
24+ } catch ( error ) {
25+ const failure = Toolbelt . failure ( error . stack ) ;
26+
27+ Utilities . logger ( `Job failed to run due to code error: ${ doc . name } ` )
28+ console . log ( error ) ;
29+
30+ if ( typeof callback === "function" ) {
31+ return callback ( error , undefined ) ;
5532 }
56- }
33+ }
5734}
5835
5936export { process }
0 commit comments