public function perform() { // Make sure we catch and process fatal errors from within the job register_shutdown_function(function () { if (!($e = error_get_last()) || !in_array($e['type'], array(E_ERROR, E_CORE_ERROR, E_COMPILE_ERROR, E_PARSE))) { return; } $this->logger->error('Worker (fork): Job [ ' . $this->job->getId() . ' ] failed fatally with message: [ ' . $e['message'] . ' ]'); $this->host->failJob($this->job, new \ErrorException($e['message'], $e['type'], 0, $e['file'], $e['line'])); }); try { $this->jobCreator->create($this->job)->perform(); } catch (\Exception $e) { $this->logger->error('Worker (fork): Job [ ' . $this->job->getId() . ' ] failed with message: [ ' . $e->getMessage() . ' ]'); $this->host->failJob($this->job, $e); throw $e; } $this->host->completeJob($this->job); $this->logger->debug('Worker (fork): Job [ ' . $this->job->getId() . ' ] completed successfully'); return 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(); }