Passed
Push — dev6 ( d51ca7...a8a5e7 )
by Ron
16:30
created

GarbageCollectionJob   A

Complexity

Total Complexity 13

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 13
eloc 39
c 1
b 0
f 0
dl 0
loc 97
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A checkChunkFolder() 0 15 3
A cleanupCustomerEquipment() 0 13 3
A handle() 0 14 2
A moduleGarbageCollection() 0 10 3
A retryQueue() 0 10 2
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
19
class GarbageCollectionJob implements ShouldQueue
20
{
21
    use Queueable;
22
    use Dispatchable;
23
    use SerializesModels;
24
    use InteractsWithQueue;
25
26
    /**
27
     * Execute the job
28
     */
29
    public function handle()
30
    {
31
        if(config('app.maintenance'))
32
        {
33
            Log::notice('Starting Garbage Collection Job');
34
            Artisan::call('down');
35
36
            $this->retryQueue();
37
            $this->checkChunkFolder();
38
            $this->cleanupCustomerEquipment();
39
            $this->moduleGarbageCollection();
40
41
            Artisan::call('up');
42
            Log::notice('Garbage Collection Job completed');
43
        }
44
    }
45
46
    /**
47
     * Retry any failed jobs in the work queue
48
     */
49
    protected function retryQueue()
50
    {
51
        try
52
        {
53
            Log::info('Retrying failed jobs');
54
            Artisan::call('queue:retry', ['id' => 'all']);
55
        }
56
        catch(Exception $e)
57
        {
58
            report($e);
59
        }
60
    }
61
62
    /**
63
     * Remove any files that have been left behind in the file chunks folder
64
     */
65
    protected function checkChunkFolder()
66
    {
67
        $files = Storage::files('chunks');
68
69
        if(count($files) > 0)
70
        {
71
            try
72
            {
73
                Log::info('Found partial files in the "chunks" folder');
74
                Log::info('File List - ', array($files));
75
                Storage::deleteDirectory('chunks');
76
            }
77
            catch(Exception $e)
78
            {
79
                report($e);
80
            }
81
        }
82
    }
83
84
    /**
85
     * Remove any customer equipment that has been soft deleted
86
     */
87
    protected function cleanupCustomerEquipment()
88
    {
89
        $equipList = CustomerEquipment::onlyTrashed()->get();
90
91
        if($equipList->count() > 0)
92
        {
93
            Log::info('Cleaning up Customer Systems', $equipList->toArray());
94
            $count = $equipList->count();
95
            foreach($equipList as $sys)
96
            {
97
                $sys->forceDelete();
98
            }
99
            Log::info($count.' customer equipment permanently deleted');
100
        }
101
    }
102
103
    /**
104
     * Run any Garbage Collection jobs for the attached modules
105
     */
106
    protected function moduleGarbageCollection()
107
    {
108
        $modules = Module::allEnabled();
109
110
        foreach($modules as $module)
111
        {
112
            if(class_exists('\\Modules\\'.$module->getName().'\\Jobs\\GarbageCollectionJob'))
113
            {
114
                $class = '\\Modules\\'.$module->getName().'\\Jobs\\GarbageCollectionJob';
115
                $class::dispatch();
116
            }
117
        }
118
    }
119
}
120