|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class PdoMonitor |
|
5
|
|
|
{ |
|
6
|
|
|
protected \PDO $db; |
|
7
|
|
|
|
|
8
|
|
|
protected string $entity = "rabbit_monitor"; |
|
9
|
|
|
|
|
10
|
|
|
protected string $consumerName; |
|
11
|
|
|
|
|
12
|
|
|
protected string $groupName; |
|
13
|
|
|
|
|
14
|
|
|
const STATUS_STOPPED = 0; |
|
15
|
|
|
|
|
16
|
|
|
const STATUS_RUNNING = 1; |
|
17
|
|
|
|
|
18
|
|
|
public function __construct( |
|
19
|
|
|
$table = "rabbit_monitor", |
|
20
|
|
|
$consumerName = null, |
|
21
|
|
|
$groupName = null |
|
22
|
|
|
) |
|
23
|
|
|
{ |
|
24
|
|
|
$this->entity = $table; |
|
25
|
|
|
$this->consumerName = !empty($consumerName) ? $consumerName : rand(0, 999); |
|
26
|
|
|
$this->groupName = !empty($groupName) ? $groupName : rand(0, 999); |
|
27
|
|
|
|
|
28
|
|
|
\WillRy\RabbitRun\Connections\ConnectPDO::config( |
|
29
|
|
|
"mysql", "db", "env_db", "root", "root", 3306 |
|
30
|
|
|
); |
|
31
|
|
|
|
|
32
|
|
|
$this->db = \WillRy\RabbitRun\Connections\ConnectPDO::getInstance(true); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function getWorkers(): array |
|
36
|
|
|
{ |
|
37
|
|
|
$stmt = $this->db->prepare("SELECT * FROM {$this->entity}"); |
|
38
|
|
|
$stmt->execute(); |
|
39
|
|
|
return $stmt->fetchAll(\PDO::FETCH_OBJ); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
public function pauseWorker(): bool |
|
43
|
|
|
{ |
|
44
|
|
|
$this->insertConsumerIfNotExists(PdoMonitor::STATUS_STOPPED); |
|
45
|
|
|
|
|
46
|
|
|
$stmt = $this->db->prepare( |
|
47
|
|
|
"UPDATE {$this->entity} SET status = :status, jobID = :jobID WHERE name = :name" |
|
48
|
|
|
); |
|
49
|
|
|
$stmt->bindValue('status', PdoMonitor::STATUS_STOPPED); |
|
50
|
|
|
$stmt->bindValue('jobID', null); |
|
51
|
|
|
$stmt->bindValue('name', $this->consumerName); |
|
52
|
|
|
$stmt->execute(); |
|
53
|
|
|
|
|
54
|
|
|
return true; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function startWorker(): bool |
|
58
|
|
|
{ |
|
59
|
|
|
$this->insertConsumerIfNotExists(PdoMonitor::STATUS_RUNNING); |
|
60
|
|
|
|
|
61
|
|
|
return true; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
public function setWorkerItem($identificador): bool |
|
65
|
|
|
{ |
|
66
|
|
|
$this->insertConsumerIfNotExists(PdoMonitor::STATUS_RUNNING); |
|
67
|
|
|
|
|
68
|
|
|
$stmt = $this->db->prepare( |
|
69
|
|
|
"UPDATE {$this->entity} SET status = :status, jobID = :jobID, modifiedAt = :modifiedAt WHERE name = :name" |
|
70
|
|
|
); |
|
71
|
|
|
$stmt->bindValue('status', PdoMonitor::STATUS_RUNNING); |
|
72
|
|
|
$stmt->bindValue('jobID', $identificador); |
|
73
|
|
|
$stmt->bindValue('name', $this->consumerName); |
|
74
|
|
|
$stmt->bindValue('modifiedAt', date('Y-m-d H:i:s')); |
|
75
|
|
|
$stmt->execute(); |
|
76
|
|
|
|
|
77
|
|
|
return true; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
public function workerIsRunning(): bool |
|
81
|
|
|
{ |
|
82
|
|
|
$stmt = $this->db->prepare("SELECT * FROM {$this->entity} WHERE name = :name"); |
|
83
|
|
|
$stmt->bindValue('name', $this->consumerName); |
|
84
|
|
|
$stmt->execute(); |
|
85
|
|
|
$row = $stmt->fetch(\PDO::FETCH_OBJ); |
|
86
|
|
|
|
|
87
|
|
|
if (!empty($row->status) && $row->status === PdoMonitor::STATUS_RUNNING) { |
|
88
|
|
|
return true; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return false; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function insertConsumerIfNotExists(int $status = PdoMonitor::STATUS_RUNNING) |
|
95
|
|
|
{ |
|
96
|
|
|
$stmt = $this->db->prepare("SELECT * FROM {$this->entity} WHERE name = :name"); |
|
97
|
|
|
$stmt->bindValue('name', $this->consumerName); |
|
98
|
|
|
$stmt->execute(); |
|
99
|
|
|
$row = $stmt->fetch(\PDO::FETCH_OBJ); |
|
100
|
|
|
|
|
101
|
|
|
if (empty($row)) { |
|
102
|
|
|
$stmt = $this->db->prepare( |
|
103
|
|
|
"INSERT INTO {$this->entity}(name, status, jobID, groupName) VALUES(:name, :status, :jobID, :groupName)" |
|
104
|
|
|
); |
|
105
|
|
|
$stmt->bindValue('name', $this->consumerName); |
|
106
|
|
|
$stmt->bindValue('status', $status); |
|
107
|
|
|
$stmt->bindValue('jobID', null); |
|
108
|
|
|
$stmt->bindValue('groupName', $this->groupName); |
|
109
|
|
|
$stmt->execute(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return true; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
} |
|
116
|
|
|
|