Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 26 |
DockerTasks | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 26 |
run | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 26 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\DockerScale\Retriever\Providers; |
4 | |
5 | use Qmp\Laravel\DockerScale\Models\DockerTask; |
6 | use Illuminate\Support\Facades\Log; |
7 | |
8 | class DockerTasks extends DockerAbstract |
9 | { |
10 | protected $model = DockerTask::class; |
11 | |
12 | protected $matchUpdate = ['service', 'task']; |
13 | |
14 | protected $toNotRemove = []; |
15 | |
16 | protected $cmd = 'docker node ps --no-trunc --format "{{.ID}}|{{.Name}}|{{.Node}}|{{.DesiredState}}|{{.CurrentState}}|{{.Error}}"'; |
17 | |
18 | public function run() |
19 | { |
20 | $this->toNotRemove = []; |
21 | $output = $this->exec($this->cmd); |
22 | $result = array_map(function($item) { |
23 | $name = explode('.', $item[1]); |
24 | $result = [ |
25 | 'task_id' => $item[0], |
26 | 'service' => $name[0], |
27 | 'task' => $name[1], |
28 | 'node' => $item[2], |
29 | 'desired_state' => $item[3], |
30 | 'current_state' => $item[4], |
31 | 'error' => $item[5], |
32 | ]; |
33 | $this->toNotRemove[] = [ |
34 | 'service' => $name[0], |
35 | 'task' => $name[1], |
36 | ]; |
37 | |
38 | return $this->update($result); |
39 | }, $output); |
40 | |
41 | // Remove no present tasks |
42 | if (count($this->toNotRemove) > 0) { |
43 | $notRemoveIds = DockerTask::orderBy('id'); |
44 | foreach ($this->toNotRemove as $notRemove) { |
45 | $notRemoveIds->orWhere(function($query) use($notRemove) { |
46 | $query->where('service', '=', $notRemove['service']) |
47 | ->where('task', '=', $notRemove['task']); |
48 | }); |
49 | } |
50 | $test = $notRemoveIds->pluck('id')->toArray(); |
51 | DockerTask::whereNotIn('id', $notRemoveIds->pluck('id')->toArray())->delete(); |
52 | } |
53 | |
54 | return $result; |
55 | } |
56 | } |