diff --git a/config/magento-async.php b/config/magento-async.php index eaabfda..1ac22b4 100644 --- a/config/magento-async.php +++ b/config/magento-async.php @@ -3,6 +3,6 @@ return [ 'queue' => 'default', - /* Cleanup requests after amount of time (hours) */ - 'cleanup' => 3, + /* Always cleanup bulk requests after amount of time (hours) */ + 'cleanup' => 168, ]; diff --git a/src/Actions/CleanBulkRequests.php b/src/Actions/CleanBulkRequests.php index c0aade3..140e7aa 100644 --- a/src/Actions/CleanBulkRequests.php +++ b/src/Actions/CleanBulkRequests.php @@ -2,7 +2,9 @@ namespace JustBetter\MagentoAsync\Actions; +use Illuminate\Database\Eloquent\Builder; use JustBetter\MagentoAsync\Contracts\CleansBulkRequests; +use JustBetter\MagentoAsync\Enums\OperationStatus; use JustBetter\MagentoAsync\Models\BulkRequest; class CleanBulkRequests implements CleansBulkRequests @@ -13,7 +15,16 @@ public function clean(): void $cleanupHours = config('magento-async.cleanup'); BulkRequest::query() - ->where('created_at', '<', now()->subHours($cleanupHours)) + // Delete where there are no more operations with a non-final status + ->where(function (Builder $query) use ($cleanupHours): void { + $query->whereDoesntHave('operations', function (Builder $query): void { + $query + ->whereNull('status') + ->orWhere('status', '=', OperationStatus::Open); + })->orWhere('created_at', '<', now()->subHour()); // Never delete within an hour + }) + // Always delete after 7 days + ->orWhere('created_at', '<', now()->subHours($cleanupHours)) ->delete(); }