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; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
use yii\base\BaseObject; |
12
|
|
|
use yii\caching\Cache; |
13
|
|
|
use yii\db\Connection; |
14
|
|
|
use yii\di\Instance; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Env |
18
|
|
|
* |
19
|
|
|
* @author Roman Zhuravlev <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
class Env extends BaseObject |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Cache|array|string |
25
|
|
|
*/ |
26
|
|
|
public $cache = 'cache'; |
27
|
|
|
/** |
28
|
|
|
* @var Connection|array|string |
29
|
|
|
*/ |
30
|
|
|
public $db = 'db'; |
31
|
|
|
/** |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
public $pushTableName = '{{%queue_push}}'; |
35
|
|
|
/** |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
public $execTableName = '{{%queue_exec}}'; |
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
public $workerTableName = '{{%queue_worker}}'; |
43
|
|
|
/** |
44
|
|
|
* @var int |
45
|
|
|
*/ |
46
|
|
|
public $workerPingInterval = 15; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return static |
50
|
|
|
*/ |
51
|
|
|
public static function ensure() |
52
|
|
|
{ |
53
|
|
|
return Yii::$container->get(static::class); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @inheritdoc |
58
|
|
|
*/ |
59
|
|
|
public function init() |
60
|
|
|
{ |
61
|
|
|
parent::init(); |
62
|
|
|
$this->cache = Instance::ensure($this->cache, Cache::class); |
63
|
|
|
$this->db = Instance::ensure($this->db, Connection::class); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function canListenWorkerLoop() |
70
|
|
|
{ |
71
|
|
|
return !!$this->workerPingInterval; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getHost() |
78
|
|
|
{ |
79
|
|
|
if ($this->db->driverName === 'mysql') { |
80
|
|
|
$pair = $this->db |
81
|
|
|
->createCommand('SELECT `HOST` FROM `information_schema`.`PROCESSLIST` WHERE `ID` = CONNECTION_ID()') |
82
|
|
|
->queryScalar(); |
83
|
|
|
return substr($pair, 0, strrpos($pair, ':')); |
|
|
|
|
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->db->driverName === 'pgsql') { |
87
|
|
|
return $this->db |
|
|
|
|
88
|
|
|
->createCommand('SELECT inet_client_addr()') |
89
|
|
|
->queryScalar(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return '127.0.0.1'; // By default |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|