-
Notifications
You must be signed in to change notification settings - Fork 664
/
Copy pathProvisioningPlan.php
191 lines (168 loc) · 5.04 KB
/
ProvisioningPlan.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
namespace Laravel\Horizon;
use Exception;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laravel\Horizon\Contracts\HorizonCommandQueue;
use Laravel\Horizon\Events\MasterSupervisorDeployed;
use Laravel\Horizon\MasterSupervisorCommands\AddSupervisor;
class ProvisioningPlan
{
/**
* The master supervisor's name.
*
* @var string
*/
public $master;
/**
* The raw provisioning plan.
*
* @var array
*/
public $plan;
/**
* The parsed provisioning plan.
*
* @var array
*/
public $parsed;
/**
* Create a new provisioning plan instance.
*
* @param string $master
* @param array $plan
* @param array $defaults
* @return void
*/
public function __construct($master, array $plan, array $defaults = [])
{
$this->plan = $this->applyDefaultOptions($plan, $defaults);
$this->master = $master;
$this->parsed = $this->toSupervisorOptions();
}
/**
* Get the current provisioning plan.
*
* @param string $master
* @return static
*/
public static function get($master)
{
return new static($master, config('horizon.environments'), config('horizon.defaults', []));
}
/**
* Apply the default supervisor options to each environment.
*
* @param array $plan
* @param array $defaults
* @return array
*/
protected function applyDefaultOptions(array $plan, array $defaults = [])
{
return collect($plan)->map(function ($plan) use ($defaults) {
return array_replace_recursive($defaults, $plan);
})->all();
}
/**
* Get all of the defined environments for the provisioning plan.
*
* @return array
*/
public function environments()
{
return array_keys($this->parsed);
}
/**
* Determine if the provisioning plan has a given environment.
*
* @param string $environment
* @return bool
*/
public function hasEnvironment($environment)
{
return array_key_exists($environment, $this->parsed);
}
/**
* Deploy a provisioning plan to the current machine.
*
* @param string $environment
* @return void
*/
public function deploy($environment)
{
$supervisors = collect($this->parsed)->first(function ($_, $name) use ($environment) {
return Str::is($name, $environment);
});
if (empty($supervisors)) {
return;
}
foreach ($supervisors as $supervisor => $options) {
$this->add($options);
}
event(new MasterSupervisorDeployed($this->master));
}
/**
* Add a supervisor with the given options.
*
* @param \Laravel\Horizon\SupervisorOptions $options
* @return void
*/
protected function add(SupervisorOptions $options)
{
app(HorizonCommandQueue::class)->push(
MasterSupervisor::commandQueueFor($this->master),
AddSupervisor::class,
$options->toArray()
);
}
/**
* Get the SupervisorOptions for a given environment and supervisor.
*
* @param string $environment
* @param string $supervisor
* @return mixed
*/
public function optionsFor($environment, $supervisor)
{
if (isset($this->parsed[$environment]) && isset($this->parsed[$environment][$supervisor])) {
return $this->parsed[$environment][$supervisor];
}
}
/**
* Convert the provisioning plan into an array of SupervisorOptions.
*
* @return array
*/
public function toSupervisorOptions()
{
return collect($this->plan)->mapWithKeys(function ($plan, $environment) {
return [$environment => collect($plan)->mapWithKeys(function ($options, $supervisor) {
return [$supervisor => $this->convert($supervisor, $options)];
})];
})->all();
}
/**
* Convert the given array of options into a SupervisorOptions instance.
*
* @param string $supervisor
* @param array $options
* @return \Laravel\Horizon\SupervisorOptions
*/
protected function convert($supervisor, $options)
{
$options = collect($options)->mapWithKeys(function ($value, $key) {
$key = $key === 'tries' ? 'max_tries' : $key;
$key = $key === 'processes' ? 'max_processes' : $key;
$value = $key === 'queue' && is_array($value) ? implode(',', $value) : $value;
$value = $key === 'backoff' && is_array($value) ? implode(',', $value) : $value;
return [Str::camel($key) => $value];
})->all();
if (isset($options['minProcesses']) && $options['minProcesses'] < 1) {
throw new Exception("The value of [{$supervisor}.minProcesses] must be greater than 0.");
}
$options['parentId'] = getmypid();
return SupervisorOptions::fromArray(
Arr::add($options, 'name', $this->master.":{$supervisor}")
);
}
}