Example #1
0
 /**
  *
  */
 public function run()
 {
     $this->shutdown = false;
     $this->host->start();
     $this->logger->info('Host: Started host with ID: ' . $this->host->getId());
     $this->logger->info(sprintf('Long poll: %sµs. Short poll: %sµs. Queue failure sleep: %ss', $this->sleepIntervalMicroSecsLongPoll, $this->sleepIntervalMicroSecsShortPoll, $this->sleepIntervalSecsFailed));
     while (1) {
         $sleep = count($this->workers) === 0 ? $this->sleepIntervalMicroSecsLongPoll : $this->sleepIntervalMicroSecsShortPoll;
         $this->updateHostStatus();
         $queueSets = $this->host->getQueueSets();
         if (!$this->pause && !$this->shutdown) {
             if ($this->checkForJobs($queueSets)) {
                 $sleep = 0;
             } elseif ($this->testing) {
                 $this->shutdown = count($this->workers) === 0;
             }
         }
         if ($this->shutdown) {
             $this->logger->debug("Waiting for " . count($this->workers) . " workers");
             if (count($this->workers) === 0) {
                 // Don't wait for child processes - we know there aren't any
                 $this->spork->zombieOkay();
                 break;
             }
         }
         foreach ($this->workers as $id => $worker) {
             if (($status = $worker->getExitStatus()) !== false) {
                 $failed = $status !== 0;
                 $job = $worker->getJob();
                 try {
                     $this->host->failJob($job, new JobIncompleteException('Job was not marked complete within the process'));
                     $failed = true;
                     $this->logger->warning("Host: Job [ {$job->getId()} ] was not completed in the forked process which exited with status code: [ {$status} ] and was failed in the parent worker");
                 } catch (JobAlreadyCompleteException $e) {
                 }
                 if ($failed) {
                     $worker->getQueueSet()->sleepFor($this->sleepIntervalSecsFailed);
                     $this->logger->error("Host: Job [ {$job->getId()} ] failed, sleeping worker [ {$id} ] for [ {$this->sleepIntervalSecsFailed} ] microseconds");
                 }
                 unset($this->workers[$id]);
             } elseif ($this->terminate || $this->host->shouldBeKilled($worker->getJob()->getId())) {
                 $worker->terminate();
             }
         }
         $this->logger->debug("Sleeping for {$sleep} µs");
         usleep($sleep);
     }
     $this->logger->info('Host: Stopped');
     $this->host->stop();
 }