|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Jobs; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use Nwidart\Modules\Facades\Module; |
|
7
|
|
|
|
|
8
|
|
|
use Illuminate\Bus\Queueable; |
|
9
|
|
|
use Illuminate\Support\Facades\Log; |
|
10
|
|
|
use Illuminate\Queue\SerializesModels; |
|
11
|
|
|
use Illuminate\Support\Facades\Artisan; |
|
12
|
|
|
use Illuminate\Support\Facades\Storage; |
|
13
|
|
|
use Illuminate\Queue\InteractsWithQueue; |
|
14
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
|
15
|
|
|
use Illuminate\Contracts\Queue\ShouldQueue; |
|
16
|
|
|
|
|
17
|
|
|
use App\Models\CustomerEquipment; |
|
18
|
|
|
use App\Models\CustomerFile; |
|
19
|
|
|
use App\Traits\FileTrait; |
|
20
|
|
|
use Carbon\Carbon; |
|
21
|
|
|
|
|
22
|
|
|
class GarbageCollectionJob implements ShouldQueue |
|
23
|
|
|
{ |
|
24
|
|
|
use Queueable; |
|
25
|
|
|
use Dispatchable; |
|
26
|
|
|
use SerializesModels; |
|
27
|
|
|
use InteractsWithQueue; |
|
28
|
|
|
|
|
29
|
|
|
use FileTrait; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Execute the job |
|
33
|
|
|
*/ |
|
34
|
|
|
public function handle() |
|
35
|
|
|
{ |
|
36
|
|
|
if(config('app.maintenance')) |
|
37
|
|
|
{ |
|
38
|
|
|
Log::notice('Starting Garbage Collection Job'); |
|
39
|
|
|
Artisan::call('down'); |
|
40
|
|
|
|
|
41
|
|
|
$this->retryQueue(); |
|
42
|
|
|
$this->checkChunkFolder(); |
|
43
|
|
|
$this->cleanupCustomerEquipment(); |
|
44
|
|
|
$this->cleanupCustomerFiles(); |
|
45
|
|
|
$this->moduleGarbageCollection(); |
|
46
|
|
|
|
|
47
|
|
|
Artisan::call('up'); |
|
48
|
|
|
Log::notice('Garbage Collection Job completed'); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Retry any failed jobs in the work queue |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function retryQueue() |
|
56
|
|
|
{ |
|
57
|
|
|
try |
|
58
|
|
|
{ |
|
59
|
|
|
Log::info('Retrying failed jobs'); |
|
60
|
|
|
Artisan::call('queue:retry', ['id' => 'all']); |
|
61
|
|
|
} |
|
62
|
|
|
catch(Exception $e) |
|
63
|
|
|
{ |
|
64
|
|
|
report($e); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Remove any files that have been left behind in the file chunks folder |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function checkChunkFolder() |
|
72
|
|
|
{ |
|
73
|
|
|
$files = Storage::files('chunks'); |
|
74
|
|
|
|
|
75
|
|
|
if(count($files) > 0) |
|
76
|
|
|
{ |
|
77
|
|
|
try |
|
78
|
|
|
{ |
|
79
|
|
|
Log::info('Found partial files in the "chunks" folder'); |
|
80
|
|
|
Log::info('File List - ', array($files)); |
|
81
|
|
|
Storage::deleteDirectory('chunks'); |
|
82
|
|
|
} |
|
83
|
|
|
catch(Exception $e) |
|
84
|
|
|
{ |
|
85
|
|
|
report($e); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Remove any customer equipment that has been soft deleted |
|
92
|
|
|
*/ |
|
93
|
|
|
protected function cleanupCustomerEquipment() |
|
94
|
|
|
{ |
|
95
|
|
|
$equipList = CustomerEquipment::onlyTrashed()->get(); |
|
96
|
|
|
|
|
97
|
|
|
if($equipList->count() > 0) |
|
98
|
|
|
{ |
|
99
|
|
|
Log::info('Cleaning up Customer Systems', $equipList->toArray()); |
|
100
|
|
|
$count = $equipList->count(); |
|
101
|
|
|
foreach($equipList as $sys) |
|
102
|
|
|
{ |
|
103
|
|
|
$sys->forceDelete(); |
|
104
|
|
|
} |
|
105
|
|
|
Log::info($count.' customer equipment permanently deleted'); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Remove any customer files that have been soft deleted more than 48 hours ago |
|
111
|
|
|
*/ |
|
112
|
|
|
protected function cleanupCustomerFiles() |
|
113
|
|
|
{ |
|
114
|
|
|
$fileList = CustomerFile::onlyTrashed()->whereDate('deleted_at', '<', now()->subDays(2))->get(); |
|
115
|
|
|
|
|
116
|
|
|
foreach($fileList as $file) |
|
117
|
|
|
{ |
|
118
|
|
|
$file->forceDelete(); |
|
119
|
|
|
$this->deleteFile($file->file_id); |
|
120
|
|
|
|
|
121
|
|
|
Log::info('Deleted Customer File ID '.$file->cust_file_id.' for Customer ID '.$file->cust_id, $file->toArray()); |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Run any Garbage Collection jobs for the attached modules |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function moduleGarbageCollection() |
|
129
|
|
|
{ |
|
130
|
|
|
$modules = Module::allEnabled(); |
|
131
|
|
|
|
|
132
|
|
|
foreach($modules as $module) |
|
133
|
|
|
{ |
|
134
|
|
|
if(class_exists('\\Modules\\'.$module->getName().'\\Jobs\\GarbageCollectionJob')) |
|
135
|
|
|
{ |
|
136
|
|
|
$class = '\\Modules\\'.$module->getName().'\\Jobs\\GarbageCollectionJob'; |
|
137
|
|
|
$class::dispatch(); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|