Skip to content

Commit

Permalink
Merge pull request #33 from biigle/missing-models-fix
Browse files Browse the repository at this point in the history
Missing models fix
  • Loading branch information
mzur authored Jun 27, 2024
2 parents e07c24f + 8d6fd3a commit 6cedad0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Jobs/DeleteStorageRequestFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,16 @@ class DeleteStorageRequestFile extends Job implements ShouldQueue

/**
* File that should be deleted
*
* **/
*/
public $file;

/**
* Path of the file to be deleted.
*
* @var string
*/
public $path;

/**
* Whether the request of the file was pending or not.
*
Expand Down Expand Up @@ -47,8 +53,6 @@ class DeleteStorageRequestFile extends Job implements ShouldQueue
* **/
public $oldRetryCount;

public $deleteWhenMissingModels = true;

/**
* Create a new job instance.
*
Expand All @@ -57,6 +61,7 @@ class DeleteStorageRequestFile extends Job implements ShouldQueue
public function __construct(StorageRequestFile $file)
{
$this->file = $file;
$this->path = $file->path;
$this->oldRetryCount = $file->retry_count;
$request = $file->request;
$this->pending = is_null($request->expires_at);
Expand All @@ -75,8 +80,10 @@ public function __construct(StorageRequestFile $file)
*/
public function handle()
{
$fileExists = $this->file && $this->file->exists();

// Do not delete files when delete-request is outdated
if($this->oldRetryCount != $this->file->refresh()->retry_count) {
if ($fileExists && $this->oldRetryCount != $this->file->refresh()->retry_count) {
return;
}

Expand All @@ -86,7 +93,7 @@ public function handle()
$disk = Storage::disk(config('user_storage.storage_disk'));
}

$path = "{$this->prefix}/{$this->file->path}";
$path = "{$this->prefix}/{$this->path}";

if ($this->chunks) {
$paths = array_map(function ($chunk) use ($path) {
Expand All @@ -99,7 +106,7 @@ public function handle()
}

if (!$success) {
throw new Exception("Could not delete file '{$this->file->path}' for storage request with prefix '{$this->prefix}'.");
throw new Exception("Could not delete file '{$this->path}' for storage request with prefix '{$this->prefix}'.");
}

if ($success && count($disk->allFiles($this->prefix)) === 0) {
Expand All @@ -110,6 +117,8 @@ public function handle()
}
}

$this->file->delete();
if ($fileExists) {
$this->file->delete();
}
}
}
25 changes: 25 additions & 0 deletions tests/Jobs/DeleteStorageRequestFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,29 @@ public function testOutdatedDeleteJob(){
$this->assertTrue($disk->exists("request-{$file->storage_request_id}/a.jpg.2"));
$this->assertModelExists($file);
}

public function testMissingModel(){
config(['user_storage.storage_disk' => 'test']);
$disk = Storage::fake('test');

$request = StorageRequest::factory()->create([
'expires_at' => now()->subWeeks(2),
]);

$file = StorageRequestFile::factory()->create([
'path' => 'image.png',
'storage_request_id' => $request->id,
'retry_count' => 1,
]);

$disk->put('user-1/image.png','');

$job = new DeleteStorageRequestFile($file);
$request->delete(); // Simulate deleted models
$job->handle();

$this->assertFalse($disk->exists("user-{$request->user_id}/image.png"));
$this->assertModelMissing($request);
$this->assertModelMissing($file);
}
}

0 comments on commit 6cedad0

Please sign in to comment.