Skip to content

Commit 6e117e9

Browse files
authored
Merge pull request #205 from BlueTM/hotfix/bug-fix-signals
Fix bug: task was ending when signal was received not after was evaluated
2 parents 53f21eb + f109b11 commit 6e117e9

File tree

4 files changed

+18
-11
lines changed

4 files changed

+18
-11
lines changed

Command/GearmanJobExecuteCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ protected function configure()
7979

8080
protected function execute(InputInterface $input, OutputInterface $output)
8181
{
82+
$this->gearmanExecute->addSystemSignalListener();
83+
8284
/**@var QuestionHelper $question*/
8385
$question = $this->getHelper('question');
8486

Command/GearmanWorkerExecuteCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ protected function configure()
8181

8282
protected function execute(InputInterface $input, OutputInterface $output)
8383
{
84+
$this->gearmanExecute->addSystemSignalListener();
85+
8486
/**
8587
* @var QuestionHelper $question
8688
*/

Service/Abstracts/AbstractGearmanService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $def
3939
$this->workers = $gearmanCacheWrapper->getWorkers();
4040

4141
if (isset($defaultSettings['job_prefix'])) {
42-
$this->jobPrefix = $defaultSettings['job_prefix']??null;
42+
$this->jobPrefix = $defaultSettings['job_prefix'];
4343
}
4444
}
4545

Service/GearmanExecute.php

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -90,22 +90,25 @@ public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $def
9090

9191

9292
$this->stopWorkSignalReceived = false;
93+
}
9394

94-
/**
95-
* If the pcntl_signal exists, subscribe to the terminate and restart events for graceful worker stops.
96-
*/
97-
if (false !== function_exists('pcntl_signal')) {
98-
declare(ticks=1);
99-
pcntl_signal(SIGTERM, [$this,"handleSystemSignal"]);
100-
pcntl_signal(SIGHUP, [$this,"handleSystemSignal"]);
95+
public function addSystemSignalListener(): void
96+
{
97+
if (extension_loaded('pcntl')) {
98+
pcntl_async_signals(true);
99+
100+
pcntl_signal(SIGTERM, [$this, 'handleSystemSignal']);
101+
pcntl_signal(SIGINT, [$this, 'handleSystemSignal']);
102+
pcntl_signal(SIGHUP, [$this, 'handleSystemSignal']);
103+
104+
pcntl_signal_dispatch();
101105
}
102106
}
103107

104108
/**
105-
* Toggles that work should be stopped, we only subscribe to SIGTERM and SIGHUP
106-
* @param int $signo Signal number
109+
* Toggles that work should be stopped, we only subscribe to SIGTERM,SIGINT and SIGHUP
107110
*/
108-
public function handleSystemSignal($signo)
111+
public function handleSystemSignal(): void
109112
{
110113
$this->stopWorkSignalReceived = true;
111114
}

0 commit comments

Comments
 (0)