Platformsh\Cli\Util\ActivityUtil::waitAndLog PHP Method

waitAndLog() public static method

Wait for a single activity to complete, and display the log continuously.
public static waitAndLog ( Platformsh\Client\Model\Activity $activity, Symfony\Component\Console\Output\OutputInterface $output, string $success = null, string $failure = null ) : boolean
$activity Platformsh\Client\Model\Activity
$output Symfony\Component\Console\Output\OutputInterface
$success string
$failure string
return boolean 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" : "\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;
    }

Usage Example

 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $this->validateInput($input);
     $selectedEnvironment = $this->getSelectedEnvironment();
     $environmentId = $selectedEnvironment->id;
     if (!$selectedEnvironment->operationAvailable('backup')) {
         $this->stdErr->writeln("Operation not available: cannot create a snapshot of <error>{$environmentId}</error>");
         if ($selectedEnvironment->is_dirty) {
             $this->api()->clearEnvironmentsCache($selectedEnvironment->project);
         }
         return 1;
     }
     $activity = $selectedEnvironment->backup();
     $this->stdErr->writeln("Creating a snapshot of <info>{$environmentId}</info>");
     if (!$input->getOption('no-wait')) {
         $this->stdErr->writeln("Waiting for the snapshot to complete...");
         $success = ActivityUtil::waitAndLog($activity, $this->stdErr, "A snapshot of environment <info>{$environmentId}</info> has been created", "The snapshot failed");
         if (!$success) {
             return 1;
         }
     }
     if (!empty($activity['payload']['backup_name'])) {
         $name = $activity['payload']['backup_name'];
         $output->writeln("Snapshot name: <info>{$name}</info>");
     }
     return 0;
 }
All Usage Examples Of Platformsh\Cli\Util\ActivityUtil::waitAndLog