|
1
|
|
|
<?php |
|
2
|
|
|
|
|
|
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole\Process; |
|
4
|
|
|
|
|
5
|
|
|
use Swoole\Http\Server; |
|
6
|
|
|
use Swoole\Process; |
|
7
|
|
|
|
|
8
|
|
|
trait CustomProcessTrait |
|
|
|
|
|
|
9
|
|
|
{ |
|
10
|
|
|
private $customProcessPidFile = 'laravels-custom-processes.pid'; |
|
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
public function addCustomProcesses(Server $swoole, $processPrefix, array $processes, array $laravelConfig) |
|
|
|
|
|
|
13
|
|
|
{ |
|
14
|
|
|
$pidfile = dirname($swoole->setting['pid_file']) . '/' . $this->customProcessPidFile; |
|
15
|
|
|
if (file_exists($pidfile)) { |
|
16
|
|
|
unlink($pidfile); |
|
17
|
|
|
} |
|
18
|
|
|
|
|
19
|
|
|
/**@var []Process $processList */ |
|
|
|
|
|
|
20
|
|
|
$processList = []; |
|
21
|
|
|
foreach ($processes as $name => $item) { |
|
22
|
|
|
if (empty($item['class'])) { |
|
23
|
|
|
throw new \InvalidArgumentException(sprintf('The class of process %s must be specified', $name)); |
|
24
|
|
|
} |
|
25
|
|
|
if (isset($item['enable']) && !$item['enable']) { |
|
26
|
|
|
continue; |
|
27
|
|
|
} |
|
28
|
|
|
$processClass = $item['class']; |
|
29
|
|
|
$restartInterval = isset($item['restart_interval']) ? (int)$item['restart_interval'] : 5; |
|
30
|
|
|
$callback = function (Process $worker) use ($pidfile, $swoole, $processPrefix, $processClass, $restartInterval, $name, $laravelConfig) { |
|
31
|
|
|
file_put_contents($pidfile, $worker->pid . "\n", FILE_APPEND | LOCK_EX); |
|
32
|
|
|
$this->initLaravel($laravelConfig, $swoole); |
|
|
|
|
|
|
33
|
|
|
if (!isset(class_implements($processClass)[CustomProcessInterface::class])) { |
|
34
|
|
|
throw new \InvalidArgumentException( |
|
35
|
|
|
sprintf( |
|
36
|
|
|
'%s must implement the interface %s', |
|
37
|
|
|
$processClass, |
|
38
|
|
|
CustomProcessInterface::class |
|
39
|
|
|
) |
|
40
|
|
|
); |
|
41
|
|
|
} |
|
42
|
|
|
/**@var CustomProcessInterface $processClass */ |
|
|
|
|
|
|
43
|
|
|
$this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name)); |
|
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
Process::signal(SIGUSR1, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) { |
|
|
|
|
|
|
46
|
|
|
$this->info(sprintf('Reloading %s process[PID=%d].', $name, $worker->pid)); |
|
|
|
|
|
|
47
|
|
|
$processClass::onReload($swoole, $worker); |
|
48
|
|
|
}); |
|
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
if (method_exists($processClass, 'onStop')) { |
|
51
|
|
|
Process::signal(SIGTERM, function ($signo) use ($name, $processClass, $worker, $pidfile, $swoole) { |
|
|
|
|
|
|
52
|
|
|
$this->info(sprintf('Stopping %s process[PID=%d].', $name, $worker->pid)); |
|
53
|
|
|
$processClass::onStop($swoole, $worker); |
|
54
|
|
|
}); |
|
|
|
|
|
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
if (class_exists('Swoole\Runtime')) { |
|
58
|
|
|
\Swoole\Runtime::enableCoroutine(); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
$this->callWithCatchException([$processClass, 'callback'], [$swoole, $worker]); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
// Avoid frequent process creation |
|
64
|
|
|
if (class_exists('Swoole\Coroutine')) { |
|
65
|
|
|
\Swoole\Coroutine::sleep($restartInterval); |
|
66
|
|
|
} else { |
|
67
|
|
|
sleep($restartInterval); |
|
68
|
|
|
} |
|
69
|
|
|
}; |
|
70
|
|
|
|
|
71
|
|
|
if (isset($item['num']) && $item['num'] > 1) { // For multiple processes |
|
72
|
|
|
for ($i = 0; $i < $item['num']; $i++) { |
|
73
|
|
|
$process = $this->makeProcess($callback, $item); |
|
74
|
|
|
$swoole->addProcess($process); |
|
75
|
|
|
$processList[$name . $i] = $process; |
|
76
|
|
|
} |
|
77
|
|
|
} else { // For single process |
|
78
|
|
|
$process = $this->makeProcess($callback, $item); |
|
79
|
|
|
$swoole->addProcess($process); |
|
80
|
|
|
$processList[$name] = $process; |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
return $processList; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
|
|
|
|
|
87
|
|
|
* @param callable $callback |
|
|
|
|
|
|
88
|
|
|
* @param array $config |
|
|
|
|
|
|
89
|
|
|
* @return Process |
|
|
|
|
|
|
90
|
|
|
*/ |
|
91
|
|
|
public function makeProcess(callable $callback, array $config) |
|
92
|
|
|
{ |
|
93
|
|
|
$redirect = isset($config['redirect']) ? $config['redirect'] : false; |
|
94
|
|
|
$pipe = isset($config['pipe']) ? $config['pipe'] : 0; |
|
95
|
|
|
$process = version_compare(SWOOLE_VERSION, '4.3.0', '>=') |
|
96
|
|
|
? new Process($callback, $redirect, $pipe, class_exists('Swoole\Coroutine')) |
|
97
|
|
|
: new Process($callback, $redirect, $pipe); |
|
98
|
|
|
if (isset($config['queue'])) { |
|
99
|
|
|
if (empty($config['queue'])) { |
|
100
|
|
|
$process->useQueue(); |
|
101
|
|
|
} else { |
|
102
|
|
|
$msgKey = isset($config['queue']['msg_key']) ? $config['queue']['msg_key'] : 0; |
|
103
|
|
|
$mode = isset($config['queue']['mode']) ? $config['queue']['mode'] : 2; |
|
104
|
|
|
$capacity = isset($config['queue']['capacity']) ? $config['queue']['capacity'] : -1; |
|
105
|
|
|
$process->useQueue($msgKey, $mode, $capacity); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $process; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|