/**
  * start the same number processes and kill the old sub process
  * just like nginx -s reload
  * this method will block until all the old process exit;
  *
  * @param bool $block
  */
 public function reload($block = true)
 {
     $old_process = $this->processes;
     for ($i = 0; $i < $this->max; $i++) {
         $process = new Process($this->runnable);
         $process->start();
         $this->processes[$process->getPid()] = $process;
     }
     foreach ($old_process as $process) {
         $process->shutdown();
         $process->wait($block);
         unset($this->processes[$process->getPid()]);
     }
 }
Example #2
0
 public static function ensureRunning($cmd, $pidfile, $restart = false)
 {
     $pidfile = '/var/run/' . $pidfile;
     // get pid from pidfile, protecting against invalid data
     $p1 = null;
     if (file_exists($pidfile) && is_readable($pidfile)) {
         $pid = substr(file_get_contents($pidfile), 0, 64);
         $p1 = new Process();
         $p1->setPid($pid);
     }
     if ($restart && $p1) {
         $p1->stop();
     }
     // check whether process with this pid is running
     if (!$p1 || !$p1->status()) {
         // check that we can write the pidfile, to avoid spawning endless processes
         if (file_put_contents($pidfile, 'test')) {
             $p2 = new Process($cmd);
             file_put_contents($pidfile, $p2->getPid());
         }
         return true;
     }
     return false;
 }
Example #3
0
 function checkPidFile($pidfile = null)
 {
     if ($pidfile == null) {
         if ($this->_pidfile) {
             $pidfile = $this->_pidfile;
         } else {
             $pidfile = $this->getName() . '.pid';
         }
     }
     logger::debug('Checking pidfile: %s', $pidfile);
     // Check if the process exist
     if (file_exists($pidfile)) {
         $pid = file_get_contents($pidfile);
         $p = new Process($pid);
         logger::debug(' - Inspecting pid %d', $pid);
         if ($p->exists()) {
             // Already running
             return self::PROCESS_RUNNING;
         }
         unlink($pidfile);
         $retval = self::PROCESS_STALE;
     } else {
         $retval = self::PROCESS_CLEAR;
     }
     $tp = new Process();
     file_put_contents($pidfile, $tp->getPid());
     $this->_pidfile = $pidfile;
     return $retval;
 }
 private function crawler_exec($keywords, $initial_date, $final_date)
 {
     $crawler_dir = Configure::read('Crawler.path');
     $scrapy_path = Configure::read('Crawler.scrapy.path');
     $crawler_name = "folha-spider";
     $command = "cd {$crawler_dir} ; {$scrapy_path} crawl {$crawler_name} " . " -a keywords={$keywords} " . " -a initial_date={$initial_date} " . " -a final_date={$final_date}";
     $this->log("Runing crawler with command {$command}", 'debug');
     $process = new Process($command);
     return $process->getPid();
 }
Example #5
0
 /**
  * fork process with job
  *
  * @return
  */
 public function spawn()
 {
     $worker = $this->ready();
     $ident = $this->getIdent($worker);
     //use Process
     $p = new Process($worker);
     $p->start();
     $pid = $p->getPid();
     if ($pid) {
         $this->childPool[$ident][$pid] = $pid;
     }
     // //@TODO
     // $pid = pcntl_fork();
     // if ($pid == -1) {
     // } elseif ($pid) {
     //     $this->childPool[$ident][$pid] = $pid;
     // } else {
     //     //@TODO
     //     //$this->setOwner($name = 'www');
     //     //$this->setTitle($title);
     //     $this->childPool[$ident] = [];
     //     //$cid = getmypid();
     //     //echo "\ngetmypid={$cid}\n";
     //     // 这个符号表示恢复系统对信号的默认处理
     //     pcntl_signal(SIGTERM, SIG_DFL);
     //     pcntl_signal(SIGCHLD, SIG_DFL);
     //     call_user_func_array($worker, array());
     //     //call_user_func_array($worker);
     //     exit(0);
     // }
 }
Example #6
0
 /**
  * @param Process $process
  * @return void
  */
 public function delete(Process $process)
 {
     unset($this->processes[$process->getPid()]);
 }
 /**
  * start the pool
  */
 public function start()
 {
     $alive_count = $this->aliveCount();
     // create sub process and run
     if ($alive_count < $this->max) {
         $need = $this->max - $alive_count;
         for ($i = 0; $i < $need; $i++) {
             $process = new Process($this->runnable);
             $process->start();
             $this->processes[$process->getPid()] = $process;
         }
     }
 }
 private function start_worker($function, $schema_id)
 {
     $cmd = "php cli.php worker {$schema_id} {$function} > protected/log/gearman_worker.{$schema_id}.{$function}.log 2>&1";
     $p = new Process($cmd);
     return $p->getPid();
 }
Example #9
0
 /**
  * Set the current process.
  * 
  * @param Process $proc the process to set as current,  the attribute <b>pid</b> should be filled with the process pid.
  * @throws EyeInvalidArgumentException If the arguments are incorrect
  * @throws EyeProcException If there is no such process with the given pid
  */
 public function setCurrentProcess(Process $proc = null)
 {
     if ($proc !== null) {
         $processTable = $this->getProcessesTable();
         $pid = $proc->getPid();
         if (!isset($processTable[$pid])) {
             throw new EyeProcException('Process $proc with PID ' . $pid . ' not found.');
         }
     }
     $this->currentProcess = $proc;
 }
Example #10
0
function stop()
{
    //stop function
    global $conf;
    global $db;
    $result = $db->query("SELECT count(*) AS C FROM pid");
    $row = $result->fetchArray();
    // output data of each row
    $result = $db->query("SELECT * FROM pid LIMIT 1");
    $row = $result->fetchArray();
    $process = new Process();
    $process->setPid($row["pid"]);
    if (!$process->status()) {
        $sql = "DELETE FROM pid";
        $result = $db->query($sql);
        $return_array["info"] = "process already dead";
    } else {
        $response_array["pid"] = $process->getPid();
        $process->stop();
        $sql = "DELETE FROM pid";
        $result = $db->query($sql);
        $return_array["info"] = "process stopped";
    }
    sleep(1);
    cleanpl();
    $return_array["status"] = 0;
    return $return_array;
}