A job's models are read from the worker's default connection before anything can switch it
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
Research direction
Read crates/queue/src/queue/job.rs around lines 374–393 and the scoped default connections and models in jobs sections of the database and queues guides. The issue leaves the behavior open among several approaches, so first get a maintainer decision before implementing anything. The reported tests are in tests/queue_models.rs; done means the agreed behavior works for both cases in the reproduction and is documented.
Written by the indexing model from the issue text.
Description
The problem
The worker restores a job from its payload, which reads its model fields from the database, and only then builds and runs the job's middleware (job.rs#L374-L393). Each run also starts in a fresh context, so a db::using_connection scope that was active at dispatch doesn't carry over. Nothing an application or package writes runs before the models are read, so they always come from the worker's default connection.
For an application that uses scoped default connections, for example one database per tenant, a job with a model field then does one of two things:
- If the default connection has a row with the same key, the job runs with that row. Nothing reports it.
- If it doesn't, the worker deletes the job with the
MissingModelsoutcome before any middleware runs, or fails it for good withfail_when_missing_models.
The same applies to a queued listener whose event holds models (including Created<M> and Updated<M>), to the failed hook, which restores the job again outside its middleware, and to chain and batch callbacks, which have no middleware.
#226 listed the connection as its first open question ("A model without #[model(connection)] dispatched inside db::using_connection is restored from its default connection"). I couldn't find a later decision, so I'm asking here.
Reproduction
Two SQLite connections, primary (the default) and archive, each with a users table and a user under key 1 with a different name.
use laravel::container::Inject;
use laravel::db;
use laravel::queue::{IntoMiddleware, Next, Queueable, job, middleware};
#[middleware]
async fn on_archive(next: Next) -> laravel::Result<()> {
db::using_connection("archive", next.run()).await
}
#[derive(Queueable)]
struct Greet {
user: User,
}
#[job]
impl Greet {
fn middleware(&self) -> impl IntoMiddleware {
on_archive
}
async fn handle(self, records: Inject<Records>) {
let connection = db::manager().default_connection_name();
records.push(format!("greeted {} on {connection}", self.user.name));
}
}
Dispatching Greet { user } with the archive user and running queue:work:
primary has a user with key 1 |
Expected | Actual |
|---|---|---|
| Yes | greeted Archived Taylor on archive |
greeted Taylor on archive |
| No | greeted Archived Taylor on archive |
Job deleted, JobProcessed reports missing models |
I have both cases as tests in tests/queue_models.rs, in the style of the existing ones, and can open a PR with them.
What works today
Carrying the key and reading the model in handle works, because handle runs inside the middleware:
#[derive(Queueable)]
struct Greet {
user_id: i64,
}
The queues guide doesn't say this is needed. Models in jobs says the worker reloads the model "before handle runs", and Creating jobs says context values "such as the active tenant" reach the job, which reads as if a tenant-aware job with a model field would work.
Options I considered
- Document it. Say in the queues guide that models are read before middleware, from the worker's default connection, and that a job dispatched for another connection should carry keys. No code change. The wrong-row case stays silent for anyone who hasn't read it.
- Carry the scoped default connection in the payload, as #226 hinted, and apply it around the job's contexts on the worker. No new public API. It changes the rule that a new application scope doesn't inherit
using_connection, and a connection added at runtime may not exist in the worker. - A queue-wide hook that wraps each job's context, registered once like global HTTP middleware, which runs before the restore. It would also need to cover the
failedhook and callbacks. One new registration method. - Restore in two phases, so job middleware runs before models load.
middleware(&self)takes the restored job, so this changes the signature ofJob::middlewareandQueuedListener::middleware. It still leaves thefailedhook and callbacks. - A field type that opts out of loading. This is what an
i64field already does.
Options 2 and 3 are the only ones that remove the silent case, and both are design decisions I don't think a contributor should make alone.
Question
Which direction would you prefer? I'm opening a docs-only PR for option 1 and will link it here. I'm happy to implement option 2 or 3 if you say which fits. If the answer is that models in jobs are meant for the default connection only, the docs change alone would settle it.
- Dominant language
- Rust
- Stars
- 847
- Forks
- 174
- Avg merge
- 5h 40m
- Merged PRs (30d)
- 167
Getting set up
This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from laravel/rust-framework
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
laravel/rust-framework#864 ·
Maintainers usually reply within 1 day
-
Difficulty 3/5 1-2 days Newbie friendliness 64/100
laravel/rust-framework#876 ·
Maintainers usually reply within 1 day
-
Case-sensitive contains, starts_with and ends_with that match % and _ literally on every databaseOpen
Difficulty 4/5 3-5 days Newbie friendliness 58/100
laravel/rust-framework#874 ·
Maintainers usually reply within 1 day
-
Difficulty 5/5 Over a week Newbie friendliness 35/100
laravel/rust-framework#873 ·
Maintainers usually reply within 1 day
-
Difficulty 4/5 3-5 days Newbie friendliness 58/100
laravel/rust-framework#872 ·
Maintainers usually reply within 1 day
All issues in laravel/rust-framework
Similar issues
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 83/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
neuralmagic/crucible#579 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 62/100
eclipse-score/kyron#249 ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 67/100
seanmonstar/reqwest#3157 ·
Maintainers usually reply within 2 days