1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Hhxsv5\LaravelS\Swoole\Traits; |
4
|
|
|
|
5
|
|
|
use Hhxsv5\LaravelS\Illuminate\Laravel; |
6
|
|
|
use Hhxsv5\LaravelS\Swoole\Process\CustomProcessInterface; |
7
|
|
|
|
8
|
|
|
trait CustomProcessTrait |
9
|
|
|
{ |
10
|
|
|
use ProcessTitleTrait; |
11
|
|
|
use LogTrait; |
12
|
|
|
|
13
|
|
|
public function addCustomProcesses(\swoole_server $swoole, $processPrefix, array $processes, array $laravelConfig) |
14
|
|
|
{ |
15
|
|
|
Laravel::autoload($laravelConfig['root_path']); |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var []CustomProcessInterface $processList |
19
|
|
|
*/ |
20
|
|
|
$processList = []; |
21
|
|
|
foreach ($processes as $process) { |
22
|
|
|
if (!isset(class_implements($process)[CustomProcessInterface::class])) { |
23
|
|
|
throw new \Exception(sprintf( |
24
|
|
|
'%s must implement the interface %s', |
25
|
|
|
$process, |
26
|
|
|
CustomProcessInterface::class |
27
|
|
|
) |
28
|
|
|
); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$processHandler = function () use ($swoole, $processPrefix, $process, $laravelConfig) { |
32
|
|
|
// Inject the global variables |
33
|
|
|
$_SERVER = $laravelConfig['_SERVER']; |
34
|
|
|
$_ENV = $laravelConfig['_ENV']; |
35
|
|
|
|
36
|
|
|
$name = $process::getName() ?: 'custom'; |
37
|
|
|
$this->setProcessTitle(sprintf('%s laravels: %s process', $processPrefix, $name)); |
38
|
|
|
$this->initLaravel($laravelConfig, $swoole); |
|
|
|
|
39
|
|
|
while (true) { |
40
|
|
|
$this->callWithCatchException(function () use ($process, $swoole) { |
41
|
|
|
return $process::callback($swoole); |
42
|
|
|
}); |
43
|
|
|
sleep(1); |
44
|
|
|
} |
45
|
|
|
}; |
46
|
|
|
$customProcess = new \swoole_process( |
47
|
|
|
$processHandler, |
48
|
|
|
$process::isRedirectStdinStdout(), |
49
|
|
|
$process::getPipeType() |
50
|
|
|
); |
51
|
|
|
if ($swoole->addProcess($customProcess)) { |
|
|
|
|
52
|
|
|
$processList[] = $customProcess; |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
return $processList; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|