1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Services; |
4
|
|
|
|
5
|
|
|
use App\Models\Collection; |
6
|
|
|
use App\Models\Settings; |
7
|
|
|
use Blacklight\ColorCLI; |
8
|
|
|
use Illuminate\Support\Facades\DB; |
9
|
|
|
use Illuminate\Support\Str; |
10
|
|
|
|
11
|
|
|
class CollectionCleanupService |
12
|
|
|
{ |
13
|
|
|
public function __construct( |
14
|
|
|
private readonly ColorCLI $colorCLI |
15
|
|
|
) {} |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Deletes finished/old collections, cleans orphans, and removes collections missed after NZB creation. |
19
|
|
|
* Mirrors the previous ProcessReleases::deleteCollections logic. |
20
|
|
|
* |
21
|
|
|
* @return int total deleted rows across operations (approximate) |
22
|
|
|
*/ |
23
|
|
|
public function deleteFinishedAndOrphans(bool $echoCLI): int |
24
|
|
|
{ |
25
|
|
|
$startTime = now()->toImmutable(); |
26
|
|
|
$deletedCount = 0; |
27
|
|
|
|
28
|
|
|
if ($echoCLI) { |
29
|
|
|
echo $this->colorCLI->header('Process Releases -> Delete finished collections.'.PHP_EOL). |
|
|
|
|
30
|
|
|
$this->colorCLI->primary(sprintf( |
|
|
|
|
31
|
|
|
'Deleting collections/binaries/parts older than %d hours.', |
32
|
|
|
Settings::settingValue('partretentionhours') |
33
|
|
|
), true); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
DB::transaction(function () use (&$deletedCount, $startTime, $echoCLI) { |
37
|
|
|
$deleted = 0; |
38
|
|
|
$deleteQuery = Collection::query() |
39
|
|
|
->where('dateadded', '<', now()->subHours(Settings::settingValue('partretentionhours'))) |
40
|
|
|
->delete(); |
41
|
|
|
if ($deleteQuery > 0) { |
42
|
|
|
$deleted = $deleteQuery; |
43
|
|
|
$deletedCount += $deleted; |
44
|
|
|
} |
45
|
|
|
$firstQuery = $fourthQuery = now(); |
46
|
|
|
|
47
|
|
|
$totalTime = $firstQuery->diffInSeconds($startTime, true); |
48
|
|
|
|
49
|
|
|
if ($echoCLI) { |
50
|
|
|
$this->colorCLI->primary( |
51
|
|
|
'Finished deleting '.$deleted.' old collections/binaries/parts in '. |
52
|
|
|
$totalTime.Str::plural(' second', $totalTime), |
53
|
|
|
true |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (random_int(0, 200) <= 1) { |
58
|
|
|
if ($echoCLI) { |
59
|
|
|
echo $this->colorCLI->header('Process Releases -> Remove CBP orphans.'.PHP_EOL). |
|
|
|
|
60
|
|
|
$this->colorCLI->primary('Deleting orphaned collections.'); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
$deleted = 0; |
64
|
|
|
$deleteQuery = Collection::query() |
65
|
|
|
->whereNull('binaries.id') |
66
|
|
|
->orWhereNull('parts.binaries_id') |
67
|
|
|
->leftJoin('binaries', 'collections.id', '=', 'binaries.collections_id') |
68
|
|
|
->leftJoin('parts', 'binaries.id', '=', 'parts.binaries_id') |
69
|
|
|
->delete(); |
70
|
|
|
|
71
|
|
|
if ($deleteQuery > 0) { |
72
|
|
|
$deleted = $deleteQuery; |
73
|
|
|
$deletedCount += $deleted; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
$totalTime = now()->diffInSeconds($firstQuery); |
77
|
|
|
|
78
|
|
|
if ($echoCLI) { |
79
|
|
|
$this->colorCLI->primary('Finished deleting '.$deleted.' orphaned collections in '.$totalTime.Str::plural(' second', $totalTime), true); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
if ($echoCLI) { |
84
|
|
|
$this->colorCLI->primary('Deleting collections that were missed after NZB creation.', true); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$deleted = 0; |
88
|
|
|
$collections = Collection::query() |
89
|
|
|
->where('releases.nzbstatus', '=', 1) |
90
|
|
|
->leftJoin('releases', 'releases.id', '=', 'collections.releases_id') |
91
|
|
|
->select('collections.id') |
92
|
|
|
->get(); |
93
|
|
|
|
94
|
|
|
foreach ($collections as $collection) { |
95
|
|
|
$deleted++; |
96
|
|
|
Collection::query()->where('id', $collection->id)->delete(); |
97
|
|
|
} |
98
|
|
|
$deletedCount += $deleted; |
99
|
|
|
|
100
|
|
|
$colDelTime = now()->diffInSeconds($fourthQuery, true); |
101
|
|
|
$totalTime = $fourthQuery->diffInSeconds($startTime, true); |
102
|
|
|
|
103
|
|
|
if ($echoCLI) { |
104
|
|
|
$this->colorCLI->primary( |
105
|
|
|
'Finished deleting '.$deleted.' collections missed after NZB creation in '.$colDelTime.Str::plural(' second', $colDelTime). |
106
|
|
|
PHP_EOL.'Removed '.number_format($deletedCount).' parts/binaries/collection rows in '.$totalTime.Str::plural(' second', $totalTime), |
107
|
|
|
true |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
}, 10); |
111
|
|
|
|
112
|
|
|
return $deletedCount; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.