nl_tokens: In your projects : const NodeResque = require ( node - resque ) ;
pl_tokens: async function boot ( ) { // //////////////////////// // SET UP THE CONNECTION // // //////////////////////// const connectionDetails = { pkg : 'ioredis' , host : '127.0.0.1' , password : null , port : 6379 , database : 0 // namespace: 'resque', // looping: true, // options: {password: 'abc'}, } // /////////////////////////// // DEFINE YOUR WORKER TASKS // // /////////////////////////// let jobsToComplete = 0 const jobs = { 'add' : { plugins : [ 'JobLock' ] , pluginOptions : { JobLock : { } } , perform : async ( a , b ) => { await new Promise ( ( resolve ) => { setTimeout ( resolve , 1000 ) } ) jobsToComplete -- tryShutdown ( ) let answer = a + b return answer } } , 'subtract' : { perform : ( a , b ) => { jobsToComplete -- tryShutdown ( ) let answer = a - b return answer } } } // just a helper for this demo async function tryShutdown ( ) { if ( jobsToComplete === 0 ) { await new Promise ( ( resolve ) => { setTimeout ( resolve , 500 ) } ) await scheduler . end ( ) await worker . end ( ) process . exit ( ) } } // ///////////////// // START A WORKER // // ///////////////// const worker = new NodeResque . Worker ( { connection : connectionDetails , queues : [ 'math' , 'otherQueue' ] } , jobs ) await worker . connect ( ) worker . start ( ) // //////////////////// // START A SCHEDULER // // //////////////////// const scheduler = new NodeResque . Scheduler ( { connection : connectionDetails } ) await scheduler . connect ( ) scheduler . start ( ) // ////////////////////// // REGESTER FOR EVENTS // // ////////////////////// worker . on ( 'start' , ( ) => { console . log ( 'worker started' ) } ) worker . on ( 'end' , ( ) => { console . log ( 'worker ended' ) } ) worker . <mask> ( 'cleaning_worker' , ( worker , pid ) => { console . log ( ` ${ worker } ` ) } ) worker . on ( 'poll' , ( queue ) => { console . log ( ` ${ queue } ` ) } ) worker . on ( 'ping' , ( time ) => { console . log ( ` ${ time } ` ) } ) worker . on ( 'job' , ( queue , job ) => { console . log ( ` ${ queue } ${ JSON . stringify ( job ) } ` ) } ) worker . on ( 'reEnqueue' , ( queue , job , plugin ) => { console . log ( ` ${ plugin } ${ queue } ${ JSON . stringify ( job ) } ` ) } ) worker . on ( 'success' , ( queue , job , result ) => { console . log ( ` ${ queue } ${ JSON . stringify ( job ) } ${ result } ` ) } ) worker . on ( 'failure' , ( queue , job , failure ) => { console . log ( ` ${ queue } ${ JSON . stringify ( job ) } ${ failure } ` ) } ) worker . on ( 'error' , ( error , queue , job ) => { console . log ( ` ${ queue } ${ JSON . stringify ( job ) } ${ error } ` ) } ) worker . on ( 'pause' , ( ) => { console . log ( 'worker paused' ) } ) scheduler . on ( 'start' , ( ) => { console . log ( 'scheduler started' ) } ) scheduler . on ( 'end' , ( ) => { console . log ( 'scheduler ended' ) } ) scheduler . on ( 'poll' , ( ) => { console . log ( 'scheduler polling' ) } ) scheduler . on ( 'master' , ( state ) => { console . log ( 'scheduler became master' ) } ) scheduler . on ( 'error' , ( error ) => { console . log ( ` ${ error } ` ) } ) scheduler . on ( 'cleanStuckWorker' , ( workerName , errorPayload , delta ) => { console . log ( ` ${ workerName } ${ delta } ${ errorPayload } ` ) } ) scheduler . on ( 'workingTimestamp' , ( timestamp ) => { console . log ( ` ${ timestamp } ` ) } ) scheduler . on ( 'transferredJob' , ( timestamp , job ) => { console . log ( ` ${ timestamp } ${ JSON . stringify ( job ) } ` ) } ) // ////////////////////// // CONNECT TO A QUEUE // // ////////////////////// const queue = new NodeResque . Queue ( { connection : connectionDetails } , jobs ) queue . on ( 'error' , function ( error ) { console . log ( error ) } ) await queue . connect ( ) await queue . enqueue ( 'math' , 'add' , [ 1 , 2 ] ) await queue . enqueue ( 'math' , 'add' , [ 1 , 2 ] ) await queue . enqueue ( 'math' , 'add' , [ 2 , 3 ] ) await queue . enqueueIn ( 3000 , 'math' , 'subtract' , [ 2 , 1 ] ) jobsToComplete = 4 }