Completed
Push — master ( 1674bd...a3e9f2 )
by Biao
03:31
created

Process::getDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hhxsv5\LaravelS\Components\Apollo;
4
5
use Hhxsv5\LaravelS\Swoole\Coroutine\Context;
6
use Hhxsv5\LaravelS\Swoole\Process\CustomProcessInterface;
7
use Swoole\Coroutine;
8
use Swoole\Http\Server;
9
use Swoole\Process as SwooleProcess;
10
11
class Process implements CustomProcessInterface
12
{
13
    /**@var Client $apollo */
14
    protected static $apollo;
15
16
    public static function getDefinition()
17
    {
18
        return [
19
            'apollo' => [
20
                'class'    => self::class,
21
                'redirect' => false,
22
                'pipe'     => 0,
23
            ],
24
        ];
25
    }
26
27
    public static function callback(Server $swoole, SwooleProcess $process)
28
    {
29
        $filename = base_path('.env');
30
        $env = getenv('LARAVELS_ENV');
31
        if ($env) {
32
            $filename .= '.' . $env;
33
        }
34
35
        self::$apollo = Client::createFromEnv();
36
        self::$apollo->startWatchNotification(function (array $notifications) use ($process, $filename) {
0 ignored issues
show
Unused Code introduced by
The parameter $notifications is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

36
        self::$apollo->startWatchNotification(function (/** @scrutinizer ignore-unused */ array $notifications) use ($process, $filename) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
37
            $configs = self::$apollo->pullAllAndSave($filename);
38
            app('log')->info('[ApolloProcess] Pull all configurations', $configs);
39
            $process->exec(PHP_BINARY, [base_path('bin/laravels'), 'reload']);
40
            if (Context::inCoroutine()) {
41
                Coroutine::sleep(5);
42
            } else {
43
                sleep(5);
44
            }
45
        });
46
    }
47
48
    public static function onReload(Server $swoole, SwooleProcess $process)
49
    {
50
        // Stop the process...
51
        self::$apollo->stopWatchNotification();
52
    }
53
}