|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Resta\Worker; |
|
4
|
|
|
|
|
5
|
|
|
use Resta\Contracts\JobContracts; |
|
6
|
|
|
|
|
7
|
|
|
class SupervisorJob extends JobAbstract implements JobContracts |
|
8
|
|
|
{ |
|
9
|
|
|
/** |
|
10
|
|
|
* execute job |
|
11
|
|
|
* |
|
12
|
|
|
* @return mixed|void |
|
13
|
|
|
*/ |
|
14
|
|
|
public function execute() |
|
15
|
|
|
{ |
|
16
|
|
|
$this->isSupervisorRunning(); |
|
17
|
|
|
|
|
18
|
|
|
$this->{$this->jobProcessor()}(); |
|
19
|
|
|
|
|
20
|
|
|
echo $this->getWorkersForSupervisor(); |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* start job |
|
25
|
|
|
* |
|
26
|
|
|
* @return mixed|void |
|
27
|
|
|
*/ |
|
28
|
|
|
public function start() |
|
29
|
|
|
{ |
|
30
|
|
|
$this->putConfigurationFile(); |
|
31
|
|
|
|
|
32
|
|
|
$this->reReadForSupervisor(); |
|
33
|
|
|
|
|
34
|
|
|
$this->updateForSupervisor(); |
|
35
|
|
|
|
|
36
|
|
|
$this->startWorkerForSupervisor(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* stop job |
|
41
|
|
|
* |
|
42
|
|
|
* @return mixed|void |
|
43
|
|
|
*/ |
|
44
|
|
|
public function stop() |
|
45
|
|
|
{ |
|
46
|
|
|
$this->stopWorkerForSupervisor(); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* get status worker |
|
51
|
|
|
* |
|
52
|
|
|
* @return mixed|void |
|
53
|
|
|
*/ |
|
54
|
|
|
public function status() |
|
55
|
|
|
{ |
|
56
|
|
|
$this->getWorkersForSupervisor(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* cleans worker |
|
61
|
|
|
* |
|
62
|
|
|
* @return mixed|void |
|
63
|
|
|
*/ |
|
64
|
|
|
public function clear() |
|
65
|
|
|
{ |
|
66
|
|
|
$this->cleanWorkerForSupervisor(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* get workers for supervisor |
|
71
|
|
|
* |
|
72
|
|
|
* @return mixed|void |
|
73
|
|
|
*/ |
|
74
|
|
|
private function cleanWorkerForSupervisor() |
|
75
|
|
|
{ |
|
76
|
|
|
$this->process->command(config('supervisor.commands.remove').' '.$this->app->get('WORKER').''); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* get workers for supervisor |
|
81
|
|
|
* |
|
82
|
|
|
* @return mixed |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getWorkersForSupervisor() |
|
85
|
|
|
{ |
|
86
|
|
|
$list = []; |
|
87
|
|
|
|
|
88
|
|
|
$status = array_filter(explode("\n",$this->process->command(config('supervisor.commands.workers'))),'strlen'); |
|
|
|
|
|
|
89
|
|
|
|
|
90
|
|
|
foreach ($status as $item){ |
|
91
|
|
|
if(preg_match('@'.$this->app->get('PROJECT_NAME').'.*@is',$item,$array)){ |
|
92
|
|
|
$list[] = $item; |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
return implode(PHP_EOL,$list).''.PHP_EOL; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* check if the supervisor is or not running |
|
101
|
|
|
* |
|
102
|
|
|
* @return mixed|void |
|
103
|
|
|
*/ |
|
104
|
|
|
public function isSupervisorRunning() |
|
105
|
|
|
{ |
|
106
|
|
|
$supervisorStatus = $this->process->command(config('supervisor.commands.status')); |
|
107
|
|
|
|
|
108
|
|
|
if(false===$supervisorStatus){ |
|
109
|
|
|
$this->upSupervisor(); |
|
110
|
|
|
} |
|
111
|
|
|
return $this->process->command(config('supervisor.commands.status')); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* up service supervisor |
|
116
|
|
|
* |
|
117
|
|
|
* @return mixed|void |
|
118
|
|
|
*/ |
|
119
|
|
|
public function upSupervisor() |
|
120
|
|
|
{ |
|
121
|
|
|
return $this->process->command(config('supervisor.commands.up')); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* reread for supervisor |
|
126
|
|
|
* |
|
127
|
|
|
* @return mixed |
|
128
|
|
|
*/ |
|
129
|
|
|
public function reReadForSupervisor() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->process->command(config('supervisor.commands.reread')); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
/** |
|
135
|
|
|
* start Worker for supervisor |
|
136
|
|
|
* |
|
137
|
|
|
* @return mixed |
|
138
|
|
|
*/ |
|
139
|
|
|
public function startWorkerForSupervisor() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->process->command(config('supervisor.commands.start').' '.$this->app->get('WORKER').':*'); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* stop worker for supervisor |
|
146
|
|
|
* |
|
147
|
|
|
* @return mixed|void |
|
148
|
|
|
*/ |
|
149
|
|
|
public function stopWorkerForSupervisor() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->process->command(config('supervisor.commands.stop').' '.$this->app->get('WORKER').':*'); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* put configutation file for supervisor |
|
156
|
|
|
* |
|
157
|
|
|
* @return mixed|void |
|
158
|
|
|
*/ |
|
159
|
|
|
public function putConfigurationFile() |
|
160
|
|
|
{ |
|
161
|
|
|
$path = config('supervisor.path').'/'.$this->app->get('WORKER').'.conf'; |
|
162
|
|
|
|
|
163
|
|
|
if(files()->exists($path)===false){ |
|
164
|
|
|
files()->put($path,' |
|
165
|
|
|
[group:'.$this->app->get('PROJECT_NAME').'] |
|
166
|
|
|
[program:'.$this->app()->get('WORKER').'] |
|
167
|
|
|
process_name=%(program_name)s_%(process_num)02d |
|
168
|
|
|
command=php '.root.'/api worker start '.$this->app->get('PROJECT_NAME').' worker:'.$this->worker->getWorker().' apply:default |
|
|
|
|
|
|
169
|
|
|
autostart=true |
|
170
|
|
|
autorestart=true |
|
171
|
|
|
user=root |
|
172
|
|
|
numprocs=1 |
|
173
|
|
|
redirect_stderr=true |
|
174
|
|
|
stdout_logfile='.config('supervisor.log').'/worker.log |
|
175
|
|
|
'); |
|
176
|
|
|
} |
|
177
|
|
|
|
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* update for supervisor |
|
182
|
|
|
* |
|
183
|
|
|
* @return mixed |
|
184
|
|
|
*/ |
|
185
|
|
|
public function updateForSupervisor() |
|
186
|
|
|
{ |
|
187
|
|
|
return $this->process->command(config('supervisor.commands.update')); |
|
188
|
|
|
} |
|
189
|
|
|
} |