Created
June 9, 2020 20:14
-
-
Save jaschaio/3781723aed359379fe78bd0781972c7d to your computer and use it in GitHub Desktop.
Bullmq Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const cluster = require( 'cluster' ); | |
const { Queue, Worker } = require( 'bullmq' ); | |
if ( cluster.isMaster ) { | |
var hrstart; | |
for ( let i = 0; i++ < 4; ) { | |
let worker = cluster.fork(); | |
worker.on( 'message', ( msg ) => { | |
const { start, end } = msg; | |
if ( start ) { | |
hrstart = process.hrtime(); | |
console.log( "started queue" ); | |
} else { | |
var hrend = process.hrtime( hrstart ); | |
console.log( `finished queue in ${ hrend[ 0 ] }s ${ hrend[ 1 ] / 1000000 }ms` ); | |
} | |
} ); | |
} | |
} else { | |
( async () => { | |
const queue = new Queue( 'queue' ); | |
if ( cluster.worker.id === 1 ) { | |
var promises = []; | |
for ( let i = 0; i++ < 10000; ) | |
promises.push( queue.add( 'job', { i } ) ) | |
await Promise.all( promises );; | |
} else { | |
const worker = new Worker( 'queue', async ( job ) => { | |
const { i } = job.data; | |
var timeout = new Promise( ( resolve ) => setTimeout( () => resolve(), 10 ) ); | |
await timeout; | |
if ( i === 1 ) | |
process.send( { start: true } ); | |
else if ( i === 10000 ) | |
process.send( { end: true } ) | |
} ); | |
} | |
} )(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment