WorkerQuery::one()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 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
            ->leftJoin(['push' => PushRecord::tableName()], '{{push}}.[[id]] = {{exec}}.[[push_id]]')
68
            ->andWhere([
69
                'or',
70
                ['>', 'worker.pinged_at', time() - $this->env->workerPingInterval - 5],
71
                [
72
                    'and',
73
                    ['is not', 'worker.last_exec_id', null],
74
                    ['exec.finished_at' => null],
75
                ],
76
            ]);
77
    }
78
79
    /**
80
     * @inheritdoc
81
     * @return WorkerRecord[]|array
82
     */
83
    public function all($db = null)
84
    {
85
        return parent::all($db);
86
    }
87
88
    /**
89
     * @inheritdoc
90
     * @return WorkerRecord|array|null
91
     */
92
    public function one($db = null)
93
    {
94
        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 array|boolean.
Loading history...
95
    }
96
}
97