Skip to content

Commit

Permalink
Clean up how events are dispatched to not use old style jobs.
Browse files Browse the repository at this point in the history
This maintains backwards compatibility by keeping around the old job
class until the 5.5 release.
  • Loading branch information
taylorotwell committed Feb 6, 2017
1 parent d1a0842 commit 8c90e7f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 13 deletions.
101 changes: 101 additions & 0 deletions src/Illuminate/Events/CallQueuedListener.php
@@ -0,0 +1,101 @@
<?php

namespace Illuminate\Events;

use Illuminate\Container\Container;
use Illuminate\Contracts\Queue\Job;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class CallQueuedListener implements ShouldQueue
{
use InteractsWithQueue;

/**
* The listener class name.
*
* @var string
*/
public $class;

/**
* The listener method.
*
* @var string
*/
public $method;

/**
* The data to be passed to the listener.
*
* @var string
*/
public $data;

/**
* Create a new job instance.
*
* @param string $class
* @param string $method
* @param string $data
* @return void
*/
public function __construct($class, $method, $data)
{
$this->data = $data;
$this->class = $class;
$this->method = $method;
}

/**
* Handle the queued job.
*
* @param \Illuminate\Container\Container $container
* @return void
*/
public function handle(Container $container)
{
$handler = $this->setJobInstanceIfNecessary(
$this->job, $container->make($this->class)
);

call_user_func_array(
[$handler, $this->method], unserialize($this->data)
);
}

/**
* Set the job instance of the given class if necessary.
*
* @param \Illuminate\Contracts\Queue\Job $job
* @param mixed $instance
* @return mixed
*/
protected function setJobInstanceIfNecessary(Job $job, $instance)
{
if (in_array(InteractsWithQueue::class, class_uses_recursive(get_class($instance)))) {
$instance->setJob($job);
}

return $instance;
}

/**
* Call the failed method on the job instance.
*
* The event instance and the exception will be passed.
*
* @param \Exception $e
* @return void
*/
public function failed($e)
{
$handler = Container::getInstance()->make($this->class);

$parameters = array_merge(unserialize($this->data), [$e]);

if (method_exists($handler, 'failed')) {
call_user_func_array([$handler, 'failed'], $parameters);
}
}
}
12 changes: 4 additions & 8 deletions src/Illuminate/Events/Dispatcher.php
Expand Up @@ -450,19 +450,15 @@ protected function callQueueMethodOnHandler($class, $method, $arguments)
*/
protected function queueHandler($class, $method, $arguments)
{
$handler = (new ReflectionClass($class))->newInstanceWithoutConstructor();
$listener = (new ReflectionClass($class))->newInstanceWithoutConstructor();

$connection = isset($handler->connection) ? $handler->connection : null;
$connection = isset($listener->connection) ? $listener->connection : null;

$queue = isset($handler->queue) ? $handler->queue : null;
$queue = isset($listener->queue) ? $listener->queue : null;

$this->resolveQueue()
->connection($connection)
->pushOn($queue, 'Illuminate\Events\CallQueuedHandler@call', [
'class' => $class,
'method' => $method,
'data' => serialize($arguments),
]);
->pushOn($queue, new CallQueuedListener($class, $method, serialize($arguments)));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/Illuminate/Queue/Jobs/JobName.php
Expand Up @@ -27,6 +27,10 @@ public static function parse($job)
*/
public static function resolve($name, $payload)
{
if (isset($payload['name'])) {
return $payload['name'];
}

if ($name === 'Illuminate\Queue\CallQueuedHandler@call') {
return Arr::get($payload, 'data.commandName', $name);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Queue/Queue.php
Expand Up @@ -3,6 +3,7 @@
namespace Illuminate\Queue;

use Illuminate\Container\Container;
use Illuminate\Events\CallQueuedListener;

abstract class Queue
{
Expand Down Expand Up @@ -118,6 +119,7 @@ protected function createObjectPayload($job)
return [
'job' => 'Illuminate\Queue\CallQueuedHandler@call',
'maxTries' => isset($job->tries) ? $job->tries : null,
'name' => $job instanceof CallQueuedListener ? $job->class : '',
'timeout' => isset($job->timeout) ? $job->timeout : null,
'data' => [
'commandName' => get_class($job),
Expand Down
6 changes: 1 addition & 5 deletions tests/Events/EventsDispatcherTest.php
Expand Up @@ -173,11 +173,7 @@ public function testQueuedEventHandlersAreQueued()

$queue->shouldReceive('connection')->once()->with(null)->andReturnSelf();

$queue->shouldReceive('pushOn')->once()->with(null, 'Illuminate\Events\CallQueuedHandler@call', [
'class' => 'Illuminate\Tests\Events\TestDispatcherQueuedHandler',
'method' => 'someMethod',
'data' => serialize(['foo', 'bar']),
]);
$queue->shouldReceive('pushOn')->once()->with(null, m::type('Illuminate\Events\CallQueuedListener'));

$d->setQueueResolver(function () use ($queue) {
return $queue;
Expand Down

0 comments on commit 8c90e7f

Please sign in to comment.