Passed
Push — dev6 ( 6464e0...3707eb )
by Ron
19:50
created

GarbageCollectionJob::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 0
c 1
b 0
f 0
dl 0
loc 2
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
namespace App\Jobs;
4
5
use Illuminate\Bus\Queueable;
6
use Illuminate\Contracts\Queue\ShouldBeUnique;
7
use Illuminate\Contracts\Queue\ShouldQueue;
8
use Illuminate\Foundation\Bus\Dispatchable;
9
use Illuminate\Queue\InteractsWithQueue;
10
use Illuminate\Queue\SerializesModels;
11
use App\Models\CustomerEquipment;
12
use Exception;
13
use Illuminate\Support\Facades\Artisan;
14
use Illuminate\Support\Facades\Log;
15
use Illuminate\Support\Facades\Storage;
16
17
class GarbageCollectionJob implements ShouldQueue
18
{
19
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
20
21
    /**
22
     * Create a new job instance.
23
     *
24
     * @return void
25
     */
26
    public function __construct()
27
    {
28
        //
29
    }
30
31
    /**
32
     * Execute the job.
33
     *
34
     * @return void
35
     */
36
    public function handle()
37
    {
38
        //
39
        if(config('app.maintenance'))
40
        {
41
            Log::notice('Starting Garbage Collection Job');
42
            Artisan::call('down');
43
44
            $this->retryQueue();
45
            $this->checkChunkFolder();
46
            $this->cleanupCustomerEquipment();
47
48
            Artisan::call('up');
49
            Log::notice('Garbage Collection Job completed');
50
        }
51
    }
52
53
    /**
54
     * Retry any failed jobs in the work queue
55
     */
56
    protected function retryQueue()
57
    {
58
        try
59
        {
60
            Log::info('Retrying failed jobs');
61
            Artisan::call('queue:retry', ['id' => 'all']);
62
        }
63
        catch(Exception $e)
64
        {
65
            report($e);
66
        }
67
    }
68
69
    /**
70
     * Remove any files that have been left behind in the file chunks folder
71
     */
72
    protected function checkChunkFolder()
73
    {
74
        $files = Storage::files('chunks');
75
76
        if(count($files) > 0)
77
        {
78
            try
79
            {
80
                Log::info('Found partial files in the "chunks" folder');
81
                Log::info('File List - ', array($files));
82
                Storage::deleteDirectory('chunks');
83
            }
84
            catch(Exception $e)
85
            {
86
                report($e);
87
            }
88
        }
89
    }
90
91
    /**
92
     * Remove any customer equipment that has been soft deleted
93
     */
94
    protected function cleanupCustomerEquipment()
95
    {
96
        $equipList = CustomerEquipment::onlyTrashed()->get();
97
98
        if($equipList->count() > 0)
99
        {
100
            Log::info('Cleaning up Customer Systems', $equipList->toArray());
101
            $count = $equipList->count();
102
            foreach($equipList as $sys)
103
            {
104
                $sys->forceDelete();
105
            }
106
            Log::info($count.' customer equipment permanently deleted');
107
        }
108
    }
109
}
110