Provide a method that allows to obtain the Job Id from a dispatched job. #47432
Replies: 5 comments 2 replies
-
The thing is, PendingDispatch, does not dispatch until __destruct. until then, there's no Id. not sure what you wanted to do with the Id? |
Beta Was this translation helpful? Give feedback.
-
I have a similar issue here #48107 My suggestion would be to give PendingDispatch some events so we can react to the behaviour. You can try use this package to track jobs: https://github.com/mateusjunges/trackable-jobs-for-laravel Here is my PR for v2 which include Job ID tracking (work in progress): https://github.com/DimaVIII/trackable-jobs-for-laravel/tree/features-v2 Than updating the Job ID: public function handle(JobQueued $event): void
{
if (isset($event->job->trackedJob))
{
$event->job->trackedJob->markAsQueued($event->id);
}
} |
Beta Was this translation helpful? Give feedback.
-
I didn't want to mess around with it for too long as I saw there isn't a legitimate solution provided in the Laravel docs so I solved my problem using a workaround. I had a Model I was manipulating inside the job (it was connected one-on-one with the job), so instead of accepting the model object I changed the job's constructor to accept the id. When I delay dispatch the job, in it's handle method, I query for that object with the id provided in the constructor earlier. Then, I check for a boolean flag inside of that model (meanwhile, before the job is actually executed, I update the object's flag field to true in the DB, so that I know it should be cancelled). I guess this could be done either way, even if you don't have such logic present you could introduce another table and solve the issue, but I believe it is not the best solution out there. |
Beta Was this translation helpful? Give feedback.
-
That is my use-case, too. That's why I ended up here. |
Beta Was this translation helpful? Give feedback.
-
For anyone having the same problem and coming here to find a solution - you can use one of the following methods instead to dispatch a job and get the jobId instantly: $job = new YourJob();
$jobId = Bus::dispatch($job);
// Or
$jobId = Queue::push($job); It's stated in the Laravel documentation for Tinker (below in the info box): https://laravel.com/docs/12.x/artisan#usage |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I observed that a some point in Laravel 10.x is not possible to obtain the dispatched job Id from a controller. It could be nice if "\Illuminate\Foundation\Bus\PendingDispatch" implements a method that allow to obtain the job id in a easy way.
I known that is possible to have the old behaviour calling the method "dispatch" from "\Illuminate\Contracts\Bus\Dispatcher::class" (Ex:
app(\Illuminate\Contracts\Bus\Dispatcher::class)->dispatch($job)
), but it could be very more convenient having a method for "PendingDispatch".I also observed that Laravel upgrade guide doesn't mention this change.
Beta Was this translation helpful? Give feedback.
All reactions