Skip to content

Commit

Permalink
Merge pull request #205 from BlueTM/hotfix/bug-fix-signals
Browse files Browse the repository at this point in the history
Fix bug: task was ending when signal was received not after was evaluated
  • Loading branch information
mihaileu authored Oct 19, 2023
2 parents 53f21eb + f109b11 commit 6e117e9
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Command/GearmanJobExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->gearmanExecute->addSystemSignalListener();

/**@var QuestionHelper $question*/
$question = $this->getHelper('question');

Expand Down
2 changes: 2 additions & 0 deletions Command/GearmanWorkerExecuteCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ protected function configure()

protected function execute(InputInterface $input, OutputInterface $output)
{
$this->gearmanExecute->addSystemSignalListener();

/**
* @var QuestionHelper $question
*/
Expand Down
2 changes: 1 addition & 1 deletion Service/Abstracts/AbstractGearmanService.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $def
$this->workers = $gearmanCacheWrapper->getWorkers();

if (isset($defaultSettings['job_prefix'])) {
$this->jobPrefix = $defaultSettings['job_prefix']??null;
$this->jobPrefix = $defaultSettings['job_prefix'];
}
}

Expand Down
23 changes: 13 additions & 10 deletions Service/GearmanExecute.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,22 +90,25 @@ public function __construct(GearmanCacheWrapper $gearmanCacheWrapper, array $def


$this->stopWorkSignalReceived = false;
}

/**
* If the pcntl_signal exists, subscribe to the terminate and restart events for graceful worker stops.
*/
if (false !== function_exists('pcntl_signal')) {
declare(ticks=1);
pcntl_signal(SIGTERM, [$this,"handleSystemSignal"]);
pcntl_signal(SIGHUP, [$this,"handleSystemSignal"]);
public function addSystemSignalListener(): void
{
if (extension_loaded('pcntl')) {
pcntl_async_signals(true);

pcntl_signal(SIGTERM, [$this, 'handleSystemSignal']);
pcntl_signal(SIGINT, [$this, 'handleSystemSignal']);
pcntl_signal(SIGHUP, [$this, 'handleSystemSignal']);

pcntl_signal_dispatch();
}
}

/**
* Toggles that work should be stopped, we only subscribe to SIGTERM and SIGHUP
* @param int $signo Signal number
* Toggles that work should be stopped, we only subscribe to SIGTERM,SIGINT and SIGHUP
*/
public function handleSystemSignal($signo)
public function handleSystemSignal(): void
{
$this->stopWorkSignalReceived = true;
}
Expand Down

0 comments on commit 6e117e9

Please sign in to comment.