Skip to content

EspoCRM – Open Source CRM Application

Daemon graceful exit

Yuriicd0db0e0e408
diff --git a/application/Espo/Core/ApplicationRunners/Daemon.php b/application/Espo/Core/ApplicationRunners/Daemon.php
index 03fc2b0828..b5d40893ba 100644
--- a/application/Espo/Core/ApplicationRunners/Daemon.php
+++ b/application/Espo/Core/ApplicationRunners/Daemon.php
@@ -30,6 +30,7 @@
namespace Espo\Core\ApplicationRunners;
use Espo\Core\Application\Runner;
+use Espo\Core\Job\Processing\Util\ExitSetup;
use Espo\Core\Utils\Config;
use Espo\Core\Utils\Log;
@@ -43,8 +44,13 @@ class Daemon implements Runner
{
use Cli;
- public function __construct(private Config $config, private Log $log)
- {}
+ private bool $stopped = false;
+
+ public function __construct(
+ private Config $config,
+ private Log $log,
+ private ExitSetup $exitSetup,
+ ) {}
public function run(): void
{
@@ -64,9 +70,13 @@ class Daemon implements Runner
return;
}
+ $this->exitSetup->setup(function () {
+ $this->stopped = true;
+ });
+
$processList = [];
- while (true) { /** @phpstan-ignore-line */
+ while (!$this->isStopped()) { /** @phpstan-ignore-line */
$toSkip = false;
$runningCount = 0;
@@ -94,7 +104,20 @@ class Daemon implements Runner
$processList[] = $process;
}
+ /** @noinspection PhpConditionAlreadyCheckedInspection */
+ if ($this->isStopped()) {
+ break;
+ }
+
sleep($interval);
}
}
+
+ /**
+ * @phpstan-impure
+ */
+ private function isStopped(): bool
+ {
+ return $this->stopped;
+ }
}