Skip to content

Commit 491ebd0

Browse files
[9.x] Add WorkOptions to WorkerStopping Event (#45120)
* [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 <[email protected]>
1 parent 15e1a54 commit 491ebd0

File tree

3 files changed

+31
-29
lines changed

3 files changed

+31
-29
lines changed

src/Illuminate/Queue/Events/WorkerStopping.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,34 @@
22

33
namespace Illuminate\Queue\Events;
44

5+
use Illuminate\Queue\WorkerOptions;
6+
57
class WorkerStopping
68
{
79
/**
8-
* The exit status.
10+
* The worker exit status.
911
*
1012
* @var int
1113
*/
1214
public $status;
1315

16+
/**
17+
* The worker options.
18+
*
19+
* @var \Illuminate\Queue\WorkerOptions|null
20+
*/
21+
public $workerOptions;
22+
1423
/**
1524
* Create a new event instance.
1625
*
1726
* @param int $status
27+
* @param \Illuminate\Queue\WorkerOptions|null $workerOptions
1828
* @return void
1929
*/
20-
public function __construct($status = 0)
30+
public function __construct($status = 0, $workerOptions = null)
2131
{
2232
$this->status = $status;
33+
$this->workerOptions = $workerOptions;
2334
}
2435
}

src/Illuminate/Queue/Worker.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public function daemon($connectionName, $queue, WorkerOptions $options)
143143
$status = $this->pauseWorker($options, $lastRestart);
144144

145145
if (! is_null($status)) {
146-
return $this->stop($status);
146+
return $this->stop($status, $options);
147147
}
148148

149149
continue;
@@ -191,7 +191,7 @@ public function daemon($connectionName, $queue, WorkerOptions $options)
191191
);
192192

193193
if (! is_null($status)) {
194-
return $this->stop($status);
194+
return $this->stop($status, $options);
195195
}
196196
}
197197
}
@@ -223,7 +223,7 @@ protected function registerTimeoutHandler($job, WorkerOptions $options)
223223
);
224224
}
225225

226-
$this->kill(static::EXIT_ERROR);
226+
$this->kill(static::EXIT_ERROR, $options);
227227
});
228228

229229
pcntl_alarm(
@@ -579,7 +579,7 @@ protected function markJobAsFailedIfItShouldFailOnTimeout($connectionName, $job,
579579
*/
580580
protected function failJob($job, Throwable $e)
581581
{
582-
return $job->fail($e);
582+
$job->fail($e);
583583
}
584584

585585
/**
@@ -676,21 +676,10 @@ protected function listenForSignals()
676676
{
677677
pcntl_async_signals(true);
678678

679-
pcntl_signal(SIGQUIT, function () {
680-
$this->shouldQuit = true;
681-
});
682-
683-
pcntl_signal(SIGTERM, function () {
684-
$this->shouldQuit = true;
685-
});
686-
687-
pcntl_signal(SIGUSR2, function () {
688-
$this->paused = true;
689-
});
690-
691-
pcntl_signal(SIGCONT, function () {
692-
$this->paused = false;
693-
});
679+
pcntl_signal(SIGQUIT, fn () => $this->shouldQuit = true);
680+
pcntl_signal(SIGTERM, fn () => $this->shouldQuit = true);
681+
pcntl_signal(SIGUSR2, fn () => $this->paused = true);
682+
pcntl_signal(SIGCONT, fn () => $this->paused = false);
694683
}
695684

696685
/**
@@ -718,11 +707,12 @@ public function memoryExceeded($memoryLimit)
718707
* Stop listening and bail out of the script.
719708
*
720709
* @param int $status
710+
* @param WorkerOptions|null $options
721711
* @return int
722712
*/
723-
public function stop($status = 0)
713+
public function stop($status = 0, $options = null)
724714
{
725-
$this->events->dispatch(new WorkerStopping($status));
715+
$this->events->dispatch(new WorkerStopping($status, $options));
726716

727717
return $status;
728718
}
@@ -731,11 +721,12 @@ public function stop($status = 0)
731721
* Kill the process.
732722
*
733723
* @param int $status
724+
* @param \Illuminate\Queue\WorkerOptions|null $options
734725
* @return never
735726
*/
736-
public function kill($status = 0)
727+
public function kill($status = 0, $options = null)
737728
{
738-
$this->events->dispatch(new WorkerStopping($status));
729+
$this->events->dispatch(new WorkerStopping($status, $options));
739730

740731
if (extension_loaded('posix')) {
741732
posix_kill(getmypid(), SIGKILL);
@@ -817,7 +808,7 @@ public static function popUsing($workerName, $callback)
817808
/**
818809
* Get the queue manager instance.
819810
*
820-
* @return \Illuminate\Queue\QueueManager
811+
* @return \Illuminate\Contracts\Queue\Factory
821812
*/
822813
public function getManager()
823814
{

tests/Queue/QueueWorkerTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ protected function tearDown(): void
4040
{
4141
parent::tearDown();
4242

43-
Carbon::setTestNow(null);
43+
Carbon::setTestNow();
4444

45-
Container::setInstance(null);
45+
Container::setInstance();
4646
}
4747

4848
public function testJobCanBeFired()
@@ -411,7 +411,7 @@ public function sleep($seconds)
411411
$this->sleptFor = $seconds;
412412
}
413413

414-
public function stop($status = 0)
414+
public function stop($status = 0, $options = null)
415415
{
416416
return $status;
417417
}

0 commit comments

Comments
 (0)