You are here

function hosting_process in Hosting 7.4

Run a command, sending output to drush logs in real time.

The Symfony\Component\Process\Process Object is used to run this command. After implementing provision_process(), you can get the Process result object via drush context:

$process = drush_get_context('provision_process_result'); print $process->getExitCode();

Parameters

string|array $command_input: The command to run

null $cwd: The directory to run the command in.

string $label: A string to display above the command block in the front-end.

array $env: A list of environment variables to set for the process.

bool $log_output: Whether or not to send output to drush_log in real time.

null $error_message: The error message to show after a failure. Defaults to NULL because the UI turning red and the error output is usually enough.

bool $throw_drush_error: Whether or not to throw a drush error if the process fails. Defaults to TRUE.

Return value

string|void The output or error output of the command.

File

./task.hosting.inc, line 36
Drush include for the Hosting module's hosting task command.

Code

function hosting_process($command, $cwd = null, $label = 'Process', $env = array(), $log_output = TRUE, $error_message = NULL, $throw_drush_error = TRUE, $log_type = 'p_info') {
  if (empty($command)) {
    return;
  }

  // @TODO: new Process() below isn't accepting an array.
  if (is_array($command)) {
    $command = implode(' ', $command);
  }

  // Merge in env vars, inheriting the CLI's
  if (is_array($env)) {
    $env = array_merge($_SERVER, $env);
  }
  else {
    $env = $_SERVER;
  }

  // Make sure colors always come through
  $env['TERM'] = 'xterm';
  $process = new \Symfony\Component\Process\Process(escapeshellcmd($command), $cwd, $env);
  $process
    ->setTimeout(NULL);
  if ($log_output) {
    drush_log("[{$label}] {$command}", 'p_command');
    $exit_code = $process
      ->run(function ($type, $buffer) use ($log_type) {
      drush_log($buffer, $log_type);
    });
  }
  else {
    $exit_code = $process
      ->run();
  }

  // Save the Provision Process object to Drush Context so that implementors can access the full object.
  drush_set_context('provision_process_result', $process);
  drush_log("Provision process command exited with {$exit_code}", 'debug');

  // check exit code
  if ($exit_code === 0) {
    if ($log_output) {
      drush_log('', 'p_ok');
    }
    return $process
      ->getOutput();
  }
  else {
    if ($log_output) {
      drush_log('', 'p_error');
    }
    if ($throw_drush_error) {
      drush_set_error('PROVISION_PROCESS_ERROR', !empty($error_message) ? $error_message : $process
        ->getErrorOutput());
    }
    return $process
      ->getErrorOutput();
  }
}