class CronRunner in CiviCRM Cron 8
Class CronRunner.
Runs the cron.
Hierarchy
- class \Drupal\civicrm_cron\CronRunner
Expanded class hierarchy of CronRunner
1 file declares its use of CronRunner
- SettingsForm.php in src/
Form/ SettingsForm.php
1 string reference to 'CronRunner'
1 service uses CronRunner
File
- src/
CronRunner.php, line 16
Namespace
Drupal\civicrm_cronView source
class CronRunner {
/**
* CiviCRM service.
*
* @var \Drupal\civicrm\Civicrm
*/
protected $civicrm;
/**
* HTTP client.
*
* @var \GuzzleHttp\ClientInterface
*/
protected $client;
/**
* CiviCRM cron settings.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected $config;
/**
* CronRunner constructor.
*
* @param \Drupal\civicrm\Civicrm $civicrm
* CiviCRM service.
* @param \Drupal\Core\Config\ConfigFactoryInterface $configFactory
* Config factory service.
* @param \GuzzleHttp\ClientInterface $client
* HTTP client service.
*/
public function __construct(Civicrm $civicrm, ConfigFactoryInterface $configFactory, ClientInterface $client) {
$this->config = $configFactory
->get('civicrm_cron.settings');
$this->client = $client;
}
/**
* Runs CiviCRM cron.
*
* @param 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.
*/
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());
}
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CronRunner:: |
protected | property | CiviCRM service. | |
CronRunner:: |
protected | property | HTTP client. | |
CronRunner:: |
protected | property | CiviCRM cron settings. | |
CronRunner:: |
public | function | Runs CiviCRM cron. | |
CronRunner:: |
public | function | CronRunner constructor. |