Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stueynet's avatar

What data does a job use?

So when you render a job like so:

$job = (new CheckStatusOfFax($this->order))->delay(120);
$this->dispatch($job);

When it comes time to actually process the Job Class, is it doing a new query to get $this->order? In other words if that object changes over the 120 seconds, will it be used? The jobs table only seems to hold references to model IDs and not actual data so I assume when it comes time to actually process a job, it needs to get the data from somewhere. Or is this data cached somewhere? I am wondering because I have some process that require multiple jobs to be dispatched using the same resource.

Any help here would be appreciated.

0 likes
5 replies
davorminchorov's avatar

The job uses the data you pass to it. Just like any other class but the only difference is that the Job class can be queued and ran in background.

stueynet's avatar

@Ruffles where does it store the data? So when that job runs it gets attributes from that order model that I passed in. In those 2 minutes that it's waiting, if something changes on the model will the job run with the new attributes or the old ones?

stueynet's avatar

Thanks @toniperic. Yes that does help. So the issue would be that if you have an object that you want to run multiple jobs on in succession, if an earlier job made a change that the latter job might depend on, you are sort of out of luck I guess. For example if one job makes a slow API call that updates the model. Then a second job that runs after 60 seconds, sends an email to the user with the new model. If you queued them up at the same time, the second job would send an email without the information that was changed. So example:

// Initially $order->fax_id === null

// Make an API call and save the result to the database in the order model
$job = (new SendOrderFax($this->order));
$this->dispatch($job);

// Now $order->fax_id === '12345')

// Check on the status of the fax based on what was saved from step 1
// It uses the fax_id returned from the first API 
// call to check the fax after 2 minutes

$job = (new CheckStatusOfFax($this->order))->delay(120);
$this->dispatch($job);

// within this class we call the API using the result returned form the first job as follows:
$fax->getStatus(['id' => $this->order->fax_id]);

So in the above case, when the second job runs after 120 seconds, it is used an object where the fax_id would still be null?

Thus in these cases rather than access the passed model in the Job class should we in fact just use the model ID and then re-query the database? So in the second Job class do something like:

$order = Order::find($this->order->id);
$fax->getStatus(['id' => $order->id]);

Is that necessary? Also when something is pushed onto the queue, it is sitting in the database in the Jobs table. Where is the data sitting? You are saying the data is serialized but where exactly does it live while it waits to be used? Its not in the jobs table itself as far as I can see.

stueynet's avatar
stueynet
OP
Best Answer
Level 5

Wait I just found the answer in the docs (https://laravel.com/docs/5.3/queues):

"If your queued job accepts an Eloquent model in its constructor, only the identifier for the model will be serialized onto the queue. When the job is actually handled, the queue system will automatically re-retrieve the full model instance from the database. "

Please or to participate in this conversation.