Scalr\System\Zmq\Cron\PidFile::create PHP Méthode

create() public méthode

It performs pid file check itself
public create ( )
    public function create()
    {
        if ($this->check()) {
            //We are using warning here to eliminate messages flood in the service, nevertheless it's actually error.
            $this->log($this->pidExistLevel, "Cannot start service, another one is already running! pid:%d, file:%s", $this->pid, $this->file);
            exit;
        }
        //Creates pid file
        $this->pid = posix_getpid();
        $res = file_put_contents($this->file, $this->pid);
        if ($res === false) {
            $this->log("ERROR", "Cannot create pid file: %s", $this->file);
            exit;
        }
        @chmod($this->file, 0666);
    }

Usage Example

Exemple #1
0
$service = $opt['name'];
//name of the class in camel case
$cls = Scalr::camelize($service);
$logger = Scalr::getContainer()->logger('cron/client.php')->setLevel(Scalr::config('scalr.crontab.log_level'));
//Checking if task class exists.
if (!file_exists(SRCPATH . '/Scalr/System/Zmq/Cron/Task/' . $cls . '.php')) {
    $logger->fatal("Launch error. File %s does not exist.", SRCPATH . '/Scalr/System/Zmq/Cron/Task/' . $cls . '.php');
    exit;
}
$taskClass = 'Scalr\\System\\Zmq\\Cron\\Task\\' . $cls;
/* @var $task \Scalr\System\Zmq\Cron\AbstractTask */
$task = new $taskClass();
$taskConfig = $task->config();
$oPid = new PidFile(CACHEPATH . '/cron.client.' . $task->getName() . '.pid', '/client.php');
$oPid->setLogger($logger);
$oPid->create();
$interrupt = 0;
//Signal handler callback function
$sigHandler = function ($signo = null) use(&$interrupt, $oPid, $task, $taskConfig) {
    static $once = 0;
    $interrupt++;
    if ($once++) {
        return;
    }
    $task->log($taskConfig->daemon ? 'SERVICE' : 'DEBUG', "Client recieved termination SIGNAL:%d", intval($signo));
    //Terminating child processes (workers)
    $task->shutdown();
    //Removing pid file
    $oPid->remove();
    //No use to proceed
    exit;