Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 39 |
WithCronTable | |
0.00% |
0 / 1 |
|
0.00% |
0 / 8 |
182 | |
0.00% |
0 / 39 |
handle | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 13 |
|||
getCron | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 6 |
|||
createCron | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 6 |
|||
hasToRun | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
|||
cronStart | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
cronStop | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
release | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
shutdownService | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 7 |
1 | <?php |
2 | |
3 | namespace Qmp\Laravel\CommandsLaravel\Middleware\Library\Job; |
4 | |
5 | use Illuminate\Support\Str; |
6 | use Qmp\Laravel\CommandsLaravel\Jobs\JobWithCronTable; |
7 | use \Qmp\Laravel\MicroService\Client\Tools\Request as ClientRequest; |
8 | use Qmp\Laravel\CommandsLaravel\Middleware\Core\Middleware; |
9 | use Qmp\Laravel\CommandsLaravel\Models\Cron; |
10 | use Qmp\Laravel\MicroService\Client\Client; |
11 | |
12 | class WithCronTable extends Middleware |
13 | { |
14 | /** |
15 | * Undocumented variable |
16 | * |
17 | * @var [type] |
18 | */ |
19 | protected $cron; |
20 | |
21 | /** |
22 | * Undocumented function |
23 | * |
24 | * @param JobWithCronTable $IOinterface |
25 | * @param Closure] $next |
26 | * @return void |
27 | */ |
28 | public function handle($IOinterface, $next) |
29 | { |
30 | $this->setInterface($IOinterface) |
31 | ->getCron() |
32 | ->cronStart(); |
33 | |
34 | if (!$this->hasToRun()) { |
35 | $this->info('SCALE SERVICE TO 0'); |
36 | $this->info('Relaese Job'); |
37 | |
38 | $this->release(0) |
39 | ->shutdownService() |
40 | ->cronStop(); |
41 | |
42 | |
43 | sleep(60); |
44 | return false; |
45 | } |
46 | |
47 | $result = $next($IOinterface); |
48 | return $result; |
49 | } |
50 | |
51 | /** |
52 | * Undocumented function |
53 | * |
54 | * @return self |
55 | */ |
56 | protected function getCron(): self |
57 | { |
58 | $name = $this->getInterface()->getName(); |
59 | $cron = Cron::where('name', $name)->first(); |
60 | |
61 | if ($cron === null) { |
62 | $cron = $this->createCron($name); |
63 | } |
64 | |
65 | $this->cron = $cron; |
66 | |
67 | return $this; |
68 | } |
69 | |
70 | /** |
71 | * Undocumented function |
72 | * |
73 | * @param [type] $name |
74 | * @return Cron |
75 | */ |
76 | protected function createCron($name) |
77 | { |
78 | return Cron::create([ |
79 | 'name' => $name, |
80 | 'prod' => env('PROD'), |
81 | 'service' => $this->serviceName, |
82 | 'type' => 'queue', |
83 | 'active' => 0 |
84 | ]); |
85 | } |
86 | |
87 | /** |
88 | * Undocumented function |
89 | * |
90 | * @return boolean |
91 | */ |
92 | protected function hasToRun(): bool |
93 | { |
94 | return $this->cron->active && !$this->cron->pause && $this->cron->prod == env('PROD'); |
95 | } |
96 | |
97 | |
98 | /** |
99 | * Undocumented function |
100 | * |
101 | * @return void |
102 | */ |
103 | protected function cronStart(): void |
104 | { |
105 | $this->cron->update(['running' => true]); |
106 | } |
107 | |
108 | |
109 | /** |
110 | * Undocumented function |
111 | * |
112 | * @return void |
113 | */ |
114 | protected function cronStop(): void |
115 | { |
116 | $this->cron->update(['running' => false]); |
117 | } |
118 | |
119 | /** |
120 | * Undocumented function |
121 | * |
122 | * @param [type] $seconds |
123 | * @return self |
124 | */ |
125 | protected function release($seconds): self |
126 | { |
127 | $this->getInterface()->release($seconds); |
128 | |
129 | return $this; |
130 | } |
131 | |
132 | /** |
133 | * Undocumented function |
134 | * |
135 | * @return self |
136 | */ |
137 | protected function shutdownService(): self |
138 | { |
139 | $this->info("Scale service to 0 : " . $this->cron->service ?? $this->serviceName); |
140 | |
141 | $clientRequest = ClientRequest::createObject('service_docker_scale', "services-scale", [ |
142 | 'body' => [ |
143 | 'services' => [$this->cron->service ?? $this->serviceName => 0] |
144 | ] |
145 | ]); |
146 | |
147 | $response = Client::systemSend('post', $clientRequest); |
148 | |
149 | if (!Str::startsWith($response->code, 2)) { |
150 | throw new \Exception('unable to scale service '); |
151 | } |
152 | |
153 | return $this; |
154 | } |
155 | } |