88 */
99
1010import { test } from '@japa/runner'
11+ import { fileURLToPath } from 'node:url'
12+ import { Locator } from '@boringnode/queue'
13+ import { sync } from '@boringnode/queue/drivers/sync_adapter'
14+
1115import { setupApp } from './helpers.js'
16+ import QueueWork from '../commands/queue_work.js'
17+ import { defineConfig } from '../index.js'
18+
19+ const BASE_URL = new URL ( './tmp/' , import . meta. url )
20+
21+ test . group ( 'Provider' , ( group ) => {
22+ group . each . setup ( ( { context } ) => {
23+ context . fs . baseUrl = BASE_URL
24+ context . fs . basePath = fileURLToPath ( BASE_URL )
25+
26+ Locator . clear ( )
27+ context . cleanup ( ( ) => Locator . clear ( ) )
28+ } )
1229
13- test . group ( 'Provider' , ( ) => {
1430 test ( 'should resolve queue manager from container' , async ( { assert } ) => {
1531 const app = await setupApp ( )
1632
@@ -20,4 +36,156 @@ test.group('Provider', () => {
2036 assert . isFunction ( queueManager . use )
2137 assert . isFunction ( queueManager . destroy )
2238 } )
39+
40+ test ( 'should load jobs when booting outside of console' , async ( { assert, fs } ) => {
41+ await fs . create (
42+ 'app/jobs/provider_web_job.ts' ,
43+ `
44+ import { Job } from '@boringnode/queue'
45+
46+ export default class ProviderWebJob extends Job {
47+ async execute() {}
48+ }
49+ `
50+ )
51+
52+ await setupApp ( 'web' , {
53+ queue : defineConfig ( {
54+ default : 'sync' ,
55+ adapters : {
56+ sync : sync ( ) ,
57+ } ,
58+ locations : [ `${ fs . basePath } /app/jobs/provider_web_job.ts` ] ,
59+ } ) ,
60+ } )
61+
62+ assert . equal ( Locator . get ( 'ProviderWebJob' ) ?. name , 'ProviderWebJob' )
63+ } )
64+
65+ test ( 'should not load jobs automatically in console' , async ( { assert, fs } ) => {
66+ await fs . create (
67+ 'app/jobs/provider_console_job.ts' ,
68+ `
69+ import { Job } from '@boringnode/queue'
70+
71+ export default class ProviderConsoleJob extends Job {
72+ async execute() {}
73+ }
74+ `
75+ )
76+
77+ await setupApp ( 'console' , {
78+ queue : defineConfig ( {
79+ default : 'sync' ,
80+ adapters : {
81+ sync : sync ( ) ,
82+ } ,
83+ locations : [ `${ fs . basePath } /app/jobs/provider_console_job.ts` ] ,
84+ } ) ,
85+ } )
86+
87+ assert . isUndefined ( Locator . get ( 'ProviderConsoleJob' ) )
88+ } )
89+
90+ test ( 'should execute sync jobs outside of console' , async ( { assert, cleanup, fs } ) => {
91+ ; ( globalThis as any ) . __adonisQueueSyncRuns = 0
92+ cleanup ( ( ) => {
93+ delete ( globalThis as any ) . __adonisQueueSyncRuns
94+ } )
95+
96+ await fs . create (
97+ 'app/jobs/provider_sync_job.ts' ,
98+ `
99+ import { Job } from '@boringnode/queue'
100+
101+ export default class ProviderSyncJob extends Job {
102+ async execute() {
103+ globalThis.__adonisQueueSyncRuns++
104+ }
105+ }
106+ `
107+ )
108+
109+ await setupApp ( 'web' , {
110+ queue : defineConfig ( {
111+ default : 'sync' ,
112+ adapters : {
113+ sync : sync ( ) ,
114+ } ,
115+ locations : [ `${ fs . basePath } /app/jobs/provider_sync_job.ts` ] ,
116+ } ) ,
117+ } )
118+
119+ const { default : ProviderSyncJob } = await import (
120+ new URL ( 'app/jobs/provider_sync_job.ts' , BASE_URL ) . href
121+ )
122+
123+ await ProviderSyncJob . dispatch ( { } )
124+
125+ assert . equal ( ( globalThis as any ) . __adonisQueueSyncRuns , 1 )
126+ } )
127+
128+ test ( 'queue:work should load jobs before starting the worker' , async ( { assert } ) => {
129+ const { Worker } = await import ( '@boringnode/queue' )
130+ const originalStart = Worker . prototype . start
131+ const order : string [ ] = [ ]
132+ const config = defineConfig ( {
133+ default : 'sync' ,
134+ adapters : {
135+ sync : sync ( ) ,
136+ } ,
137+ locations : [ './app/jobs/*.ts' ] ,
138+ } )
139+
140+ Worker . prototype . start = async ( ) => {
141+ order . push ( 'start' )
142+ }
143+
144+ try {
145+ await QueueWork . prototype . run . call ( {
146+ app : {
147+ config : {
148+ get ( ) {
149+ return config
150+ } ,
151+ } ,
152+ container : {
153+ async make ( binding : string ) {
154+ if ( binding === 'queue.manager' ) {
155+ return {
156+ async loadJobs ( ) {
157+ order . push ( 'loadJobs' )
158+ } ,
159+ async destroy ( ) {
160+ order . push ( 'destroy' )
161+ } ,
162+ }
163+ }
164+
165+ if ( binding === 'logger' ) {
166+ return { }
167+ }
168+
169+ if ( binding === 'router' ) {
170+ return {
171+ commit ( ) {
172+ order . push ( 'router' )
173+ } ,
174+ }
175+ }
176+
177+ throw new Error ( `Unexpected binding: ${ binding } ` )
178+ } ,
179+ } ,
180+ } ,
181+ logger : {
182+ info ( ) { } ,
183+ } ,
184+ } )
185+ } finally {
186+ Worker . prototype . start = originalStart
187+ }
188+
189+ assert . deepEqual ( order , [ 'router' , 'loadJobs' , 'start' , 'destroy' ] )
190+ } )
23191} )
0 commit comments