Skip to content

Commit

Permalink
New: Use job queue instead of while loop
Browse files Browse the repository at this point in the history
  • Loading branch information
markusguenther committed Jul 14, 2020
1 parent 324f83b commit d7c2ca8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 22 deletions.
22 changes: 22 additions & 0 deletions Classes/Exception/FilePreviewException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Unikka\FilePreviews\Exception;

/*
* This file is part of the Unikka.FilePreviews package.
*
* (c) Contributors to the package
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/

use Neos\Flow\Exception as FlowException;

/**
* A generic FilePreview Exception
*/
class FilePreviewException extends FlowException
{
}
33 changes: 17 additions & 16 deletions Classes/Service/ThumbnailGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@
use Neos\Utility\Arrays;
use Psr\Log\LoggerInterface;
use Flowpack\JobQueue\Common\Annotations as Job;
use Unikka\FilePreviews\Exception\FilePreviewException;

/**
* File Previews Client Service
*/
class ThumbnailGenerator
{
const API_STATUS_PENDING = 'pending';
const API_STATUS_FAILURE = 'failure';
const API_STATUS_SUCCESS = 'success';

/**
* @var ResourceManager
Expand Down Expand Up @@ -86,28 +89,26 @@ public function submitThumbnailToFilePreviewApi($thumbnail)
* @param Thumbnail $thumbnail
* @return void
* @throws Exception
* @throws FilePreviewException
* @throws \Neos\Flow\Persistence\Exception\IllegalObjectTypeException
* @throws \Neos\Flow\ResourceManagement\Exception
*/
public function fetchThumbnailFromFilePreviewApi($previewIdentifier, Thumbnail $thumbnail)
{
$success = false;
$elapsedTime = 0;
$maximumWaitingTime = (integer)$this->settings['maximumWaitingTime'];
$retryInterval = (integer)$this->settings['retryInterval'];
while ($success === false) {
if ($elapsedTime >= $maximumWaitingTime) {
break;
}
$previewApiClient = $this->getApiClient();
$response = $previewApiClient->retrieve($previewIdentifier);

$success = $response->status === 'success';
sleep($retryInterval);
$elapsedTime = $elapsedTime + $retryInterval;
$previewApiClient = $this->getApiClient();
$response = $previewApiClient->retrieve($previewIdentifier);

if ($response->status === self::API_STATUS_FAILURE) {
$message = sprintf(
'FilePreview.io was unable to generate thumbnail for the given reason: %s (%s)',
$response->error->message,
$response->error->code
);
throw new FilePreviewException($message, 1594730717);
}

if ($success === false || !isset($response->thumbnails[0])) {
throw new Exception('Unable to process the thumbnail is less than 20 seconds, sorry', 1447891433);
if ($response->status !== self::API_STATUS_SUCCESS || !isset($response->thumbnails[0])) {
throw new Exception('Unable to process the thumbnail', 1594729983);
}

$url = $response->thumbnails[0]->url;
Expand Down
6 changes: 2 additions & 4 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ Unikka:
FilePreviews:
apiKey: 'sQjSRy8WGaWo6d5dCDcGQ8Su1OxFY3'
apiSecret: 'f6VtvhwxhIX3HxBINlM9yxLQZ1rzHi'
maximumWaitingTime: 30
retryInterval: 15
defaultOptions:
format: 'jpg'

Expand All @@ -25,7 +23,7 @@ Flowpack:
maximumNumberOfReleases: 3
executeIsolated: true
options:
defaultTimeout: 50
defaultTimeout: 30
releaseOptions:
priority: 512
delay: 120
delay: 15
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ Unikka:
FilePreviews:
apiKey: 'key'
apiSecret: 'secret'
maximumWaitingTime: 30
retryInterval: 15
defaultOptions:
format: 'jpg'
```
Expand Down

0 comments on commit d7c2ca8

Please sign in to comment.