You are here

public function CronRunner::runCron in CiviCRM Cron 8

Runs CiviCRM cron.

Parameters

string $key: Optional, the CiviCRM site key to utilize. Omit to use the site key from configuration.

Throws

\Exception If the cron run fails. Inspect the exception message for further details.

File

src/CronRunner.php, line 64

Class

CronRunner
Class CronRunner.

Namespace

Drupal\civicrm_cron

Code

public function runCron($key = NULL) {
  if (!$key) {
    $key = $this->config
      ->get('sitekey');
  }
  $options = [
    'absolute' => TRUE,
  ];
  $url = Url::fromRoute('civicrm_cron.passthrough', [], $options)
    ->toString();
  $response = NULL;
  try {
    $response = $this->client
      ->request('GET', $url, [
      'query' => [
        'key' => $key,
      ],
    ]);
  } catch (ClientException $e) {
    if ($e
      ->hasResponse()) {
      $response = $e
        ->getResponse();
    }
  }
  if (!$response) {
    throw new \Exception('Unknown error');
  }
  if (!$response
    ->getHeader('X-CiviCRM-Cron')) {
    throw new \Exception($response
      ->getBody());
  }
}