From 8c90e7fe31bee11bd496050a807c790da12cc519 Mon Sep 17 00:00:00 2001 From: Taylor Otwell Date: Mon, 6 Feb 2017 16:45:40 -0600 Subject: [PATCH] Clean up how events are dispatched to not use old style jobs. This maintains backwards compatibility by keeping around the old job class until the 5.5 release. --- src/Illuminate/Events/CallQueuedListener.php | 101 +++++++++++++++++++ src/Illuminate/Events/Dispatcher.php | 12 +-- src/Illuminate/Queue/Jobs/JobName.php | 4 + src/Illuminate/Queue/Queue.php | 2 + tests/Events/EventsDispatcherTest.php | 6 +- 5 files changed, 112 insertions(+), 13 deletions(-) create mode 100644 src/Illuminate/Events/CallQueuedListener.php diff --git a/src/Illuminate/Events/CallQueuedListener.php b/src/Illuminate/Events/CallQueuedListener.php new file mode 100644 index 000000000000..7786722c0ff4 --- /dev/null +++ b/src/Illuminate/Events/CallQueuedListener.php @@ -0,0 +1,101 @@ +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); + } + } +} diff --git a/src/Illuminate/Events/Dispatcher.php b/src/Illuminate/Events/Dispatcher.php index 1ff100ef4dab..cc304e6e44b0 100755 --- a/src/Illuminate/Events/Dispatcher.php +++ b/src/Illuminate/Events/Dispatcher.php @@ -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))); } /** diff --git a/src/Illuminate/Queue/Jobs/JobName.php b/src/Illuminate/Queue/Jobs/JobName.php index 92803e271d62..3d2cb079d381 100644 --- a/src/Illuminate/Queue/Jobs/JobName.php +++ b/src/Illuminate/Queue/Jobs/JobName.php @@ -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); } diff --git a/src/Illuminate/Queue/Queue.php b/src/Illuminate/Queue/Queue.php index 52344bf7e906..b1aefee99d51 100755 --- a/src/Illuminate/Queue/Queue.php +++ b/src/Illuminate/Queue/Queue.php @@ -3,6 +3,7 @@ namespace Illuminate\Queue; use Illuminate\Container\Container; +use Illuminate\Events\CallQueuedListener; abstract class Queue { @@ -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), diff --git a/tests/Events/EventsDispatcherTest.php b/tests/Events/EventsDispatcherTest.php index 354a18f686e7..3d42e8a2e731 100755 --- a/tests/Events/EventsDispatcherTest.php +++ b/tests/Events/EventsDispatcherTest.php @@ -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;