diff --git a/src/Illuminate/Queue/Events/WorkerStarting.php b/src/Illuminate/Queue/Events/WorkerStarting.php new file mode 100644 index 000000000000..89ada14873c0 --- /dev/null +++ b/src/Illuminate/Queue/Events/WorkerStarting.php @@ -0,0 +1,20 @@ +app['events']->listen(Events\JobFailed::class, $callback); } + /** + * Register an event listener for the daemon queue starting. + * + * @param mixed $callback + * @return void + */ + public function starting($callback) + { + $this->app['events']->listen(Events\WorkerStarting::class, $callback); + } + /** * Register an event listener for the daemon queue stopping. * diff --git a/src/Illuminate/Queue/Worker.php b/src/Illuminate/Queue/Worker.php index 6ac69dcf5ec8..83df07eac618 100644 --- a/src/Illuminate/Queue/Worker.php +++ b/src/Illuminate/Queue/Worker.php @@ -16,6 +16,7 @@ use Illuminate\Queue\Events\JobReleasedAfterException; use Illuminate\Queue\Events\JobTimedOut; use Illuminate\Queue\Events\Looping; +use Illuminate\Queue\Events\WorkerStarting; use Illuminate\Queue\Events\WorkerStopping; use Illuminate\Support\Carbon; use Throwable; @@ -139,6 +140,8 @@ public function daemon($connectionName, $queue, WorkerOptions $options) [$startTime, $jobsProcessed] = [hrtime(true) / 1e9, 0]; + $this->raiseWorkerStartingEvent($connectionName, $queue, $options); + while (true) { // Before reserving any jobs, we will make sure this queue is not paused and // if it is we will just pause this worker for a given amount of time and @@ -624,7 +627,20 @@ protected function calculateBackoff($job, WorkerOptions $options) } /** - * Raise the before job has been popped. + * Raise an event indicating the worker is starting. + * + * @param string $connectionName + * @param string $queue + * @param \Illuminate\Queue\WorkerOptions $options + * @return void + */ + protected function raiseWorkerStartingEvent($connectionName, $queue, $options) + { + $this->events->dispatch(new WorkerStarting($connectionName, $queue, $options)); + } + + /** + * Raise an event indicating a job is being popped from the queue. * * @param string $connectionName * @return void @@ -635,7 +651,7 @@ protected function raiseBeforeJobPopEvent($connectionName) } /** - * Raise the after job has been popped. + * Raise an event indicating a job has been popped from the queue. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job|null $job @@ -649,7 +665,7 @@ protected function raiseAfterJobPopEvent($connectionName, $job) } /** - * Raise the before queue job event. + * Raise an event indicating a job is being processed. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job @@ -663,7 +679,7 @@ protected function raiseBeforeJobEvent($connectionName, $job) } /** - * Raise the after queue job event. + * Raise an event indicating a job has been processed. * * @param string $connectionName * @param \Illuminate\Contracts\Queue\Job $job diff --git a/tests/Queue/QueueWorkerTest.php b/tests/Queue/QueueWorkerTest.php index a9eebdeae4b4..afbb7a2c4738 100755 --- a/tests/Queue/QueueWorkerTest.php +++ b/tests/Queue/QueueWorkerTest.php @@ -12,6 +12,7 @@ use Illuminate\Queue\Events\JobPopping; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; +use Illuminate\Queue\Events\WorkerStarting; use Illuminate\Queue\MaxAttemptsExceededException; use Illuminate\Queue\QueueManager; use Illuminate\Queue\Worker; @@ -386,6 +387,24 @@ public function testWorkerPicksJobUsingCustomCallbacks() Worker::popUsing('myworker', null); } + public function testWorkerStartingIsDispatched() + { + $workerOptions = new WorkerOptions(); + $workerOptions->stopWhenEmpty = true; + + $worker = $this->getWorker('default', ['queue' => [ + $firstJob = new WorkerFakeJob(), + $secondJob = new WorkerFakeJob(), + ]]); + + $worker->daemon('default', 'queue', $workerOptions); + + $this->assertTrue($firstJob->fired); + $this->assertTrue($secondJob->fired); + + $this->events->shouldHaveReceived('dispatch')->with(m::type(WorkerStarting::class))->once(); + } + /** * Helpers... */