Drop Instagram import job when profile is missing instead of crashing

pull/7141/head
Your Name 2 weeks ago
parent 0e1a5686c3
commit 922d7f766e

@ -51,9 +51,27 @@ class ImportInstagram implements ShouldQueue
return;
}
$job = ImportJob::findOrFail($this->import->id);
$profile = Profile::findOrFail($job->profile_id);
$job = ImportJob::find($this->import->id);
if (! $job) {
return;
}
// The profile may have been soft-deleted (e.g. account deletion) after
// this job was queued. findOrFail would throw inside handle() and the
// job would retry until it lands in failed_jobs; drop it cleanly instead.
$profile = Profile::find($job->profile_id);
if (! $profile) {
$job->delete();
return;
}
$user = $profile->user;
if (! $user) {
$job->delete();
return;
}
$json = $job->mediaJson();
$collection = array_reverse($json['photos']);
$files = $job->files;

@ -0,0 +1,64 @@
<?php
use App\Jobs\ImportPipeline\ImportInstagram;
use App\Models\ImportJob;
use App\Models\Profile;
use App\Models\User;
use Illuminate\Foundation\Testing\LazilyRefreshDatabase;
uses(LazilyRefreshDatabase::class);
/*
|--------------------------------------------------------------------------
| ImportInstagram missing/soft-deleted profile handling
|--------------------------------------------------------------------------
|
| A queued Instagram import must not crash (and retry into failed_jobs) when
| its Profile has been soft-deleted after queueing. It should drop the job.
|
*/
beforeEach(function () {
config(['pixelfed.import.instagram.enabled' => true]);
});
it('drops the import job when the profile has been soft-deleted', function () {
$user = User::factory()->create();
$user->refresh();
$job = new ImportJob;
$job->profile_id = $user->profile_id;
$job->service = 'instagram';
$job->uuid = (string) \Illuminate\Support\Str::uuid();
$job->stage = 0;
$job->save();
// Soft-delete the profile, as DeleteAccountPipeline does.
Profile::whereId($user->profile_id)->delete();
// Must not throw.
(new ImportInstagram($job))->handle();
// The orphaned job is cleaned up.
expect(ImportJob::find($job->id))->toBeNull();
});
it('drops the import job when the job no longer exists', function () {
$user = User::factory()->create();
$user->refresh();
$job = new ImportJob;
$job->profile_id = $user->profile_id;
$job->service = 'instagram';
$job->uuid = (string) \Illuminate\Support\Str::uuid();
$job->stage = 0;
$job->save();
$jobId = $job->id;
$job->delete();
// Must not throw even though the ImportJob row is gone.
(new ImportInstagram($job))->handle();
expect(ImportJob::find($jobId))->toBeNull();
});
Loading…
Cancel
Save