/** * Push a job by its name onto the selected queue * * @param string $name Name of the job to create * @param mixed $payload Payload of the job set as content * @throws QueueNotFoundException If the method is called without a queue set * @return JobInterface Created job by the job plugin manager */ public function push($name, $payload = null) { if (null === $this->queue) { throw new QueueNotFoundException('You cannot push a job without a queue selected'); } $job = $this->jobPluginManager->get($name); if (null !== $payload) { $job->setContent($payload); } return $this->queue->push($job); }
/** * Create a resource * * @param mixed $data * @return ApiProblem|mixed */ public function create($data) { try { $pageEntity = new PagesEntity(); $pageEntity->setUuid(Uuid::uuid4()->toString()); $pageEntity->setUrl($data->site_url); $pageEntity->setStatus(PageInterface::STATUS_PENDING); $this->entityManager->persist($pageEntity); $this->entityManager->flush(); $queueJob = $this->queue->getJobPluginManager()->get('Application\\QueueJob\\ParsePage'); $queueJob->setContent(array('page_url' => $pageEntity->getUrl(), 'page_id' => $pageEntity->getId())); $this->queue->push($queueJob); return $pageEntity; } catch (\Exception $e) { return new ApiProblem(500, $e->getMessage()); } }
/** * {@inheritDoc} */ public function processJob(JobInterface $job, QueueInterface $queue) { if (!$queue instanceof BeanstalkdQueueInterface) { return WorkerEvent::JOB_STATUS_UNKNOWN; } /** * In Beanstalkd, if an error occurs (exception for instance), the job * is automatically reinserted into the queue after a configured delay * (the "visibility_timeout" option). If the job executed correctly, it * must explicitly be removed */ try { $job->execute(); $queue->delete($job); return WorkerEvent::JOB_STATUS_SUCCESS; } catch (Exception $exception) { // Do nothing, the job will be reinserted automatically for another try return WorkerEvent::JOB_STATUS_FAILURE_RECOVERABLE; } }
/** * {@inheritDoc} */ public function processJob(JobInterface $job, QueueInterface $queue) { if (!$queue instanceof SqsQueueInterface) { return WorkerEvent::JOB_STATUS_UNKNOWN; } // In SQS, if an error occurs (exception for instance), the job is automatically reinserted // into the queue after a configured delay (the "visibility_timeout" option). If the job executed // correctly, it must explicitly be removed try { $job->execute(); $queue->delete($job); return WorkerEvent::JOB_STATUS_SUCCESS; } catch (SqsException $sqsException) { // We want to retrigger SQS exception as they may include useful debugging information like lack of // permissions throw $sqsException; } catch (Exception $exception) { // Do nothing, the job will be reinserted automatically for another try return WorkerEvent::JOB_STATUS_FAILURE_RECOVERABLE; } }
public function execute() { $payload = $this->getContent(); echo "processing >> " . $payload['page_url'] . " >> for id >> " . $payload['page_id'] . "\n"; /* @var \Application\V1\Entity\Pages $pageEntity */ $pageEntity = $this->entityManager->find('Application\\V1\\Entity\\Pages', $payload['page_id']); try { $this->httpClient->setUri($payload['page_url']); $response = $this->httpClient->send(); $document = new Document($response->getBody()); $manager = $this->grabImageQueue->getJobPluginManager(); $jobs = []; $parsedPageUrl = parse_url($this->httpClient->getRequest()->getUriString()); $cnt = 0; /* @var \DOMElement $node */ foreach ($this->documentQuery->execute('//body//img', $document) as $node) { $job = $manager->get('Application\\QueueJob\\GrabImage'); $src = $this->normalizeSchemeAndHost($node->getAttribute('src'), $parsedPageUrl['scheme'], $parsedPageUrl['host']); $ext = strtolower(pathinfo($src, PATHINFO_EXTENSION)); $job->setContent(['image_src' => $src, 'image_ext' => $ext, 'page_id' => $payload['page_id']]); $jobs[] = $job; $cnt++; } if ($cnt < 1) { $pageEntity->setStatus(PageInterface::STATUS_DONE); } else { $pageEntity->setStatus(PageInterface::STATUS_RUNNING); } $pageEntity->setPendingImagesCnt($cnt); $pageEntity->setTotalImagesCnt($cnt); $this->entityManager->flush(); foreach ($jobs as $job) { $this->grabImageQueue->push($job); } echo "Jobs to push >> " . count($jobs) . " count pending images >>" . $cnt . "\n"; } catch (\Exception $e) { echo 'Exception: >> ' . $e->getMessage(); $pageEntity->setErrorMessage($e->getMessage()); if ($pageEntity->getStatusNumeric() == PageInterface::STATUS_RECOVERING) { $pageEntity->setStatus(PageInterface::STATUS_ERROR); $this->entityManager->flush(); return WorkerEvent::JOB_STATUS_FAILURE; } else { $pageEntity->setStatus(PageInterface::STATUS_RECOVERING); $this->entityManager->flush(); throw new ReleasableException(array('priority' => 10, 'delay' => 15)); } } }