mirror of
https://github.com/Icinga/icingaweb2.git
synced 2025-07-21 04:44:25 +02:00
Introduce class Icinga\Application\Daemon
This commit is contained in:
parent
74022ae4e0
commit
cec69f63a4
113
library/Icinga/Application/Daemon.php
Normal file
113
library/Icinga/Application/Daemon.php
Normal file
@ -0,0 +1,113 @@
|
|||||||
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
|
namespace Icinga\Application;
|
||||||
|
|
||||||
|
use Icinga\Application\Daemon\DaemonJob;
|
||||||
|
use LogicException;
|
||||||
|
use React\EventLoop\Loop;
|
||||||
|
use React\EventLoop\LoopInterface;
|
||||||
|
|
||||||
|
class Daemon
|
||||||
|
{
|
||||||
|
/** @var LoopInterface */
|
||||||
|
protected $loop;
|
||||||
|
|
||||||
|
/** @var bool */
|
||||||
|
protected $shuttingDown = false;
|
||||||
|
|
||||||
|
/** @var DaemonJob[] */
|
||||||
|
protected $jobs = [];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new Daemon
|
||||||
|
*
|
||||||
|
* @param LoopInterface|null $loop If not given, the default loop is used
|
||||||
|
*/
|
||||||
|
public function __construct(LoopInterface $loop = null)
|
||||||
|
{
|
||||||
|
$this->loop = $loop ?? Loop::get();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Initialize the daemon
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function initialize(): void
|
||||||
|
{
|
||||||
|
$this->registerSignals();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Register signals
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function registerSignals(): void
|
||||||
|
{
|
||||||
|
$this->loop->addSignal(SIGTERM, [$this, 'shutdown']);
|
||||||
|
$this->loop->addSignal(SIGINT, [$this, 'shutdown']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Deregister signals
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function deregisterSignals(): void
|
||||||
|
{
|
||||||
|
$this->loop->removeSignal(SIGTERM, [$this, 'shutdown']);
|
||||||
|
$this->loop->removeSignal(SIGINT, [$this, 'shutdown']);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Launch the daemon
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function run(): void
|
||||||
|
{
|
||||||
|
if (empty($this->jobs)) {
|
||||||
|
throw new LogicException('No jobs added');
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->loop->futureTick(function () {
|
||||||
|
$this->initialize();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop the daemon
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function shutdown(): void
|
||||||
|
{
|
||||||
|
if ($this->shuttingDown) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->shuttingDown = true;
|
||||||
|
$this->deregisterSignals();
|
||||||
|
|
||||||
|
foreach ($this->jobs as $job) {
|
||||||
|
$job->cancel();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a job to this daemon
|
||||||
|
*
|
||||||
|
* @param DaemonJob $job
|
||||||
|
*
|
||||||
|
* @return $this
|
||||||
|
*/
|
||||||
|
public function addJob(DaemonJob $job): self
|
||||||
|
{
|
||||||
|
$this->jobs[] = $job;
|
||||||
|
$job->attach($this->loop);
|
||||||
|
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
}
|
27
library/Icinga/Application/Daemon/DaemonJob.php
Normal file
27
library/Icinga/Application/Daemon/DaemonJob.php
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
/* Icinga Web 2 | (c) 2022 Icinga GmbH | GPLv2+ */
|
||||||
|
|
||||||
|
namespace Icinga\Application\Daemon;
|
||||||
|
|
||||||
|
use React\EventLoop\LoopInterface;
|
||||||
|
|
||||||
|
interface DaemonJob
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Attach this job to the given event loop
|
||||||
|
*
|
||||||
|
* @param LoopInterface $loop
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function attach(LoopInterface $loop): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Stop processing of this job
|
||||||
|
*
|
||||||
|
* Cancelling pending runs of the job is optional.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function cancel(): void;
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user