/** * @param Activity $activity * @param OutputInterface $output * @param bool $poll * @param float|int $interval A refresh interval (in seconds). */ protected function displayLog(Activity $activity, OutputInterface $output, $poll = true, $interval = 1) { $logger = function ($log) use($output) { $output->write($log); }; if (!$poll) { $logger($activity['log']); return; } $activity->wait(null, $logger, $interval); }
/** * Wait for a single activity to complete, and display the log. * * @param Activity $activity * @param OutputInterface $output * @param string $success * @param string $failure * * @return bool * True if the activity succeeded, false otherwise. */ public static function waitAndLog(Activity $activity, OutputInterface $output, $success = null, $failure = null) { $output->writeln('Waiting for the remote operation to complete...'); $activity->wait(null, function ($log) use($output) { $output->write(preg_replace('/^/m', ' ', $log)); }); switch ($activity['result']) { case 'success': if ($success !== null) { $output->writeln($success); } return true; case 'failure': if ($failure !== null) { $output->writeln($failure); } } return false; }
/** * Wait for a single activity to complete, and display the log continuously. * * @param Activity $activity * @param OutputInterface $output * @param string $success * @param string $failure * * @return bool * True if the activity succeeded, false otherwise. */ public static function waitAndLog(Activity $activity, OutputInterface $output, $success = null, $failure = null) { $output->writeln('Waiting for the activity <info>' . $activity->id . '</info> (' . $activity->getDescription() . "):"); // Initialize a progress bar which will show elapsed time and the // activity's state. $bar = new ProgressBar($output); $bar->setPlaceholderFormatterDefinition('state', function () use($activity) { return self::formatState($activity->state); }); $bar->setFormat(" [%bar%] %elapsed:6s% (%state%)"); $bar->start(); // Wait for the activity to complete. $activity->wait(function () use($bar) { $bar->advance(); }, function ($log) use($output, $bar) { // Clear the progress bar and ensure the current line is flushed. $bar->clear(); $output->write($output->isDecorated() ? "\n[1A" : "\n"); // Display the new log output, with an indent. $output->write(preg_replace('/^/m', ' ', $log)); // Display the progress bar again. $bar->advance(); }); $bar->finish(); $output->writeln(''); // Display the success or failure messages. switch ($activity['result']) { case Activity::RESULT_SUCCESS: $output->writeln($success ?: "Activity <info>{$activity->id}</info> succeeded"); return true; case Activity::RESULT_FAILURE: $output->writeln($failure ?: "Activity <error>{$activity->id}</error> failed"); return false; } return false; }