Exemplo n.º 1
0
 public function __construct(Config $config)
 {
     $this->ownerPid = $config->get('ownerPid');
     $this->ipcKey = new IpcKey($this->ownerPid, str_replace('\\', '_', get_class($this)));
     $this->id = msg_get_queue($this->ipcKey->generate());
     $this->stat = msg_stat_queue($this->id);
 }
Exemplo n.º 2
0
 /**
  * fork master process
  *
  * @return  int     $masterPid
  */
 public function forkMaster()
 {
     try {
         $fork = $this->fork();
     } catch (\RuntimeException $e) {
         throw $e;
     }
     $this->masterPid = $fork->getPid();
     $this->log->setMasterPid($this->masterPid);
     if (getmypid() === $this->ownerPid) {
         // owner
         $this->log->info('pid: ' . getmypid());
         return $this->masterPid;
     } else {
         // master
         $taskQueue = $this->queueFactory->createTaskQueue();
         $activeWorkerSet = new ActiveWorkerSet();
         $this->log->info('pid: ' . $this->masterPid);
         $log = $this->log;
         foreach ($this->signals as $sig) {
             $this->pcntl->signal($sig, function ($sig) use($log, $activeWorkerSet) {
                 $log->info('received signal: ' . $sig);
                 if ($activeWorkerSet->count() === 0) {
                     $log->info('no worker is active.');
                 } else {
                     $log->info('------> sending signal to workers. signal: ' . $sig);
                     $activeWorkerSet->terminate($sig);
                     $log->info('<------ sent signal');
                 }
                 exit;
             });
         }
         $concurrency = (int) $this->config->get('concurrency');
         while ($task = $taskQueue->dequeue()) {
             $this->log->info('dequeued task #' . $taskQueue->dequeuedCount());
             if ($activeWorkerSet->count() >= $concurrency) {
                 $status = null;
                 $workerPid = $this->pcntl->waitpid(-1, $status);
                 $activeWorkerSet->delete($workerPid);
             }
             $activeWorkerSet->add($this->forkWorker($task));
         }
         exit;
     }
 }
Exemplo n.º 3
0
 public function createResultQueue()
 {
     return $this->createQueue($this->config->get('resultQueue'));
 }