From 0de0eef473f776781f6270719e97985bf52773db Mon Sep 17 00:00:00 2001 From: Mathias Gelhausen Date: Wed, 13 Feb 2019 11:15:51 +0100 Subject: [PATCH] Fix queue jobs to fetch external images --- .../Jobs/src/Queue/FetchExternalImageJob.php | 27 +++++++------------ .../Queue/FindJobsWithExternalImageJob.php | 3 ++- 2 files changed, 11 insertions(+), 19 deletions(-) diff --git a/module/Jobs/src/Queue/FetchExternalImageJob.php b/module/Jobs/src/Queue/FetchExternalImageJob.php index 8b1346a22..2de0427ce 100644 --- a/module/Jobs/src/Queue/FetchExternalImageJob.php +++ b/module/Jobs/src/Queue/FetchExternalImageJob.php @@ -10,11 +10,9 @@ /** */ namespace Jobs\Queue; -use Core\Queue\Exception\FatalJobException; -use Core\Queue\Exception\RecoverableJobException; +use Core\Queue\Job\MongoJob; use Core\Queue\LoggerAwareJobTrait; use Jobs\Repository\Job; -use SlmQueue\Job\AbstractJob; use Jobs\Entity\JobInterface as JobEntityInterface; use Zend\Http\Client; use Zend\Log\LoggerAwareInterface; @@ -25,7 +23,7 @@ * @author Mathias Gelhausen * @todo write test */ -class FetchExternalImageJob extends AbstractJob implements LoggerAwareInterface +class FetchExternalImageJob extends MongoJob implements LoggerAwareInterface { use LoggerAwareJobTrait; @@ -49,31 +47,25 @@ public function execute() $logger = $this->getLogger(); if (!$this->repository) { - $logger->err('Cannot execute without repository.'); - - throw new FatalJobException('Cannot execute without repository.'); + return $this->failure('Cannot execute without repository.'); } $payload = $this->getContent(); if (!isset($payload['jobId'])) { - $logger->err('Missing jobId in playload.'); - throw new FatalJobException('Missing jobId in playload.'); + return $this->failure('Missing jobId in playload.'); } /* @var \Jobs\Entity\Job $jobEntity */ $jobEntity = $this->repository->find($payload['jobId']); if (!$jobEntity) { - $logger->err('No job entity with the id ' . $payload['jobId'] . ' was found.'); - - throw new FatalJobException('No job entity with the id ' . $payload['jobId'] . ' was found.'); + return $this->failure('No job entity with the id ' . $payload['jobId'] . ' was found.'); } $uri = $jobEntity->getLogoRef(); if (0 !== strpos($uri, 'http')) { $logger->notice('logoRef seems not to be external: ' . $uri); - $logger->info('Skip fetching for this job.'); - return; + return $this->success('Skip fetching for this job.'); } $logger->debug('Trying to fetch image from ' . $uri); @@ -84,7 +76,7 @@ public function execute() if (200 != $response->getStatusCode()) { $logger->err('Received status code ' . $response->getStatusCode() . ' when trying to fetch ' . $uri); - throw new RecoverableJobException('Status code ' . $response->getStatusCode() . ' received.', ['delay' => '+5 minutes']); + return $this->recoverable('Status code ' . $response->getStatusCode() . ' received.', ['delay' => '+5 minutes']); } $content = $response->getBody(); @@ -93,9 +85,7 @@ public function execute() $imageName = '/static/Jobs/logos/' . $jobEntity->getId() . '.' . $ext; if (false === file_put_contents("public$imageName", $content, FILE_BINARY)) { - $logger->err('Writing image failed.'); - - throw new FatalJobException('Writing image failed.'); + return $this->failure('Writing image to "public' . $imageName . '" failed.'); } $logger->info('Saved job logo as ' . basename($imageName)); @@ -103,5 +93,6 @@ public function execute() $this->repository->store($jobEntity); $logger->info('Saved job logo as ' . basename($imageName)); + return $this->success(); } } diff --git a/module/Jobs/src/Queue/FindJobsWithExternalImageJob.php b/module/Jobs/src/Queue/FindJobsWithExternalImageJob.php index 7d8a7eee6..101d9f6ee 100644 --- a/module/Jobs/src/Queue/FindJobsWithExternalImageJob.php +++ b/module/Jobs/src/Queue/FindJobsWithExternalImageJob.php @@ -50,7 +50,6 @@ public function __construct(Job $repository = null) public function execute() { - return $this->recoverable('Test recoverable', '+12 days'); if (!$this->repository) { return $this->failure('Cannot execute without repository.'); } @@ -85,6 +84,8 @@ public function execute() $delay = 0 >= ($cursor->count() - $invalidJobs) ? '+2 hours' : '+5 minutes'; $queue->push(self::create(), ['delay' => $delay]); $logger->info('Reinserted to fetch more jobs with delay: ' . $delay); + + return $this->success(); } }