Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Enable dispatchAfterResponse for batch 🔥 #41787

Conversation

civilcoder55
Copy link
Contributor

Before

Batch can't be dispatched after the response is sent to the user.

After

Batch can be dispatched and all jobs will be saved to storage after response.

Use Case

  • You need to dispatch a large number of jobs that takes a lot of time to insert into the database without blocking the current request.
  • You don't need your worker to process jobs until the response is finished.

Changes

  • added dispatchAfterResponse method to:
  1. store batch itself into database
  2. register a terminating callback to dispatch batch jobs
  3. return created batch
  • added private dispatchAlreadyCreated method to:
  1. dispatch batch jobs and fire BatchDispatched event like before but without creating
    batch because we already created it in dispatchAfterResponse

Example

use App\Jobs\ImportCsv;
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
use Throwable;
 
$batch = Bus::batch([
    new ImportCsv(1, 100),
    new ImportCsv(101, 200),
    new ImportCsv(201, 300),
    new ImportCsv(301, 400),
    new ImportCsv(401, 500),
])->then(function (Batch $batch) {
    // All jobs completed successfully...
})->catch(function (Batch $batch, Throwable $e) {
    // First batch job failure detected...
})->finally(function (Batch $batch) {
    // The batch has finished executing...
})->dispatchAfterResponse();  //Jobs will be dispatched after response sent to user
 
return $batch->id; //also it returns batch object so you can access batch id 

civilcoder55 and others added 2 commits April 2, 2022 01:39
[before]
batch can't be dispatched after response sent to user.

[after]
batch can be dipatched and all jobs will be saved to storage after response.

- added dispatchAfterResponse method to:
1- store batch itself into database
2- register a terminating callback to dispatch batch jobs
3- return created batch

- added private dispatchAlreadyCreated method to:
1- dispatch batch jobs and fire BatchDispatched event like before but without creating
batch because we already created it in dispatchAfterResponse
@taylorotwell taylorotwell merged commit a3f49a6 into laravel:9.x Apr 4, 2022
chu121su12 pushed a commit to chu121su12/framework that referenced this pull request Apr 5, 2022
* [9.x] Enable dispatchAfterResponse for batch 🔥

[before]
batch can't be dispatched after response sent to user.

[after]
batch can be dipatched and all jobs will be saved to storage after response.

- added dispatchAfterResponse method to:
1- store batch itself into database
2- register a terminating callback to dispatch batch jobs
3- return created batch

- added private dispatchAlreadyCreated method to:
1- dispatch batch jobs and fire BatchDispatched event like before but without creating
batch because we already created it in dispatchAfterResponse

* formatting

Co-authored-by: Taylor Otwell <taylor@laravel.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants