1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole\Process; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Illuminate\Laravel; |
6
|
|
|
use Swoole\Http\Server; |
7
|
|
|
use Swoole\Process; |
8
|
|
|
|
9
|
|
|
trait CustomProcessTrait |
10
|
|
|
{ |
11
|
|
|
public function addCustomProcesses(Server $swoole, $processPrefix, array $processes, array $laravelConfig) |
12
|
|
|
{ |
13
|
|
|
$pidfile = dirname($swoole->setting['pid_file']) . '/laravels-custom-processes.pid'; |
14
|
|
|
if (file_exists($pidfile)) { |
15
|
|
|
unlink($pidfile); |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/**@var []CustomProcessInterface $processList */ |
19
|
|
|
$processList = []; |
20
|
|
|
foreach ($processes as $item) { |
21
|
|
|
if (is_string($item)) { |
22
|
|
|
// Backwards compatible |
23
|
|
|
Laravel::autoload($laravelConfig['root_path']); |
24
|
|
|
$process = $item; |
25
|
|
|
$redirect = $process::isRedirectStdinStdout(); |
26
|
|
|
$pipe = $process::getPipeType(); |
27
|
|
|
} else { |
28
|
|
|
if (empty($item['class'])) { |
29
|
|
|
throw new \InvalidArgumentException(sprintf( |
30
|
|
|
'process class name must be specified' |
31
|
|
|
) |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
if (isset($item['enable']) && !$item['enable']) { |
35
|
|
|
continue; |
36
|
|
|
} |
37
|
|
|
$process = $item['class']; |
38
|
|
|
$redirect = isset($item['redirect']) ? $item['redirect'] : false; |
39
|
|
|
$pipe = isset($item['pipe']) ? $item['pipe'] : 0; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
$processHandler = function (Process $worker) use ($pidfile, $swoole, $processPrefix, $process, $laravelConfig) { |
43
|
|
|
file_put_contents($pidfile, $worker->pid . "\n", FILE_APPEND | LOCK_EX); |
44
|
|
|
$this->initLaravel($laravelConfig, $swoole); |
|
|
|
|
45
|
|
|
if (!isset(class_implements($process)[CustomProcessInterface::class])) { |
46
|
|
|
throw new \InvalidArgumentException( |
47
|
|
|
sprintf( |
48
|
|
|
'%s must implement the interface %s', |
49
|
|
|
$process, |
50
|
|
|
CustomProcessInterface::class |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
$name = $process::getName() ?: 'custom'; |
55
|
|
|
$this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name)); |
|
|
|
|
56
|
|
|
|
57
|
|
|
Process::signal(SIGUSR1, function ($signo) use ($name, $process, $worker, $pidfile, $swoole) { |
|
|
|
|
58
|
|
|
$this->info(sprintf('Reloading the process %s [pid=%d].', $name, $worker->pid)); |
|
|
|
|
59
|
|
|
$process::onReload($swoole, $worker); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
$enableCoroutine = class_exists('Swoole\Coroutine'); |
63
|
|
|
$runProcess = function () use ($name, $process, $swoole, $worker, $enableCoroutine) { |
64
|
|
|
$maxTry = 10; |
65
|
|
|
$i = 0; |
66
|
|
|
do { |
67
|
|
|
$this->callWithCatchException([$process, 'callback'], [$swoole, $worker]); |
|
|
|
|
68
|
|
|
++$i; |
69
|
|
|
if ($enableCoroutine) { |
70
|
|
|
\Swoole\Coroutine::sleep(1); |
71
|
|
|
} else { |
72
|
|
|
sleep(1); |
73
|
|
|
} |
74
|
|
|
} while ($i < $maxTry); |
75
|
|
|
$this->error( |
|
|
|
|
76
|
|
|
sprintf( |
77
|
|
|
'The custom process "%s" reaches the maximum number of retries %d times, and will be restarted by the manager process.', |
78
|
|
|
$name, |
79
|
|
|
$maxTry |
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
}; |
83
|
|
|
$enableCoroutine ? go($runProcess) : $runProcess(); |
84
|
|
|
}; |
85
|
|
|
$customProcess = new Process( |
86
|
|
|
$processHandler, |
87
|
|
|
$redirect, |
88
|
|
|
$pipe |
89
|
|
|
); |
90
|
|
|
if ($swoole->addProcess($customProcess)) { |
|
|
|
|
91
|
|
|
$processList[] = $customProcess; |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
return $processList; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|