Passed
Push — master ( 338d1c...cf56cd )
by Biao
03:57
created

CollectorProcess::getDefinition()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 0
dl 0
loc 8
rs 10
c 0
b 0
f 0
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace Hhxsv5\LaravelS\Components\Prometheus;
4
5
use Hhxsv5\LaravelS\Components\Prometheus\Collectors\SwooleProcessCollector;
6
use Hhxsv5\LaravelS\Components\Prometheus\Collectors\SwooleStatsCollector;
7
use Hhxsv5\LaravelS\Components\Prometheus\Collectors\SystemCollector;
8
use Hhxsv5\LaravelS\Swoole\Process\CustomProcessInterface;
9
use Swoole\Http\Server;
10
use Swoole\Process;
11
use Swoole\Timer;
12
13
class CollectorProcess implements CustomProcessInterface
0 ignored issues
show
Coding Style introduced by
Missing doc comment for class CollectorProcess
Loading history...
14
{
15
    private static $timerId;
0 ignored issues
show
Coding Style introduced by
Private member variable "timerId" must be prefixed with an underscore
Loading history...
16
17
    public static function getDefinition()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function getDefinition()
Loading history...
18
    {
19
        return [
20
            'prometheus' => [
21
                'class'    => static::class,
22
                'redirect' => false,
23
                'pipe'     => 0,
24
                'enable'   => (bool)config('prometheus.enable', true),
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

24
                'enable'   => (bool)/** @scrutinizer ignore-call */ config('prometheus.enable', true),
Loading history...
25
            ],
26
        ];
27
    }
28
29
    public static function callback(Server $swoole, Process $process)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function callback()
Loading history...
30
    {
31
        /**@var SwooleProcessCollector $processCollector */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
32
        $processCollector = app(SwooleProcessCollector::class);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

32
        $processCollector = /** @scrutinizer ignore-call */ app(SwooleProcessCollector::class);
Loading history...
33
        /**@var SwooleStatsCollector $swooleStatsCollector */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
34
        $swooleStatsCollector = app(SwooleStatsCollector::class);
35
        /**@var SystemCollector $systemCollector */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
36
        $systemCollector = app(SystemCollector::class);
37
        $workerNum = $swoole->setting['worker_num'];
38
        $taskWorkerNum = isset($swoole->setting['task_worker_num']) ? $swoole->setting['task_worker_num'] : 0;
39
        $totalNum = $workerNum + $taskWorkerNum - 1;
40
        $workerIds = range(0, $totalNum);
41
        $runJob = function () use ($swoole, $workerIds, $processCollector, $swooleStatsCollector, $systemCollector) {
42
            // Collect the metrics of Swoole stats()
43
            $swooleStatsCollector->collect();
44
45
            // Collect the metrics of system
46
            $systemCollector->collect();
47
48
            // Collect the metrics of all workers
49
            foreach ($workerIds as $workerId) {
50
                $swoole->sendMessage($processCollector, $workerId);
51
            }
52
        };
53
54
        $interval = config('prometheus.collect_metrics_interval', 10) * 1000;
0 ignored issues
show
Bug introduced by
The function config was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

54
        $interval = /** @scrutinizer ignore-call */ config('prometheus.collect_metrics_interval', 10) * 1000;
Loading history...
55
        self::$timerId = Timer::tick($interval, $runJob);
56
    }
57
58
    public static function onReload(Server $swoole, Process $process)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onReload()
Loading history...
59
    {
60
        Timer::clear(self::$timerId);
61
    }
62
63
    public static function onStop(Server $swoole, Process $process)
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function onStop()
Loading history...
Unused Code introduced by
The parameter $swoole 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

63
    public static function onStop(/** @scrutinizer ignore-unused */ Server $swoole, Process $process)

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...
Unused Code introduced by
The parameter $process 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

63
    public static function onStop(Server $swoole, /** @scrutinizer ignore-unused */ Process $process)

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...
64
    {
65
        Timer::clear(self::$timerId);
66
    }
67
}