Skip to content

Commit

Permalink
Fix queue jobs to fetch external images
Browse files Browse the repository at this point in the history
  • Loading branch information
TiSiE committed Feb 13, 2019
1 parent 4fb2617 commit 0de0eef
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 19 deletions.
27 changes: 9 additions & 18 deletions module/Jobs/src/Queue/FetchExternalImageJob.php
Expand Up @@ -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;
Expand All @@ -25,7 +23,7 @@
* @author Mathias Gelhausen <gelhausen@cross-solution.de>
* @todo write test
*/
class FetchExternalImageJob extends AbstractJob implements LoggerAwareInterface
class FetchExternalImageJob extends MongoJob implements LoggerAwareInterface
{
use LoggerAwareJobTrait;

Expand All @@ -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);
Expand All @@ -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();
Expand All @@ -93,15 +85,14 @@ 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));
$jobEntity->setLogoRef($imageName);
$this->repository->store($jobEntity);
$logger->info('Saved job logo as ' . basename($imageName));

return $this->success();
}
}
3 changes: 2 additions & 1 deletion module/Jobs/src/Queue/FindJobsWithExternalImageJob.php
Expand Up @@ -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.');
}
Expand Down Expand Up @@ -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();
}

}

0 comments on commit 0de0eef

Please sign in to comment.