//If we start service each minute we should eliminate exrtra messages like "Another process already running..."
$oPid->pidExistLevel = 'WARN';
$oPid->setLogger($logger)->create();
$interrupt = 0;
//Signal handler callback function
$sigHandler = function ($signo = null) use(&$interrupt, $oPid, $logger) {
static $once = 0;
$interrupt++;
if ($once++) {
return;
}
//Reporting about termination
$logger->log("SERVICE", "Service recieved termination SIGNAL:%d", intval($signo));
//We should stop all running tasks.
//It's enough to terminate all running clients.
Launcher::terminateClients();
//Without this workers may not be terminated
sleep(1);
//Terminating an MDP broker, not sure we have to do so.
Launcher::terminateBroker();
//Removing pid file
$oPid->remove();
};
pcntl_signal(SIGINT, $sigHandler);
pcntl_signal(SIGTERM, $sigHandler);
pcntl_signal(SIGHUP, $sigHandler);
register_shutdown_function($sigHandler);
$previous = new DateTime('-5 minutes');
$logger->log("SERVICE", "Starting service");
while (!$interrupt) {
// System tymezone is considered to be used here