Resque\Worker::setPidFile PHP Method

setPidFile() public method

Set the worker pid file
public setPidFile ( string $pidFile )
$pidFile string Filename to store pid in
    public function setPidFile($pidFile)
    {
        $dir = realpath(dirname($pidFile));
        $filename = basename($pidFile);
        if (substr($pidFile, -1) == '/' or $filename == '.') {
            throw new \InvalidArgumentException('The pid file "' . $pidFile . '" must be a valid file path');
        }
        if (!is_dir($dir)) {
            throw new \RuntimeException('The pid file directory "' . $dir . '" does not exist');
        }
        if (!is_writeable($dir)) {
            throw new \RuntimeException('The pid file directory "' . $dir . '" is not writeable');
        }
        $this->pidFile = $dir . '/' . $filename;
        if (file_exists($this->pidFile) and posix_kill((int) trim(file_get_contents($this->pidFile)), 0)) {
            throw new \RuntimeException('Pid file "' . $pidFile . '" already exists and worker is still running.');
        }
        if (!file_put_contents($this->pidFile, getmypid(), LOCK_EX)) {
            throw new \RuntimeException('Could not write pid to file "' . $pidFile . '"');
        }
    }

Usage Example

Example #1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $queue = $this->getConfig('queue');
     $blocking = filter_var($this->getConfig('blocking'), FILTER_VALIDATE_BOOLEAN);
     // Create worker instance
     $worker = new Resque\Worker($queue, $blocking);
     $worker->setLogger($this->logger);
     if ($pidFile = $this->getConfig('pid')) {
         $worker->setPidFile($pidFile);
     }
     if ($interval = $this->getConfig('interval')) {
         $worker->setInterval($interval);
     }
     if ($timeout = $this->getConfig('timeout')) {
         $worker->setTimeout($timeout);
     }
     // The memory limit is the amount of memory we will allow the script to occupy
     // before killing it and letting a process manager restart it for us, which
     // is to protect us against any memory leaks that will be in the scripts.
     if ($memory = $this->getConfig('memory')) {
         $worker->setMemoryLimit($memory);
     }
     $worker->work();
 }