Skip to content

Commit

Permalink
Fixed bad dependency assumptions
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamCampbell committed Mar 10, 2020
1 parent 367c2c8 commit 41ac503
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 4 deletions.
48 changes: 44 additions & 4 deletions src/Illuminate/Console/Scheduling/Schedule.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
use DateTimeInterface;
use Illuminate\Console\Application;
use Illuminate\Container\Container;
use Illuminate\Contracts\Bus\Dispatcher;
use Illuminate\Contracts\Container\BindingResolutionException;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\ProcessUtils;
use Illuminate\Support\Traits\Macroable;
use RuntimeException;

class Schedule
{
Expand All @@ -34,6 +37,13 @@ class Schedule
*/
protected $schedulingMutex;

/**
* The job dispatcher implementation.
*
* @var \Illuminate\Contracts\Bus\Dispatcher
*/
protected $dispatcher;

/**
* The timezone the date should be evaluated on.
*
Expand All @@ -51,6 +61,12 @@ public function __construct($timezone = null)
{
$this->timezone = $timezone;

if (! class_exists(Container::class)) {
throw new RuntimeException(
'The container implementation is required to use the scheduler. Please install illuminate/container.'
);
}

$container = Container::getInstance();

$this->eventMutex = $container->bound(EventMutex::class)
Expand Down Expand Up @@ -110,11 +126,13 @@ public function job($job, $queue = null, $connection = null)
$job = is_string($job) ? resolve($job) : $job;

if ($job instanceof ShouldQueue) {
dispatch($job)
->onConnection($connection ?? $job->connection)
->onQueue($queue ?? $job->queue);
$pending = $this->getDispatcher()->dispatch($job);
if ($pending !== null) {
$pending->onConnection($connection ?? $job->connection)
->onQueue($queue ?? $job->queue);
}
} else {
dispatch_now($job);
$this->getDispatcher()->dispatchNow($job);
}
})->name(is_string($job) ? $job : get_class($job));
}
Expand Down Expand Up @@ -209,4 +227,26 @@ public function useCache($store)

return $this;
}

/**
* Get the job dispatcher, if available.
*
* @return \Illuminate\Contracts\Bus\Dispatcher
*/
protected function getDispatcher()
{
if ($this->dispatcher === null) {
try {
$this->dispatcher = Container::getInstance()->make(Dispatcher::class);
} catch (BindingResolutionException $e) {
throw new RuntimeException(
'Unable to resolve the dispatcher from the service container. Please bind it or install illuminate/bus.',
$e->getCode(),
$e
);
}
}

return $this->dispatcher;
}
}
2 changes: 2 additions & 0 deletions src/Illuminate/Console/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"suggest": {
"dragonmantank/cron-expression": "Required to use scheduling component (^2.0).",
"guzzlehttp/guzzle": "Required to use the ping methods on schedules (^6.0|^7.0).",
"illuminate/bus": "Required to use the scheduled job dispatcher (^6.0)",
"illuminate/container": "Required to use the scheduler (^6.0)",
"illuminate/filesystem": "Required to use the generator command (^6.0)"
},
"config": {
Expand Down
3 changes: 3 additions & 0 deletions tests/Integration/Console/JobSchedulingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Tests\Integration\Console;

use Illuminate\Bus\Queueable;
use Illuminate\Container\Container;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
Expand All @@ -14,6 +15,7 @@ class JobSchedulingTest extends TestCase
public function testJobQueuingRespectsJobQueue()
{
Queue::fake();
Container::setInstance($this->app);

/** @var Schedule $scheduler */
$scheduler = $this->app->make(Schedule::class);
Expand Down Expand Up @@ -42,6 +44,7 @@ public function testJobQueuingRespectsJobQueue()
public function testJobQueuingRespectsJobConnection()
{
Queue::fake();
Container::setInstance($this->app);

/** @var Schedule $scheduler */
$scheduler = $this->app->make(Schedule::class);
Expand Down

0 comments on commit 41ac503

Please sign in to comment.