Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions providers/queue_provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import '../src/types/extended.js'
import { resolveAdapters, resolveHotReload, resolveJobFactory } from '../src/utils.js'
import type { JobData } from '@boringnode/queue/types'
import type { ApplicationService } from '@adonisjs/core/types'
import type { QueueConfig } from '../src/types/main.js'

Expand Down Expand Up @@ -52,8 +53,41 @@ export default class QueueProvider {
return this.app.getMode() === 'warmup'
}

/**
* Preload files, such as "start/scheduler.ts", still run while warming up
* the app. Faking every adapter turns the schedules and dispatches they
* define into no-ops, without opening a connection.
*/
async #initWithFakeAdapters() {
const { QueueManager } = await import('@boringnode/queue')
const { FakeAdapter } = await import('@boringnode/queue/drivers/fake_adapter')
const config = this.app.config.get<QueueConfig>('queue')
const logger = await this.app.container.make('logger')

/**
* The fake adapter holds delayed jobs in a timer, which would keep a
* warmed up app (and commands like "node ace codegen") alive until the
* delay elapses. Delayed jobs are pushed right away instead.
*/
class WarmupAdapter extends FakeAdapter {
pushLaterOn(queue: string, job: JobData) {
return this.pushOn(queue, job)
}
}

await QueueManager.init({
...config,
autoLoadJobs: false,
adapters: Object.fromEntries(
Object.keys(config.adapters).map((name) => [name, () => new WarmupAdapter()])
),
logger: config.logger ?? (logger as any),
})
}

async boot() {
if (this.#isWarmingUp()) {
await this.#initWithFakeAdapters()
return
}

Expand Down
44 changes: 43 additions & 1 deletion tests/provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
import { test } from '@japa/runner'
import { fileURLToPath } from 'node:url'
import { configProvider } from '@adonisjs/core'
import { Locator } from '@boringnode/queue'
import { Job, Locator } from '@boringnode/queue'
import { sync } from '@boringnode/queue/drivers/sync_adapter'

import { setupApp } from './helpers.js'
Expand Down Expand Up @@ -318,6 +318,48 @@ test.group('Provider', (group) => {
assert.isUndefined(Locator.get('ProviderWarmupJob'))
})

test('should accept schedules and dispatches while warming up the app', async ({ assert }) => {
let resolvedAdapters = 0
await setupApp(
'web',
{
queue: defineConfig({
default: 'sync',
adapters: {
sync: configProvider.create(async () => {
resolvedAdapters++
return sync()
}),
},
}),
},
'warmup'
)

let executions = 0
class WarmupScheduledJob extends Job {
async execute() {
executions++
}
}

/**
* A pending timer would keep the warmed up app alive until the delay
* elapses, since its providers are never shut down
*/
const countTimers = () =>
process.getActiveResourcesInfo().filter((resource) => resource === 'Timeout').length
const timersBeforeDispatch = countTimers()

await WarmupScheduledJob.schedule({}).id('warmup-schedule').every('1m').run()
await WarmupScheduledJob.dispatch({})
await WarmupScheduledJob.dispatch({}).in('1h')

assert.equal(executions, 0)
assert.equal(resolvedAdapters, 0)
assert.equal(countTimers(), timersBeforeDispatch)
})

test('should resolve the queue manager when booting the app', async ({ assert }) => {
let resolvedAdapters = 0
await setupApp('web', {
Expand Down
Loading