Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/daemon/query/PhabricatorDaemonLogQuery.php
12262 views
1
<?php
2
3
final class PhabricatorDaemonLogQuery
4
extends PhabricatorCursorPagedPolicyAwareQuery {
5
6
const STATUS_ALL = 'status-all';
7
const STATUS_ALIVE = 'status-alive';
8
const STATUS_RUNNING = 'status-running';
9
10
private $ids;
11
private $notIDs;
12
private $status = self::STATUS_ALL;
13
private $daemonClasses;
14
private $allowStatusWrites;
15
private $daemonIDs;
16
17
public static function getTimeUntilUnknown() {
18
return 3 * PhutilDaemonHandle::getHeartbeatEventFrequency();
19
}
20
21
public static function getTimeUntilDead() {
22
return 30 * PhutilDaemonHandle::getHeartbeatEventFrequency();
23
}
24
25
public function withIDs(array $ids) {
26
$this->ids = $ids;
27
return $this;
28
}
29
30
public function withoutIDs(array $ids) {
31
$this->notIDs = $ids;
32
return $this;
33
}
34
35
public function withStatus($status) {
36
$this->status = $status;
37
return $this;
38
}
39
40
public function withDaemonClasses(array $classes) {
41
$this->daemonClasses = $classes;
42
return $this;
43
}
44
45
public function setAllowStatusWrites($allow) {
46
$this->allowStatusWrites = $allow;
47
return $this;
48
}
49
50
public function withDaemonIDs(array $daemon_ids) {
51
$this->daemonIDs = $daemon_ids;
52
return $this;
53
}
54
55
protected function loadPage() {
56
$table = new PhabricatorDaemonLog();
57
$conn_r = $table->establishConnection('r');
58
59
$data = queryfx_all(
60
$conn_r,
61
'SELECT * FROM %T %Q %Q %Q',
62
$table->getTableName(),
63
$this->buildWhereClause($conn_r),
64
$this->buildOrderClause($conn_r),
65
$this->buildLimitClause($conn_r));
66
67
return $table->loadAllFromArray($data);
68
}
69
70
protected function willFilterPage(array $daemons) {
71
$unknown_delay = self::getTimeUntilUnknown();
72
$dead_delay = self::getTimeUntilDead();
73
74
$status_running = PhabricatorDaemonLog::STATUS_RUNNING;
75
$status_unknown = PhabricatorDaemonLog::STATUS_UNKNOWN;
76
$status_wait = PhabricatorDaemonLog::STATUS_WAIT;
77
$status_exiting = PhabricatorDaemonLog::STATUS_EXITING;
78
$status_exited = PhabricatorDaemonLog::STATUS_EXITED;
79
$status_dead = PhabricatorDaemonLog::STATUS_DEAD;
80
81
$filter = array_fuse($this->getStatusConstants());
82
83
foreach ($daemons as $key => $daemon) {
84
$status = $daemon->getStatus();
85
$seen = $daemon->getDateModified();
86
87
$is_running = ($status == $status_running) ||
88
($status == $status_wait) ||
89
($status == $status_exiting);
90
91
// If we haven't seen the daemon recently, downgrade its status to
92
// unknown.
93
$unknown_time = ($seen + $unknown_delay);
94
if ($is_running && ($unknown_time < time())) {
95
$status = $status_unknown;
96
}
97
98
// If the daemon hasn't been seen in quite a while, assume it is dead.
99
$dead_time = ($seen + $dead_delay);
100
if (($status == $status_unknown) && ($dead_time < time())) {
101
$status = $status_dead;
102
}
103
104
// If we changed the daemon's status, adjust it.
105
if ($status != $daemon->getStatus()) {
106
$daemon->setStatus($status);
107
108
// ...and write it, if we're in a context where that's reasonable.
109
if ($this->allowStatusWrites) {
110
$guard = AphrontWriteGuard::beginScopedUnguardedWrites();
111
$daemon->save();
112
unset($guard);
113
}
114
}
115
116
// If the daemon no longer matches the filter, get rid of it.
117
if ($filter) {
118
if (empty($filter[$daemon->getStatus()])) {
119
unset($daemons[$key]);
120
}
121
}
122
}
123
124
return $daemons;
125
}
126
127
protected function buildWhereClause(AphrontDatabaseConnection $conn) {
128
$where = array();
129
130
if ($this->ids !== null) {
131
$where[] = qsprintf(
132
$conn,
133
'id IN (%Ld)',
134
$this->ids);
135
}
136
137
if ($this->notIDs !== null) {
138
$where[] = qsprintf(
139
$conn,
140
'id NOT IN (%Ld)',
141
$this->notIDs);
142
}
143
144
if ($this->getStatusConstants()) {
145
$where[] = qsprintf(
146
$conn,
147
'status IN (%Ls)',
148
$this->getStatusConstants());
149
}
150
151
if ($this->daemonClasses !== null) {
152
$where[] = qsprintf(
153
$conn,
154
'daemon IN (%Ls)',
155
$this->daemonClasses);
156
}
157
158
if ($this->daemonIDs !== null) {
159
$where[] = qsprintf(
160
$conn,
161
'daemonID IN (%Ls)',
162
$this->daemonIDs);
163
}
164
165
$where[] = $this->buildPagingClause($conn);
166
167
return $this->formatWhereClause($conn, $where);
168
}
169
170
private function getStatusConstants() {
171
$status = $this->status;
172
switch ($status) {
173
case self::STATUS_ALL:
174
return array();
175
case self::STATUS_RUNNING:
176
return array(
177
PhabricatorDaemonLog::STATUS_RUNNING,
178
);
179
case self::STATUS_ALIVE:
180
return array(
181
PhabricatorDaemonLog::STATUS_UNKNOWN,
182
PhabricatorDaemonLog::STATUS_RUNNING,
183
PhabricatorDaemonLog::STATUS_WAIT,
184
PhabricatorDaemonLog::STATUS_EXITING,
185
);
186
default:
187
throw new Exception(pht('Unknown status "%s"!', $status));
188
}
189
}
190
191
public function getQueryApplicationClass() {
192
return 'PhabricatorDaemonsApplication';
193
}
194
195
}
196
197