1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MadWeb\Initializer\Jobs\Supervisor; |
4
|
|
|
|
5
|
|
|
use Illuminate\Bus\Queueable; |
6
|
|
|
use Illuminate\Foundation\Bus\Dispatchable; |
7
|
|
|
|
8
|
|
|
abstract class MakeSupervisorConfig |
9
|
|
|
{ |
10
|
|
|
use Dispatchable, Queueable; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Supervisor config folder path. |
14
|
|
|
*/ |
15
|
|
|
protected $path; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Supervisor configuration name. |
19
|
|
|
*/ |
20
|
|
|
protected $fileName; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Supervisor config parameters. |
24
|
|
|
*/ |
25
|
|
|
protected $params; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Name of the supervisor process. |
29
|
|
|
*/ |
30
|
|
|
protected $processName = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Create a new job instance. |
34
|
|
|
*/ |
35
|
12 |
|
public function __construct(array $params = [], string $fileName = '', string $path = '/etc/supervisor/conf.d/') |
36
|
|
|
{ |
37
|
12 |
|
$this->path = $path; |
38
|
12 |
|
$this->fileName = $fileName ?: $this->configName().'.conf'; |
39
|
12 |
|
$this->params = $params; |
40
|
12 |
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Execute the job. |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
12 |
|
public function handle() |
48
|
|
|
{ |
49
|
12 |
|
file_put_contents( |
50
|
12 |
|
$this->path.$this->fileName, |
51
|
12 |
|
$this->makeSupervisorConfig($this->processName, $this->params) |
52
|
|
|
); |
53
|
|
|
|
54
|
12 |
|
return 'Supervisor config file created. Path: '.$this->path.$this->fileName; |
55
|
|
|
} |
56
|
|
|
|
57
|
12 |
|
protected function makeSupervisorConfig(string $programName, array $data) |
58
|
|
|
{ |
59
|
|
|
$default_config = [ |
60
|
12 |
|
'process_name' => '%(program_name)s_%(process_num)02d', |
61
|
12 |
|
'directory' => base_path(), |
62
|
|
|
'autostart' => true, |
63
|
|
|
'autorestart' => true, |
64
|
12 |
|
'user' => get_current_user(), |
65
|
12 |
|
'numprocs' => 1, |
66
|
|
|
'redirect_stderr' => true, |
67
|
12 |
|
'stdout_logfile' => "{$this->getLogsPath()}/{$this->configName()}.log", |
68
|
|
|
]; |
69
|
12 |
|
$data = array_merge($default_config, $data); |
70
|
|
|
|
71
|
12 |
|
$app_name = str_slug($this->getApplicationName()); |
72
|
12 |
|
$config = "[program:$app_name-$programName]".PHP_EOL; |
73
|
|
|
|
74
|
12 |
|
foreach ($data as $key => $value) { |
75
|
12 |
|
if (is_bool($value)) { |
76
|
12 |
|
$value = $value ? 'true' : 'false'; |
77
|
|
|
} else { |
78
|
12 |
|
$value = (string) $value; |
79
|
|
|
} |
80
|
12 |
|
$config .= "$key=$value".PHP_EOL; |
81
|
|
|
} |
82
|
|
|
|
83
|
12 |
|
return $config; |
84
|
|
|
} |
85
|
|
|
|
86
|
12 |
|
protected function getLogsPath(): string |
87
|
|
|
{ |
88
|
12 |
|
return storage_path('logs'); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return string |
93
|
|
|
*/ |
94
|
12 |
|
protected function configName(): string |
95
|
|
|
{ |
96
|
12 |
|
return str_slug($this->getApplicationName().'-'.$this->processName); |
97
|
|
|
} |
98
|
|
|
|
99
|
12 |
|
protected function getApplicationName() |
100
|
|
|
{ |
101
|
12 |
|
return config('app.name'); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|