Passed
Push — master ( b313ea...47c9db )
by Roman
03:00
created

WorkerQuery   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 72
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 72
rs 10
c 0
b 0
f 0
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A one() 0 3 1
A active() 0 9 1
A init() 0 4 1
A all() 0 3 1
A byEvent() 0 5 1
1
<?php
2
/**
3
 * @link https://github.com/zhuravljov/yii2-queue-monitor
4
 * @copyright Copyright (c) 2017 Roman Zhuravlev
5
 * @license http://opensource.org/licenses/BSD-3-Clause
6
 */
7
8
namespace zhuravljov\yii\queue\monitor\records;
9
10
use yii\db\ActiveQuery;
11
use zhuravljov\yii\queue\monitor\Env;
12
13
/**
14
 * Worker Query
15
 *
16
 * @author Roman Zhuravlev <[email protected]>
17
 */
18
class WorkerQuery extends ActiveQuery
19
{
20
    /**
21
     * @var Env
22
     */
23
    private $env;
24
25
    /**
26
     * @param string $modelClass
27
     * @param Env $env
28
     * @param array $config
29
     * @inheritdoc
30
     */
31
    public function __construct($modelClass, Env $env, array $config = [])
32
    {
33
        $this->env = $env;
34
        parent::__construct($modelClass, $config);
35
    }
36
37
    /**
38
     * @inheritdoc
39
     */
40
    public function init()
41
    {
42
        parent::init();
43
        $this->alias('worker');
44
    }
45
46
    /**
47
     * @param string $host
48
     * @param int $pid
49
     * @return $this
50
     */
51
    public function byEvent($host, $pid)
52
    {
53
        return $this->andWhere([
54
            'worker.host' => $host,
55
            'worker.pid' => $pid,
56
        ]);
57
    }
58
59
    /**
60
     * @return $this
61
     */
62
    public function active()
63
    {
64
        return $this
65
            ->andWhere(['worker.finished_at' => null])
66
            ->leftJoin(['exec' => ExecRecord::tableName()], '{{exec}}.[[id]] = {{worker}}.[[last_exec_id]]')
67
            ->andWhere([
68
                'or',
69
                ['>', 'worker.pinged_at', time() - $this->env->workerPingInterval - 5],
70
                ['exec.finished_at' => null],
71
            ]);
72
    }
73
74
    /**
75
     * @inheritdoc
76
     * @return WorkerRecord[]|array
77
     */
78
    public function all($db = null)
79
    {
80
        return parent::all($db);
81
    }
82
83
    /**
84
     * @inheritdoc
85
     * @return WorkerRecord|array|null
86
     */
87
    public function one($db = null)
88
    {
89
        return parent::one($db);
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::one($db) also could return the type yii\db\ActiveRecord which is incompatible with the return type mandated by yii\db\QueryInterface::one() of boolean|array.
Loading history...
90
    }
91
}
92