Skip to content

Commit

Permalink
Merge pull request #148 from biigle/patch-1
Browse files Browse the repository at this point in the history
Restore behavior to ignore special exceptions
  • Loading branch information
mzur authored Feb 6, 2024
2 parents 8c38244 + e040202 commit b198fb0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
27 changes: 23 additions & 4 deletions src/Jobs/ProcessAnnotatedFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,10 @@ public function handle()
->onQueue($this->queue)
->delay(60);
} catch (Exception $e) {
if ($this->shouldRetryAfterException($e)) {
if ($this->shouldGiveUpAfterException($e)) {
$class = get_class($this->file);
Log::warning("Could not process annotated {$class} {$this->file->id}: {$e->getMessage()}", ['exception' => $e]);
} elseif ($this->shouldRetryAfterException($e)) {
// Exponential backoff for retry after 10 and then 20 minutes.
$this->release($this->attempts() * 600);
} else {
Expand All @@ -147,13 +150,29 @@ public function handle()
abstract public function handleFile(VolumeFile $file, $path);

/**
* Determine if this job should retry instead of fail after an exception
* Determine if the job should give up trying because the error will likely not be
* fixed the next time.
*
* @param Exception $e
*/
protected function shouldGiveUpAfterException(Exception $e): bool
{
$message = $e->getMessage();
$giveUpError = (
// SSL certificate problem of the remote server.
// See: https://curl.haxx.se/libcurl/c/libcurl-errors.html
Str::contains($message, 'cURL error 60:')
);

return $giveUpError;
}

/**
* Determine if this job should retry instead of fail after an exception
*
* @return bool
* @param Exception $e
*/
protected function shouldRetryAfterException(Exception $e)
protected function shouldRetryAfterException(Exception $e): bool
{
$message = $e->getMessage();
$knownError = (
Expand Down
14 changes: 14 additions & 0 deletions tests/Jobs/ProcessAnnotatedImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use File;
use FileCache;
use Jcupitt\Vips\Image as VipsImage;
use Log;
use Mockery;
use Storage;
use TestCase;
Expand Down Expand Up @@ -385,6 +386,19 @@ public function testHandleError()
}
}

public function testHandleGiveUpError()
{
$disk = Storage::fake('test');
FileCache::shouldReceive('get')->andThrow(new Exception('cURL error 60:'));
Log::shouldReceive('warning')->once();

$annotation = ImageAnnotationTest::create();
$job = new ProcessAnnotatedImage($annotation->image);
$job->handle();
$prefix = fragment_uuid_path($annotation->image->uuid);
$disk->assertMissing("{$prefix}/{$annotation->id}.svg");
}

public function testFileLockedError()
{
$disk = Storage::fake('test');
Expand Down
13 changes: 13 additions & 0 deletions tests/Jobs/ProcessAnnotatedVideoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,19 @@ public function testHandleError()
}
}

public function testHandleGiveUpError()
{
$disk = Storage::fake('test');
FileCache::shouldReceive('get')->andThrow(new Exception('cURL error 60:'));
Log::shouldReceive('warning')->once();

$annotation = VideoAnnotationTest::create();
$job = new ProcessAnnotatedVideo($annotation->video);
$job->handle();
$prefix = fragment_uuid_path($annotation->video->uuid);
$disk->assertMissing("{$prefix}/v-{$annotation->id}.svg");
}

public function testFileLockedError()
{
$disk = Storage::fake('test');
Expand Down

0 comments on commit b198fb0

Please sign in to comment.