How to run Laravel job without a using a queue worker

How to create a job that runs without a queue service and the Job should be dispatched right after sending the response back to end user? You can use dispatchAfterResponse() to run a job right after sending response back to user. This is extremely useful in many occasions.

Real world use of dispatchAfterResponse() method

Imagine you have a third-party service that is not available 24/7 or there are situations where it is not working as intended. You want the request to be successful even if the third-party service failed or succeeded in providing a better user experience. Your Laravel web application does not use Laravel queue services at all. Does not want to implement such a service for this single situation. Use the dispatchAfterResponse() method on the job class to execute right after sending a response back to the user and the server will handle all the problems caused by that unreliable third-party service. The end user will always receive a good response and able continue his/her work.

Example

Create a sample job

php artisan make:job ProcessImageData

Modify handle() like below code to identify whether job is executed or not.

app/Jobs/ProcessImageData.php
<?php namespace App\Jobs; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Queue\Queueable; use Illuminate\Support\Facades\Log; class ProcessImageData implements ShouldQueue { use Queueable; /** * Execute the job. */ public function handle(): void { Log::info("Running Process Image Data Job on the queue"); } }

Now change web.php file to dispatch above job class normally without using dispatchAfterResponse()

routes/web.php
use App\Jobs\ProcessImageData; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Route; Route::get('/', function () { Log::info('Received a request on the root domain'); ProcessImageData::dispatch(); });

Then run queue worker using the artisan command below.

php artisan queue:work

Load root domain of your web application to execute above sample code. Then check your terminal where you run above queue work artisan command. You will see that job was hand over to queue worker.

D> php artisan queue:work
2024-12-08 07:58:12 App\Jobs\ProcessImageData ...........................RUNNING
2024-12-08 07:58:12 App\Jobs\ProcessImageData .................... 103.75ms DONE

Change dispatch() method dispatchAfterResponse() run above job without using the queue worker. (No need to run artisan queue:work)

routes/web.php
use App\Jobs\ProcessImageData; use Illuminate\Support\Facades\Log; use Illuminate\Support\Facades\Route; Route::get('/', function () { Log::info('Received a request on the root domain'); ProcessImageData::dispatchAfterResponse(); });

Now check your terminal. You won’t be able see queued job in the terminal. Check Laravel logs. Job is executed successfully without using the queue worker.

[2024-12-08 07:58:08] local.INFO: Received a request on the root domain  
[2024-12-08 07:58:12] local.INFO: Running Process Image Data Job on the queue