-
Notifications
You must be signed in to change notification settings - Fork 0
/
statushandler.inc
263 lines (239 loc) · 6.61 KB
/
statushandler.inc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
<?php
class StatusHandler {
/**
* Status constants.
*
* @var int
*/
const STOPPED = 'stopped';
const PROCESSING = 'processing';
const HIBERNATING = 'hibernating';
const REFORKING = 'reforking';
const STOPPING = 'stopping';
/**
* The daemon object.
*
* @var int
*/
protected $daemon;
/**
* The currently daemonized command.
*
* @var string
*/
public $daemonCommand;
public function getDaemonCommand() { return $this->daemonCommand; }
/**
* The timestamp of when the status was last written to file.
*
* @var int
*/
public $lastWrittenTime;
public function getLastWrittenTime() { return $this->lastWrittenTime; }
/**
* A formatted string of the current peak memory usage.
*
* @var string
*/
public $memoryPeakUsage;
/**
* The process ID.
*
* @var int
*/
public $pid;
public function getPid() { return $this->pid; }
/**
* An array of items that need processing.
*
* @var array
*/
public $processItems;
/**
* An array of timestamps of when a command was reforked, keyed by command.
*
* @var array
*/
public $reforks;
/**
* The current status.
*
* @var string
*/
public $status;
/**
* The path to the status file, containing current status and process id.
*
* @var string
*/
protected $statusFilename = '';
/**
* The timestamp when the status file was last read.
*
* @var int
*/
protected $statusLastRead = 0;
/**
* Duration in seconds that the status should reside in the static cache. When
* this expires, the status is re-read from the status file.
*
* @var int
*/
protected $statusTTL;
/**
* The timestamp of when the status was written to disk.
*
* @var int
*/
public $writtenTime;
public function getWrittenTime() { return $this->writtenTime; }
/**
* Constructor.
*
* @param DrushDaemon $daemon
* The DrushDaemon object.
* @param string $filename
* The filename for the status file.
*/
public function __construct(DrushDaemon $daemon, $filename) {
$this->daemon = $daemon;
$this->statusTTL = $daemon->getStatusTTL();
$this->statusFilename = $filename;
$this->detectStatus();
if ($this->daemon->isDaemon()) {
$this->daemonCommand = $this->daemon->getCommand();
if (function_exists('posix_getpid')) {
$this->pid = posix_getpid();
}
}
}
/**
* Detect the current status of this process.
*/
protected function detectStatus() {
if ($this->readStatusFile()) {
drush_log(dt('The status file has been read.'));
}
else {
$this->status = StatusHandler::STOPPED;
}
}
/**
* Retrieve the current status of the daemon process.
*/
public function getStatus($refresh = FALSE) {
if ($refresh || $this->statusNeedsRefresh()) {
$this->detectStatus();
}
return $this->status;
}
/**
* Determine if the status file needs to be re-read.
*/
public function statusNeedsRefresh() {
$now = time();
return ($now - $this->statusLastRead) >= $this->statusTTL;
}
/**
* Set the current status of the daemon process.
*
* @param int $status
* The status to be set.
* @param bool $log
* Whether to log this status change.
*/
public function setStatus($status, $log = TRUE) {
if ($this->status != $status) {
$this->status = $status;
// If we're reforking, store the reforked command.
if ($status == StatusHandler::REFORKING && isset($this->daemonCommand)) {
// Store the timestamp of this refork.
$this->reforks[$this->daemonCommand][] = time();
}
// Write the file, without re-reading from disk first.
$this->writeStatusFile(FALSE);
if ($log) {
$this->logStatus('ok', "The @drush-cmd daemon state has changed to `@status`.");
}
}
else {
drush_log(dt('The status is already @status', array('@status' => $status)), 'warning');
}
}
/**
* Write this object to disk.
*
* @param bool $refresh_status
* If this is set to TRUE, the status will be re-read from disk before writing.
*/
public function writeStatusFile($refresh_status = TRUE) {
if ($refresh_status) {
// Force a re-read of the status first, in case it has changed on disk.
$this->getStatus(TRUE);
}
$file = fopen($this->statusFilename, 'w');
if (isset($this->writtenTime)) {
$this->lastWrittenTime = $this->writtenTime;
}
$this->writtenTime = time();
fwrite($file, $this);
fclose($file);
drush_log(dt('The status file has been written.'));
}
/**
* Read the status file, and populate instance variables as needed. If instance
* variables have already been set, they will not be overwritten by the file,
* except for the case of status. We always want to read a fresh status in from
* the file, in case another process has queued this for stopping or restarting.
*
* @return bool
* TRUE if the file was successfully read.
*/
protected function readStatusFile() {
// Just to be sure the file status is correct, clear out the stat cache.
clearstatcache();
if (file_exists($this->statusFilename) && $json = file_get_contents($this->statusFilename)) {
$this->statusLastRead = time();
foreach ((array) json_decode($json, TRUE) as $key => $value) {
// The initial read will set up all these values, but we don't want to
// overwrite the variables with the ones on disk after the initial load,
// unless it's the status variable, of course. :)
if (!isset($this->$key) || $key == 'status') {
$this->$key = $value;
}
}
return TRUE;
}
return FALSE;
}
/**
* Log the current status to drush.
*
* @param string $type
* The type of message to be logged. Common types are 'warning', 'error',
* 'success' and 'notice'. Defaults to 'notice'.
* @param string $message
* An optional custom message to log to be passed to dt(). You can use the
* @status placeholder.
* @param array $t_args
* Custom placeholder arguments to be passed to dt().
*/
public function logStatus($type = 'notice', $message = NULL, $t_args = NULL) {
if (!isset($t_args)) {
$t_args = array(
'@status' => $this->getStatus(),
'@drush-cmd' => $this->daemon->getDrushCommand(),
);
}
if (!isset($message)) {
$message = 'The @drush-cmd daemon is currently @status.';
}
drush_log(dt($message, $t_args), $type);
}
/**
* Convert this object to json as a string.
*/
public function __toString() {
return json_encode($this);
}
}