@@ -20,49 +20,30 @@ const run = async () => {
2020 jobShouldTakeThisLongMs : number ;
2121 groupId : string ;
2222 } > ( {
23+ logger : true ,
2324 redis : redis ,
2425 namespace : 'example' ,
25- keepCompleted : 100 ,
26+ keepCompleted : 100000 ,
2627 keepFailed : 100 ,
2728 } ) ;
2829
29- const cronQueue = new Queue < {
30- id : string ;
31- jobShouldTakeThisLongMs : number ;
32- groupId : string ;
33- } > ( {
34- redis : redis ,
35- namespace : 'cron' ,
36- keepCompleted : 100 ,
37- keepFailed : 100 ,
38- } ) ;
30+ const CONCURRENCY = 4 ;
31+ const WORKERS = 4 ;
3932
40- const worker = new Worker ( {
41- concurrency : 2 ,
42- queue : queue ,
43- async handler ( job ) {
44- await sleep ( job . data . jobShouldTakeThisLongMs ) ;
45- if ( Math . random ( ) > 0.9 ) {
46- throw new Error ( 'test error' ) ;
47- }
48- return `basic worker completed job ${ job . id } ` ;
49- } ,
50- } ) ;
51-
52- const cronWorker = new Worker ( {
53- queue : cronQueue ,
54- // Run cleanup/scheduler frequently so repeating jobs trigger on time
55- cleanupIntervalMs : 1000 ,
56- async handler ( job ) {
57- await sleep ( job . data . jobShouldTakeThisLongMs ) ;
58- if ( Math . random ( ) > 0.9 ) {
59- throw new Error ( 'test error' ) ;
60- }
61- return `cron worker completed job ${ job . id } ` ;
62- } ,
63- } ) ;
33+ const createWorker = ( index : number , concurrency : number ) => {
34+ return new Worker ( {
35+ concurrency : concurrency ,
36+ queue : queue ,
37+ async handler ( job ) {
38+ console . log ( `processing job worker ${ index } ` , job . id ) ;
39+ await sleep ( job . data . jobShouldTakeThisLongMs ) ;
40+ } ,
41+ } ) ;
42+ } ;
6443
65- const workers = [ worker , cronWorker ] ;
44+ const workers = Array . from ( { length : WORKERS } , ( _ , i ) =>
45+ createWorker ( i , CONCURRENCY ) ,
46+ ) ;
6647
6748 workers . forEach ( ( worker ) => {
6849 worker . on ( 'completed' , ( job ) => {
@@ -106,58 +87,43 @@ const run = async () => {
10687 const serverAdapter = new HonoAdapter ( serveStatic ) ;
10788
10889 createBullBoard ( {
109- queues : [
110- new BullBoardGroupMQAdapter ( queue ) ,
111- new BullBoardGroupMQAdapter ( cronQueue ) ,
112- ] ,
90+ queues : [ new BullBoardGroupMQAdapter ( queue ) ] ,
11391 serverAdapter,
11492 } ) ;
11593
11694 const basePath = '/ui' ;
11795 serverAdapter . setBasePath ( basePath ) ;
11896 app . route ( basePath , serverAdapter . registerPlugin ( ) ) ;
11997
120- // Add a cron job that runs every 5 seconds
121- await cronQueue . removeRepeatingJob ( 'groupId' , { every : 5000 } ) ;
122- await cronQueue . add ( {
123- data : {
124- id : Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) ,
125- jobShouldTakeThisLongMs : Math . random ( ) * 1000 ,
126- groupId : 'groupId' ,
127- } ,
128- groupId : 'groupId' ,
129- repeat : { every : 5000 } , // every 5 seconds
130- } ) ;
131-
13298 // Add basic job every 2.5 seconds
13399 const groups = [
134100 ...new Array ( 25 ) . fill ( null ) . map ( ( _ , i ) => `groupId_${ i + 1 } ` ) ,
135101 ] ;
136- setInterval ( async ( ) => {
137- const groupId = groups [ Math . floor ( Math . random ( ) * groups . length ) ] ! ;
138- await queue . add ( {
139- data : {
140- id : Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) ,
141- jobShouldTakeThisLongMs : Math . random ( ) * 1000 ,
142- groupId,
143- } ,
144- groupId,
145- } ) ;
146- } , 1_000 ) ;
102+ // setInterval(async () => {
103+ // const groupId = groups[Math.floor(Math.random() * groups.length)]!;
104+ // await queue.add({
105+ // data: {
106+ // id: Math.random().toString(36).substring(2, 15),
107+ // jobShouldTakeThisLongMs: Math.random() * 1000,
108+ // groupId,
109+ // },
110+ // groupId,
111+ // });
112+ // }, 1_000);
147113
148114 app . get ( '/add' , async ( c ) => {
149115 const groups = [
150- ...new Array ( 25 ) . fill ( null ) . map ( ( _ , i ) => `groupId_${ i + 1 } ` ) ,
116+ ...new Array ( 1000 ) . fill ( null ) . map ( ( _ , i ) => `groupId_${ i + 1 } ` ) ,
151117 ] ;
152118 const events = [
153- ...new Array ( 50 ) . fill ( null ) . map ( ( _ , i ) => `event_${ i + 1 } ` ) ,
119+ ...new Array ( 200 ) . fill ( null ) . map ( ( _ , i ) => `event_${ i + 1 } ` ) ,
154120 ] ;
155121 for ( const event of events ) {
156122 const groupId = groups [ Math . floor ( Math . random ( ) * groups . length ) ] ! ;
157123 await queue . add ( {
158124 data : {
159125 id : event ,
160- jobShouldTakeThisLongMs : Math . random ( ) * 500 + 500 ,
126+ jobShouldTakeThisLongMs : Math . random ( ) * 40 + 20 ,
161127 groupId,
162128 } ,
163129 groupId,
@@ -167,6 +133,20 @@ const run = async () => {
167133 return c . json ( { message : `${ events . length } jobs added` } ) ;
168134 } ) ;
169135
136+ app . get ( '/add-single' , async ( c ) => {
137+ const groupId = `groupId_${ Math . floor ( Math . random ( ) * 100 ) + 1 } ` ;
138+ const event = `event_${ Math . floor ( Math . random ( ) * 200 ) + 1 } ` ;
139+ await queue . add ( {
140+ data : {
141+ id : event ,
142+ jobShouldTakeThisLongMs : Math . random ( ) * 40 + 20 ,
143+ groupId,
144+ } ,
145+ groupId,
146+ } ) ;
147+ return c . json ( { message : 'job added' } ) ;
148+ } ) ;
149+
170150 showRoutes ( app ) ;
171151
172152 serve ( { fetch : app . fetch , port : 3000 } , ( { address, port } ) => {
0 commit comments