Skip to content

Commit

Permalink
[9.x] Add WorkOptions to WorkerStopping Event (#45120)
Browse files Browse the repository at this point in the history
* [9.x] Add WorkOptions to WorkerStopping Event

* chore: lint

* chore: update

* chore: lint

* Update WorkerStopping.php

* Update WorkerStopping.php

* Update WorkerStopping.php

* Update Worker.php

Co-authored-by: Taylor Otwell <taylor@laravel.com>
  • Loading branch information
dammy001 and taylorotwell authored Nov 28, 2022
1 parent 15e1a54 commit 491ebd0
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
15 changes: 13 additions & 2 deletions src/Illuminate/Queue/Events/WorkerStopping.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,34 @@

namespace Illuminate\Queue\Events;

use Illuminate\Queue\WorkerOptions;

class WorkerStopping
{
/**
* The exit status.
* The worker exit status.
*
* @var int
*/
public $status;

/**
* The worker options.
*
* @var \Illuminate\Queue\WorkerOptions|null
*/
public $workerOptions;

/**
* Create a new event instance.
*
* @param int $status
* @param \Illuminate\Queue\WorkerOptions|null $workerOptions
* @return void
*/
public function __construct($status = 0)
public function __construct($status = 0, $workerOptions = null)
{
$this->status = $status;
$this->workerOptions = $workerOptions;
}
}
39 changes: 15 additions & 24 deletions src/Illuminate/Queue/Worker.php
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public function daemon($connectionName, $queue, WorkerOptions $options)
$status = $this->pauseWorker($options, $lastRestart);

if (! is_null($status)) {
return $this->stop($status);
return $this->stop($status, $options);
}

continue;
Expand Down Expand Up @@ -191,7 +191,7 @@ public function daemon($connectionName, $queue, WorkerOptions $options)
);

if (! is_null($status)) {
return $this->stop($status);
return $this->stop($status, $options);
}
}
}
Expand Down Expand Up @@ -223,7 +223,7 @@ protected function registerTimeoutHandler($job, WorkerOptions $options)
);
}

$this->kill(static::EXIT_ERROR);
$this->kill(static::EXIT_ERROR, $options);
});

pcntl_alarm(
Expand Down Expand Up @@ -579,7 +579,7 @@ protected function markJobAsFailedIfItShouldFailOnTimeout($connectionName, $job,
*/
protected function failJob($job, Throwable $e)
{
return $job->fail($e);
$job->fail($e);
}

/**
Expand Down Expand Up @@ -676,21 +676,10 @@ protected function listenForSignals()
{
pcntl_async_signals(true);

pcntl_signal(SIGQUIT, function () {
$this->shouldQuit = true;
});

pcntl_signal(SIGTERM, function () {
$this->shouldQuit = true;
});

pcntl_signal(SIGUSR2, function () {
$this->paused = true;
});

pcntl_signal(SIGCONT, function () {
$this->paused = false;
});
pcntl_signal(SIGQUIT, fn () => $this->shouldQuit = true);
pcntl_signal(SIGTERM, fn () => $this->shouldQuit = true);
pcntl_signal(SIGUSR2, fn () => $this->paused = true);
pcntl_signal(SIGCONT, fn () => $this->paused = false);
}

/**
Expand Down Expand Up @@ -718,11 +707,12 @@ public function memoryExceeded($memoryLimit)
* Stop listening and bail out of the script.
*
* @param int $status
* @param WorkerOptions|null $options
* @return int
*/
public function stop($status = 0)
public function stop($status = 0, $options = null)
{
$this->events->dispatch(new WorkerStopping($status));
$this->events->dispatch(new WorkerStopping($status, $options));

return $status;
}
Expand All @@ -731,11 +721,12 @@ public function stop($status = 0)
* Kill the process.
*
* @param int $status
* @param \Illuminate\Queue\WorkerOptions|null $options
* @return never
*/
public function kill($status = 0)
public function kill($status = 0, $options = null)
{
$this->events->dispatch(new WorkerStopping($status));
$this->events->dispatch(new WorkerStopping($status, $options));

if (extension_loaded('posix')) {
posix_kill(getmypid(), SIGKILL);
Expand Down Expand Up @@ -817,7 +808,7 @@ public static function popUsing($workerName, $callback)
/**
* Get the queue manager instance.
*
* @return \Illuminate\Queue\QueueManager
* @return \Illuminate\Contracts\Queue\Factory
*/
public function getManager()
{
Expand Down
6 changes: 3 additions & 3 deletions tests/Queue/QueueWorkerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ protected function tearDown(): void
{
parent::tearDown();

Carbon::setTestNow(null);
Carbon::setTestNow();

Container::setInstance(null);
Container::setInstance();
}

public function testJobCanBeFired()
Expand Down Expand Up @@ -411,7 +411,7 @@ public function sleep($seconds)
$this->sleptFor = $seconds;
}

public function stop($status = 0)
public function stop($status = 0, $options = null)
{
return $status;
}
Expand Down

0 comments on commit 491ebd0

Please sign in to comment.