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