Skip to content

Instantly share code, notes, and snippets.

@jaschaio
Last active June 9, 2020 20:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaschaio/2faecb0287d2949440e618b868551977 to your computer and use it in GitHub Desktop.
Save jaschaio/2faecb0287d2949440e618b868551977 to your computer and use it in GitHub Desktop.
Bull Test
const cluster = require( 'cluster' );
const Bull = require( 'bull' );
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 Bull( '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 {
queue.process( 'job', 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