Skip to content

@adonisjs/queue eagerly loads jobs during migration:run #5

@diego-sepulveda-lavin

Description

@diego-sepulveda-lavin

Package version

@adonisjs/queue@0.6.0

Describe the bug

Summary

@adonisjs/queue appears to eagerly initialize job autodiscovery during unrelated Ace commands like node ace migration:run.

In this reproduction repo:

  • queue autodiscovery is enabled via locations in config/queue.ts
  • app/jobs/check_user_job.ts imports User
  • app/models/user.ts extends UserSchema from #database/schema

After rolling back all migrations, database/schema.ts no longer exports UserSchema. Running node ace migration:run then causes queue boot to import the job before the migration regenerates the schema classes.

This does not stop the migration from finishing, but it does emit a boot-time error for an unrelated job import during command startup.

Steps To Reproduce

node ace migration:rollback --batch=0
node ace migration:run

What Seems To Happen

The boot chain seems to be:

  1. Run an unrelated Ace command such as node ace migration:run
  2. App boots in console mode
  3. Queue initializes and scans locations
  4. Job files are imported during startup
  5. A job import pulls in a model
  6. The model imports a generated class from #database/schema
  7. If database/schema.ts is temporarily stale or empty after rollback, startup logs the import failure before the migration regenerates the schema classes

Actual Behavior

During migration:run, queue autodiscovery imports the job too early and logs:

Failed to load job from app/jobs/check_user_job.ts: .../app/models/user.ts:1
import { UserSchema } from '#database/schema'
SyntaxError: The requested module '#database/schema' does not provide an export named 'UserSchema'

After that, the migrations still complete and database/schema.ts is regenerated successfully.

So the problem is not that the migration fails. The problem is that queue boot imports autodiscovered jobs during startup for an unrelated console command.

Expected Behavior

Unrelated console commands like migration:run should not require every autodiscovered job to be importable before the command finishes its own work.

I do not think the fix is to disable queue support in console entirely, since queue console commands still need that environment, for example:

  • node ace queue:work
  • node ace queue:scheduler:list
  • node ace queue:scheduler:clear
  • node ace queue:scheduler:remove

The issue seems more like queue initialization and job discovery happening for all console commands instead of only queue-related ones or actual queue usage.

Notes

  • This reproduction repo uses SQLite, but I am having it also on my app with Postgres.
  • If locations is commented out in config/queue.ts, the error does not happen.
  • This suggests the issue is tied to eager queue job autodiscovery during unrelated console boot, rather than migrations themselves.

Versions

  • @adonisjs/core: ^7.3.1
  • @adonisjs/lucid: ^22.4.2
  • @adonisjs/queue: ^0.6.0
  • better-sqlite3: ^12.9.0

Reproduction repo

https://github.com/diego-sepulveda-lavin/adonisjs_queue_bug_reproduction

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions