1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Console\Commands; |
4
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Facades\Log; |
8
|
|
|
use Illuminate\Support\Facades\File; |
9
|
|
|
use Illuminate\Support\Facades\Storage; |
10
|
|
|
use Illuminate\Support\Facades\Artisan; |
11
|
|
|
use Illuminate\Support\Facades\Queue; |
12
|
|
|
use Symfony\Component\Console\Output\BufferedOutput; |
13
|
|
|
|
14
|
|
|
class garbageCollection extends Command |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The name and signature of the console command. |
18
|
|
|
* |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $signature = 'garbagecollection:run'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The console command description. |
25
|
|
|
* |
26
|
|
|
* @var string |
27
|
|
|
*/ |
28
|
|
|
protected $description = 'Nightly maintenance script to wipe temporary and deleted data'; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Create a new command instance. |
32
|
|
|
* |
33
|
|
|
* @return void |
34
|
|
|
*/ |
35
|
2 |
|
public function __construct() |
36
|
|
|
{ |
37
|
2 |
|
parent::__construct(); |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Execute the console command. |
42
|
|
|
* |
43
|
|
|
* @return mixed |
44
|
|
|
*/ |
45
|
|
|
public function handle() |
46
|
|
|
{ |
47
|
|
|
Log::notice('Running Garbage Collection Job'); |
48
|
|
|
$this->call('down'); |
49
|
|
|
$this->retryQueue(); |
50
|
|
|
$this->checkChunkFolder(); |
51
|
|
|
$this->checkArchiveFolder(); |
52
|
|
|
$this->call('up'); |
53
|
|
|
Log::notice('Garbage Collection Job completed'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
// Check for any failed jobs in the work queue |
57
|
|
|
protected function retryQueue() |
58
|
|
|
{ |
59
|
|
|
Log::info('Restarting Worker Queues'); |
60
|
|
|
$this->call('queue:restart'); |
61
|
|
|
Log::info('Retrying failed jobs'); |
62
|
|
|
$this->call('queue:retry', ['id' => 'all']); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Determine if there are any files held over in the file chunks folder |
66
|
|
|
protected function checkChunkFolder() |
67
|
|
|
{ |
68
|
|
|
$files = Storage::files('chunks'); |
69
|
|
|
Log::info('Found partial files in the "chunks" folder'); |
70
|
|
|
Log::info('File List - ', array($files)); |
71
|
|
|
|
72
|
|
|
if($files) |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
Storage::deleteDirectory('chunks'); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
// Determine if there are any files held over in the archive_downlaods folder |
79
|
|
|
protected function checkArchiveFolder() |
80
|
|
|
{ |
81
|
|
|
$files = Storage::files('archive_downloads'); |
82
|
|
|
Log::info('Found files in the "archive_downloads" folder'); |
83
|
|
|
Log::debug('File List - ', array($files)); |
84
|
|
|
|
85
|
|
|
if($files) |
|
|
|
|
86
|
|
|
{ |
87
|
|
|
Storage::deleteDirectory('archive_downloads'); |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.