From 7c027e6e7e6cf3068672c5912ccf340c7a0f031f Mon Sep 17 00:00:00 2001 From: Mansoor Khan <8mansoorkhan@gmail.com> Date: Mon, 22 Apr 2024 22:48:26 +0500 Subject: [PATCH] fix: dispatch CleanupUnusedUploadedFile job only in async queue connection --- README.md | 2 +- src/Actions/UnsplashPickerAction.php | 14 ++++++++------ src/Jobs/CleanupUnusedUploadedFile.php | 5 +---- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index 7b44ee8..5d65c9e 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ Add your Unsplash Client ID to your `.env` file UNSPLASH_CLIENT_ID=your-unsplash-client-id-goes-here ``` -**Note: Package includes a queueable job to clear/delete unused files after 24 hours. Make sure to set your queue connection to any async driver** +**Note: Package includes a queueable job to clear/delete unused files after 24 hours. Make sure to set your queue connection to any async driver. This Job is only dispatched when QUEUE_CONNECTION is not set to `sync`** ```php QUEUE_CONNECTION=database diff --git a/src/Actions/UnsplashPickerAction.php b/src/Actions/UnsplashPickerAction.php index c49e3ed..eb9d1da 100644 --- a/src/Actions/UnsplashPickerAction.php +++ b/src/Actions/UnsplashPickerAction.php @@ -117,12 +117,14 @@ public function uploadImage($arguments, Component $livewire, FileUpload $compone $filePath = Arr::first($component->getState()); - dispatch(new CleanupUnusedUploadedFile( - model: $livewire->getModel(), - column: $component->getStatePath(false), - filePath: $filePath, - diskName: $component->getDiskName() - ))->delay(now()->addDay()); + if (env('QUEUE_CONNECTION') !== 'sync') { + dispatch(new CleanupUnusedUploadedFile( + model: $livewire->getModel(), + column: $component->getStatePath(false), + filePath: $filePath, + diskName: $component->getDiskName() + ))->delay(now()->addDay()); + } } public function getImageSize(): ImageSize diff --git a/src/Jobs/CleanupUnusedUploadedFile.php b/src/Jobs/CleanupUnusedUploadedFile.php index 97ef59e..04a239c 100644 --- a/src/Jobs/CleanupUnusedUploadedFile.php +++ b/src/Jobs/CleanupUnusedUploadedFile.php @@ -41,9 +41,6 @@ public function handle(): void return; } - $pastHourTimestamp = now()->subHour()->timestamp; - if ($pastHourTimestamp > $storage->lastModified($this->filePath)) { - $storage->delete($this->filePath); - } + $storage->delete($this->filePath); } }