Skip to content

Commit

Permalink
Adding chain able jobs.
Browse files Browse the repository at this point in the history
This adds a simple mechanism to dispatch a new job if the previous job
is successful.
  • Loading branch information
taylorotwell committed Apr 1, 2017
1 parent c8a9413 commit 81bcb03
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 1 deletion.
34 changes: 34 additions & 0 deletions src/Illuminate/Bus/Queueable.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,13 @@ trait Queueable
*/
public $delay;

/**
* The jobs that should run if this job is successful.
*
* @var array
*/
public $chained = [];

/**
* Set the desired connection for the job.
*
Expand Down Expand Up @@ -63,4 +70,31 @@ public function delay($delay)

return $this;
}

/**
* Set the jobs that should run if this job is successful.
*
* @param array $chain
* @return $this
*/
public function then($chain)
{
$this->chained = collect($chain)->map(function ($job) {
return serialize($job);
})->all();

return $this;
}

/**
* Dispatch the next job on the chain.
*
* @return void
*/
public function dispatchNextJobInChain()
{
if (! empty($this->chained)) {
dispatch(unserialize(array_shift($this->chained))->then($this->chained));
}
}
}
13 changes: 13 additions & 0 deletions src/Illuminate/Foundation/Bus/PendingDispatch.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ public function delay($delay)
return $this;
}

/**
* Set the jobs that should run if this job is successful.
*
* @param array $chain
* @return $this
*/
public function then($chain)
{
$this->job->then($chain);

return $this;
}

/**
* Handle the object's destruction.
*
Expand Down
15 changes: 15 additions & 0 deletions src/Illuminate/Queue/CallQueuedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public function call(Job $job, array $data)
);

if (! $job->isDeletedOrReleased()) {
$this->ensureNextJobIsChainIsDispatched($command);

$job->delete();
}
}
Expand Down Expand Up @@ -81,6 +83,19 @@ protected function setJobInstanceIfNecessary(Job $job, $instance)
return $instance;
}

/**
* Ensure the next job in the chain is dispatched if applicable.
*
* @param mixed $command
* @return void
*/
protected function ensureNextJobIsChainIsDispatched($command)
{
if (method_exists($command, 'dispatchNextJobInChain')) {
$command->dispatchNextJobInChain();
}
}

/**
* Call the failed method on the job instance.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/Integration/IntegrationTest.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<?php

use Orchestra\Testbench\TestCase;
use Illuminate\Support\Facades\Route;

/**
* @group integration
*/
class IntegrationTest extends Orchestra\Testbench\TestCase
class IntegrationTest extends TestCase
{
public function test_simple_route_through_the_framework()
{
Expand Down
55 changes: 55 additions & 0 deletions tests/Integration/Queue/JobChainingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

use Illuminate\Bus\Queueable;
use Orchestra\Testbench\TestCase;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Contracts\Queue\ShouldQueue;

/**
* @group integration
*/
class JobChainingTest extends TestCase
{
public function tearDown()
{
JobChainingTestFirstJob::$ran = false;
JobChainingTestSecondJob::$ran = false;
}


public function test_jobs_can_be_chained_on_success()
{
JobChainingTestFirstJob::dispatch()->then([
new JobChainingTestSecondJob
]);

$this->assertTrue(JobChainingTestFirstJob::$ran);
$this->assertTrue(JobChainingTestSecondJob::$ran);
}
}


class JobChainingTestFirstJob implements ShouldQueue
{
use Dispatchable, Queueable;

public static $ran = false;

public function handle()
{
static::$ran = true;
}
}


class JobChainingTestSecondJob implements ShouldQueue
{
use Dispatchable, Queueable;

public static $ran = false;

public function handle()
{
static::$ran = true;
}
}

0 comments on commit 81bcb03

Please sign in to comment.