Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make ApplyLargoSession job more robust against errors #198

Merged
merged 1 commit into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 35 additions & 16 deletions src/Jobs/ApplyLargoSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use DB;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;
use Throwable;

class ApplyLargoSession extends Job implements ShouldQueue
{
Expand Down Expand Up @@ -120,23 +121,41 @@ public function __construct($id, User $user, $dismissedImageAnnotations, $change
*/
public function handle()
{
try {
DB::transaction(function () {
$this->handleImageAnnotations();
$this->handleVideoAnnotations();
});

LargoSessionSaved::dispatch($this->id, $this->user);
} catch (\Exception $e) {
LargoSessionFailed::dispatch($this->id, $this->user);
} finally {
Volume::where('attrs->largo_job_id', $this->id)->each(function ($volume) {
$attrs = $volume->attrs;
unset($attrs['largo_job_id']);
$volume->attrs = $attrs;
$volume->save();
});
if (!Volume::where('attrs->largo_job_id', $this->id)->exists()) {
// The job previously exited or failed. Don't run it twice.
// This can happen if the admin reruns failed jobs.
return;
}

DB::transaction(function () {
$this->handleImageAnnotations();
$this->handleVideoAnnotations();
});

$this->cleanupJobId();
LargoSessionSaved::dispatch($this->id, $this->user);
}

/**
* Handle a job failure.
*/
public function failed(?Throwable $exception): void
{
$this->cleanupJobId();
LargoSessionFailed::dispatch($this->id, $this->user);
}

/**
* Remove the properties that indicate that a save Largo session is in progress.
*/
protected function cleanupJobId(): void
{
Volume::where('attrs->largo_job_id', $this->id)->each(function ($volume) {
$attrs = $volume->attrs;
unset($attrs['largo_job_id']);
$volume->attrs = $attrs;
$volume->save();
});
}

/**
Expand Down
61 changes: 38 additions & 23 deletions tests/Jobs/ApplyLargoSessionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Biigle\Tests\VideoAnnotationLabelTest;
use Biigle\Tests\VideoAnnotationTest;
use Biigle\Tests\VideoTest;
use Biigle\VolumeFile;
use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Queue;
use TestCase;
Expand All @@ -27,6 +28,7 @@ public function testChangedAlreadyExistsImageAnnotations()
{
$user = UserTest::create();
$image = ImageTest::create();
$this->setJobId($image, 'job_id');
$a1 = ImageAnnotationTest::create(['image_id' => $image->id]);

$l1 = LabelTest::create();
Expand Down Expand Up @@ -57,6 +59,7 @@ public function testChangedAlreadyExistsVideoAnnotations()
{
$user = UserTest::create();
$video = VideoTest::create();
$this->setJobId($video, 'job_id');
$a1 = VideoAnnotationTest::create(['video_id' => $video->id]);

$l1 = LabelTest::create();
Expand Down Expand Up @@ -87,6 +90,7 @@ public function testChangedDuplicateImageAnnotations()
{
$user = UserTest::create();
$image = ImageTest::create();
$this->setJobId($image, 'job_id');
$a1 = ImageAnnotationTest::create(['image_id' => $image->id]);
$l1 = LabelTest::create();
$al1 = ImageAnnotationLabelTest::create([
Expand All @@ -112,6 +116,7 @@ public function testChangedDuplicateVideoAnnotations()
{
$user = UserTest::create();
$video = VideoTest::create();
$this->setJobId($video, 'job_id');
$a1 = VideoAnnotationTest::create(['video_id' => $video->id]);
$l1 = LabelTest::create();
$al1 = VideoAnnotationLabelTest::create([
Expand All @@ -137,6 +142,7 @@ public function testAnnotationMeanwhileDeletedImageAnnotations()
{
$user = UserTest::create();
$image = ImageTest::create();
$this->setJobId($image, 'job_id');
$a1 = ImageAnnotationTest::create(['image_id' => $image->id]);
$a2 = ImageAnnotationTest::create(['image_id' => $image->id]);

Expand Down Expand Up @@ -169,6 +175,7 @@ public function testAnnotationMeanwhileDeletedVideoAnnotations()
{
$user = UserTest::create();
$video = VideoTest::create();
$this->setJobId($video, 'job_id');
$a1 = VideoAnnotationTest::create(['video_id' => $video->id]);
$a2 = VideoAnnotationTest::create(['video_id' => $video->id]);

Expand Down Expand Up @@ -201,6 +208,7 @@ public function testLabelMeanwhileDeletedImageAnnotations()
{
$user = UserTest::create();
$image = ImageTest::create();
$this->setJobId($image, 'job_id');
$a1 = ImageAnnotationTest::create(['image_id' => $image->id]);
$a2 = ImageAnnotationTest::create(['image_id' => $image->id]);

Expand Down Expand Up @@ -235,6 +243,7 @@ public function testLabelMeanwhileDeletedVideoAnnotations()
{
$user = UserTest::create();
$video = VideoTest::create();
$this->setJobId($video, 'job_id');
$a1 = VideoAnnotationTest::create(['video_id' => $video->id]);
$a2 = VideoAnnotationTest::create(['video_id' => $video->id]);

Expand Down Expand Up @@ -269,6 +278,7 @@ public function testDismissImageAnnotations()
{
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');

$dismissed = [$al1->label_id => [$al1->annotation_id]];
$job = new ApplyLargoSession('job_id', $user, $dismissed, [], [], [], false);
Expand All @@ -284,6 +294,7 @@ public function testDismissVideoAnnotations()
{
$user = UserTest::create();
$al1 = VideoAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->video, 'job_id');

$dismissed = [$al1->label_id => [$al1->annotation_id]];
$job = new ApplyLargoSession('job_id', $user, [], [], $dismissed, [], false);
Expand All @@ -299,6 +310,7 @@ public function testDismissForceImageAnnotations()
{
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');
$user2 = UserTest::create();

$dismissed = [$al1->label_id => [$al1->annotation_id]];
Expand All @@ -314,6 +326,7 @@ public function testDismissForceVideoAnnotations()
{
$user = UserTest::create();
$al1 = VideoAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->video, 'job_id');
$user2 = UserTest::create();

$dismissed = [$al1->label_id => [$al1->annotation_id]];
Expand All @@ -329,6 +342,7 @@ public function testChangeOwnImageAnnotations()
{
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');
$annotation = $al1->annotation;
$l1 = LabelTest::create();

Expand All @@ -347,6 +361,7 @@ public function testChangeOwnVideoAnnotations()
{
$user = UserTest::create();
$al1 = VideoAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->video, 'job_id');
$annotation = $al1->annotation;
$l1 = LabelTest::create();

Expand All @@ -365,6 +380,7 @@ public function testChangeOtherImageAnnotations()
{
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');
$annotation = $al1->annotation;
$l1 = LabelTest::create();

Expand All @@ -386,6 +402,7 @@ public function testChangeOtherVideoAnnotations()
{
$user = UserTest::create();
$al1 = VideoAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->video, 'job_id');
$annotation = $al1->annotation;
$l1 = LabelTest::create();

Expand All @@ -407,6 +424,7 @@ public function testChangeOtherForceImageAnnotations()
{
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');
$annotation = $al1->annotation;
$l1 = LabelTest::create();

Expand All @@ -426,6 +444,7 @@ public function testChangeOtherForceVideoAnnotations()
{
$user = UserTest::create();
$al1 = VideoAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->video, 'job_id');
$annotation = $al1->annotation;
$l1 = LabelTest::create();

Expand All @@ -445,6 +464,7 @@ public function testChangeMultipleImageAnnotations()
{
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');
$annotation = $al1->annotation;
$al2 = ImageAnnotationLabelTest::create([
'user_id' => $user->id,
Expand Down Expand Up @@ -478,6 +498,7 @@ public function testChangeMultipleVideoAnnotations()
{
$user = UserTest::create();
$al1 = VideoAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->video, 'job_id');
$annotation = $al1->annotation;
$al2 = VideoAnnotationLabelTest::create([
'user_id' => $user->id,
Expand Down Expand Up @@ -555,12 +576,8 @@ public function testRemoveJobIdOnErrorImageAnnotations()

$dismissed = [$al1->label_id => [$al1->annotation_id]];
$changed = [$l1->id => [$al1->annotation_id]];
$job = new ApplyLargoSessionStub('job_id', $user, $dismissed, $changed, [], [], false);
try {
$job->handle();
} catch (\Exception $e) {
// ignore
}
$job = new ApplyLargoSession('job_id', $user, $dismissed, $changed, [], [], false);
$job->failed(null);

$this->assertEmpty($volume->fresh()->attrs);
}
Expand All @@ -577,12 +594,8 @@ public function testRemoveJobIdOnErrorVideoAnnotations()

$dismissed = [$al1->label_id => [$al1->annotation_id]];
$changed = [$l1->id => [$al1->annotation_id]];
$job = new ApplyLargoSessionStub('job_id', $user, [], [], $dismissed, $changed, false);
try {
$job->handle();
} catch (\Exception $e) {
// ignore
}
$job = new ApplyLargoSession('job_id', $user, [], [], $dismissed, $changed, false);
$job->failed(null);

$this->assertEmpty($volume->fresh()->attrs);
}
Expand All @@ -592,6 +605,7 @@ public function testDispatchEventOnSuccess()
Event::fake();
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');
$l1 = LabelTest::create();

$dismissed = [$al1->label_id => [$al1->annotation_id]];
Expand All @@ -611,16 +625,13 @@ public function testDispatchEventOnError()
Event::fake();
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');
$l1 = LabelTest::create();

$dismissed = [$al1->label_id => [$al1->annotation_id]];
$changed = [$l1->id => [$al1->annotation_id]];
$job = new ApplyLargoSessionStub('job_id', $user, $dismissed, $changed, [], [], false);
try {
$job->handle();
} catch (\Exception $e) {
// ignore
}
$job = new ApplyLargoSession('job_id', $user, $dismissed, $changed, [], [], false);
$job->failed(null);

Event::assertDispatched(function (LargoSessionFailed $event) use ($user) {
$this->assertSame($user->id, $event->user->id);
Expand All @@ -633,6 +644,7 @@ public function testChangeImageAnnotationCopyFeatureVector()
{
$user = UserTest::create();
$al1 = ImageAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->image, 'job_id');
$vector1 = ImageAnnotationLabelFeatureVector::factory()->create([
'id' => $al1->id,
'annotation_id' => $al1->annotation_id,
Expand All @@ -655,6 +667,7 @@ public function testChangeVideoAnnotationCopyFeatureVector()
{
$user = UserTest::create();
$al1 = VideoAnnotationLabelTest::create(['user_id' => $user->id]);
$this->setJobId($al1->annotation->video, 'job_id');
$vector1 = VideoAnnotationLabelFeatureVector::factory()->create([
'id' => $al1->id,
'annotation_id' => $al1->annotation_id,
Expand All @@ -677,6 +690,7 @@ public function testDismissButChangeBackToSameLabelImage()
{
$user = UserTest::create();
$image = ImageTest::create();
$this->setJobId($image, 'job_id');
$a1 = ImageAnnotationTest::create(['image_id' => $image->id]);
$al1 = ImageAnnotationLabelTest::create([
'annotation_id' => $a1->id,
Expand All @@ -703,6 +717,7 @@ public function testDismissButChangeBackToSameLabelVideo()
{
$user = UserTest::create();
$video = VideoTest::create();
$this->setJobId($video, 'job_id');
$a1 = VideoAnnotationTest::create(['video_id' => $video->id]);
$al1 = VideoAnnotationLabelTest::create([
'annotation_id' => $a1->id,
Expand All @@ -724,12 +739,12 @@ public function testDismissButChangeBackToSameLabelVideo()
$this->assertNotNull($a1->fresh());
$this->assertNotNull($a2->fresh());
}
}

class ApplyLargoSessionStub extends ApplyLargoSession
{
protected function ignoreDeletedLabels($dismissed, $changed)
protected function setJobId(VolumeFile $file, string $id): void
{
throw new \Exception;
$attrs = $file->volume->attrs ?? [];
$attrs['largo_job_id'] = $id;
$file->volume->attrs = $attrs;
$file->volume->save();
}
}