This repository was archived by the owner on Apr 27, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 127
Expand file tree
/
Copy pathActivityMonitor.php
More file actions
512 lines (452 loc) · 17.4 KB
/
Copy pathActivityMonitor.php
File metadata and controls
512 lines (452 loc) · 17.4 KB
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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
<?php
namespace Platformsh\Cli\Service;
use GuzzleHttp\Exception\BadResponseException;
use Platformsh\Client\Model\Activity;
use Platformsh\Client\Model\ActivityLog\LogItem;
use Platformsh\Client\Model\Project;
use Symfony\Component\Console\Helper\Helper;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Output\ConsoleOutputInterface;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
class ActivityMonitor
{
protected static $resultNames = [
Activity::RESULT_FAILURE => 'failure',
Activity::RESULT_SUCCESS => 'success',
];
protected static $stateNames = [
Activity::STATE_PENDING => 'pending',
Activity::STATE_COMPLETE => 'complete',
Activity::STATE_IN_PROGRESS => 'in progress',
Activity::STATE_CANCELLED => 'cancelled',
];
protected $output;
protected $config;
protected $api;
/**
* @param OutputInterface $output
* @param Config $config
* @param Api $api
*/
public function __construct(OutputInterface $output, Config $config, Api $api)
{
$this->output = $output;
$this->config = $config;
$this->api = $api;
}
/**
* @return \Symfony\Component\Console\Output\OutputInterface
*/
protected function getStdErr()
{
return $this->output instanceof ConsoleOutputInterface ? $this->output->getErrorOutput() : $this->output;
}
/**
* Indent a multi-line string.
*
* @param string $string
* @param string $prefix
*
* @return string
*/
protected function indent($string, $prefix = ' ')
{
return preg_replace('/^/m', $prefix, $string);
}
/**
* Wait for a single activity to complete, and display the log continuously.
*
* @param Activity $activity The activity.
* @param int $pollInterval The interval between refreshing the activity (seconds).
* @param bool|string $timestamps Whether to display timestamps (or pass in a date format).
* @param bool $context Whether to add a context message.
* @param OutputInterface|null $logOutput The output object for log messages (defaults to stderr).
*
* @return bool True if the activity succeeded, false otherwise.
*/
public function waitAndLog(Activity $activity, $pollInterval = 3, $timestamps = false, $context = true, OutputInterface $logOutput = null)
{
$stdErr = $this->getStdErr();
$logOutput = $logOutput ?: $stdErr;
if ($context) {
$stdErr->writeln(sprintf(
'Waiting for the activity <info>%s</info> (%s):',
$activity->id,
self::getFormattedDescription($activity)
));
$stdErr->writeln('');
}
// The progress bar will show elapsed time and the activity's state.
$bar = $this->newProgressBar($stdErr);
$overrideState = '';
$bar->setPlaceholderFormatterDefinition('state', function () use ($activity, &$overrideState) {
return $this->formatState($overrideState ?: $activity->state);
});
$startTime = $this->getStart($activity) ?: time();
$bar->setPlaceholderFormatterDefinition('elapsed', function () use ($startTime) {
return Helper::formatTime(time() - $startTime);
});
$bar->setFormat('[%bar%] %elapsed:6s% (%state%)');
// Set up cancellation for the activity on Ctrl+C.
if (\function_exists('\\pcntl_signal') && $activity->operationAvailable('cancel')) {
declare(ticks = 1);
$sigintReceived = false;
/** @noinspection PhpComposerExtensionStubsInspection */
\pcntl_signal(SIGINT, function () use ($activity, $stdErr, $bar, &$sigintReceived) {
if ($sigintReceived) {
exit(1);
}
$sigintReceived = true;
$bar->clear();
if ($this->cancel($activity, $stdErr)) {
exit(1);
}
$stdErr->writeln('');
$bar->advance();
});
$stdErr->writeln('Enter Ctrl+C once to cancel the activity (or twice to quit this command).');
}
$bar->start();
$logStream = $this->getLogStream($activity, $bar);
$bar->advance();
// Read the log while waiting for the activity to complete.
$lastRefresh = microtime(true);
$buffer = '';
while (!feof($logStream) || !$activity->isComplete()) {
// If $pollInterval has passed, or if there is nothing else left
// to do, then refresh the activity.
if (feof($logStream) || microtime(true) - $lastRefresh >= $pollInterval) {
$activity->refresh();
$overrideState = '';
$lastRefresh = microtime(true);
}
// Update the progress bar.
$bar->advance();
// Wait to see if a read will not block the stream, for up to .2
// seconds.
if (!$this->canRead($logStream, 200000)) {
continue;
}
// Parse the log.
$items = $this->parseLog($logStream, $buffer);
if (empty($items)) {
continue;
}
// If there is log output, assume the activity must be in progress.
if ($activity->state === Activity::STATE_PENDING) {
$overrideState = Activity::STATE_IN_PROGRESS;
}
// Format log items.
$formatted = $this->formatLog($items, $timestamps);
// Clear the progress bar and ensure the current line is flushed.
$bar->clear();
$stdErr->write($stdErr->isDecorated() ? "\n\033[1A" : "\n");
// Display the new log output.
$logOutput->write($formatted);
// Display the progress bar again.
$bar->advance();
}
$bar->finish();
$stdErr->writeln('');
// Display the success or failure messages.
switch ($activity['result']) {
case Activity::RESULT_SUCCESS:
$stdErr->writeln("Activity <info>{$activity->id}</info> succeeded");
return true;
case Activity::RESULT_FAILURE:
$stdErr->writeln("Activity <error>{$activity->id}</error> failed");
return false;
}
return false;
}
/**
* Attempts to cancel the activity, catching and printing errors.
*
* @param Activity $activity
* @param OutputInterface $stdErr
*
* @return bool
*/
private function cancel(Activity $activity, OutputInterface $stdErr)
{
if (!$activity->operationAvailable('cancel')) {
$stdErr->writeln('The activity cannot be cancelled.');
return false;
}
$stdErr->writeln('Cancelling the activity...');
try {
$activity->cancel();
} catch (\Exception $e) {
if ($e instanceof BadResponseException
&& $e->getResponse() && $e->getResponse()->getStatusCode() === 400
&& \strpos($e->getMessage(), 'cannot be cancelled in its current state') !== false) {
$activity->refresh();
$stdErr->writeln(\sprintf('The activity cannot be cancelled in its current state (<error>%s</error>).', $activity->state));
return false;
}
$stdErr->writeln(\sprintf('Failed to cancel the activity: <error>%s</error>', $e->getMessage()));
return false;
}
$stdErr->writeln('The activity was successfully cancelled.');
return true;
}
/**
* Reads the log stream and returns LogItem objects.
*
* @param resource $stream
* The stream.
* @param string &$buffer
* A string where a buffer can be stored between stream updates.
*
* @return LogItem[]
*/
private function parseLog($stream, &$buffer) {
$buffer .= stream_get_contents($stream);
$lastNewline = strrpos($buffer, "\n");
if ($lastNewline === false) {
return [];
}
$content = substr($buffer, 0, $lastNewline + 1);
$buffer = substr($buffer, $lastNewline + 1);
return LogItem::multipleFromJsonStream($content);
}
/**
* Waits to see if a stream can be read (if the read will not block).
*
* @param resource $stream The stream.
* @param int $microseconds A timeout in microseconds.
*
* @return bool
*/
private function canRead($stream, $microseconds) {
$readSet = [$stream];
$ignore = [];
return (bool) stream_select($readSet, $ignore, $ignore, 0, $microseconds);
}
/**
* Formats log items for display.
*
* @param LogItem[] $items
* The log items.
* @param bool|string $timestamps
* False for no timestamps, or a string date format or true to display timestamps
*
* @return string
*/
public function formatLog(array $items, $timestamps = false) {
$timestampFormat = false;
if ($timestamps !== false) {
$timestampFormat = $timestamps ?: $this->config->getWithDefault('application.date_format', 'Y-m-d H:i:s');
}
$formatItem = function (LogItem $item) use ($timestampFormat) {
if ($timestampFormat !== false) {
return '[' . $item->getTime()->format($timestampFormat) . '] '. $item->getMessage();
}
return $item->getMessage();
};
return implode('', array_map($formatItem, $items));
}
/**
* Wait for multiple activities to complete.
*
* A progress bar tracks the state of each activity. The activity log is
* only displayed at the end, if an activity failed.
*
* @param Activity[] $activities
* @param Project $project
*
* @return bool
* True if all activities succeed, false otherwise.
*/
public function waitMultiple(array $activities, Project $project)
{
$stdErr = $this->getStdErr();
$count = count($activities);
if ($count == 0) {
return true;
} elseif ($count === 1) {
return $this->waitAndLog(reset($activities));
}
$stdErr->writeln(sprintf('Waiting for %d activities...', $count));
// The progress bar will show elapsed time and all of the activities'
// states.
$bar = $this->newProgressBar($stdErr);
$states = [];
foreach ($activities as $activity) {
$state = $activity->state;
$states[$state] = isset($states[$state]) ? $states[$state] + 1 : 1;
}
$bar->setPlaceholderFormatterDefinition('states', function () use (&$states) {
$format = '';
foreach ($states as $state => $count) {
$format .= $count . ' ' . $this->formatState($state) . ', ';
}
return rtrim($format, ', ');
});
$bar->setFormat(' [%bar%] %elapsed:6s% (%states%)');
$bar->start();
// Get the most recent created date of each of the activities, as a Unix
// timestamp, so that they can be more efficiently refreshed.
$mostRecentTimestamp = 0;
foreach ($activities as $activity) {
$created = strtotime($activity->created_at);
$mostRecentTimestamp = $created > $mostRecentTimestamp ? $created : $mostRecentTimestamp;
}
// Wait for the activities to complete, polling (refreshing) all of them
// with a 1 second delay.
$complete = 0;
while ($complete < $count) {
sleep(1);
$states = [];
$complete = 0;
// Get a list of activities on the project. Any of our activities
// which are not contained in this list must be refreshed
// individually.
$projectActivities = $project->getActivities(0, null, $mostRecentTimestamp ?: null);
foreach ($activities as &$activity) {
$refreshed = false;
foreach ($projectActivities as $projectActivity) {
if ($projectActivity->id === $activity->id) {
$activity = $projectActivity;
$refreshed = true;
break;
}
}
if (!$refreshed && !$activity->isComplete()) {
$activity->refresh();
}
if ($activity->isComplete()) {
$complete++;
}
$state = $activity->state;
$states[$state] = isset($states[$state]) ? $states[$state] + 1 : 1;
}
$bar->advance();
}
$bar->finish();
$stdErr->writeln('');
// Display success or failure messages for each activity.
$success = true;
foreach ($activities as $activity) {
$description = self::getFormattedDescription($activity);
switch ($activity['result']) {
case Activity::RESULT_SUCCESS:
$stdErr->writeln(sprintf('Activity <info>%s</info> succeeded: %s', $activity->id, $description));
break;
case Activity::RESULT_FAILURE:
$success = false;
$stdErr->writeln(sprintf('Activity <error>%s</error> failed', $activity->id));
// If the activity failed, show the complete log.
$stdErr->writeln(' Description: ' . $description);
$stdErr->writeln(' Log:');
$stdErr->writeln($this->indent($this->formatLog($activity->readLog())));
break;
}
}
return $success;
}
/**
* Format a state name.
*
* @param string $state
*
* @return string
*/
public static function formatState($state)
{
return isset(self::$stateNames[$state]) ? self::$stateNames[$state] : $state;
}
/**
* Format a result.
*
* @param string $result
* @param bool $decorate
*
* @return string
*/
public static function formatResult($result, $decorate = true)
{
$name = isset(self::$resultNames[$result]) ? self::$resultNames[$result] : $result;
return $decorate && $result === Activity::RESULT_FAILURE
? '<error>' . $name . '</error>'
: $name;
}
/**
* Initialize a new progress bar.
*
* @param OutputInterface $output
*
* @return ProgressBar
*/
protected function newProgressBar(OutputInterface $output)
{
// If the console output is not decorated (i.e. it does not support
// ANSI), use NullOutput to suppress the progress bar entirely.
$progressOutput = $output->isDecorated() ? $output : new NullOutput();
return new ProgressBar($progressOutput);
}
/**
* Get the formatted description of an activity.
*
* @param \Platformsh\Client\Model\Activity $activity
* @param bool $withDecoration
*
* @return string
*/
public static function getFormattedDescription(Activity $activity, $withDecoration = true)
{
if (!$withDecoration) {
return $activity->getDescription(false);
}
$value = $activity->getDescription(true);
// Replace description HTML elements with Symfony Console decoration
// tags.
$value = preg_replace('@<[^/][^>]+>@', '<options=underscore>', $value);
$value = preg_replace('@</[^>]+>@', '</>', $value);
// Replace literal tags like "<info&;gt;" with escaped tags like
// "\<info>".
$value = preg_replace('@<(/?[a-z][a-z0-9,_=;-]*+)>@i', '\\\<$1>', $value);
// Decode other HTML entities.
$value = html_entity_decode($value, ENT_QUOTES, 'utf-8');
return $value;
}
/**
* @param Activity $activity
*
* @return false|int
*/
private function getStart(Activity $activity) {
return !empty($activity->started_at) ? strtotime($activity->started_at) : strtotime($activity->created_at);
}
/**
* Returns the activity log as a PHP stream resource.
*
* @param Activity $activity
* @param ProgressBar $bar
* Progress bar, updated when we retry.
*
* @return resource
*/
private function getLogStream(Activity $activity, ProgressBar $bar) {
$url = $activity->getLink('log');
// Try fetching the stream with an up to 10 second timeout per call,
// and a .5 second interval between calls, for up to 2 minutes.
$readTimeout = .5;
$stream = \fopen($url, 'r', false, $this->api->getStreamContext($readTimeout));
$interval = .5;
$start = \microtime(true);
while ($stream === false) {
if (\microtime(true) - $start > 120) {
throw new \RuntimeException('Failed to open activity log stream: ' . $url);
}
$bar->advance();
\usleep($interval * 1000000);
$bar->advance();
$readTimeout = $readTimeout >= 10 ? $readTimeout : $readTimeout + .5;
$stream = \fopen($url, 'r', false, $this->api->getStreamContext($readTimeout));
}
\stream_set_blocking($stream, 0);
return $stream;
}
}