class AutomatedCron in Drupal 9
Same name and namespace in other branches
- 8 core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php \Drupal\automated_cron\EventSubscriber\AutomatedCron
A subscriber running cron after a response is sent.
Hierarchy
- class \Drupal\automated_cron\EventSubscriber\AutomatedCron implements \Symfony\Component\EventDispatcher\EventSubscriberInterface
Expanded class hierarchy of AutomatedCron
1 string reference to 'AutomatedCron'
- automated_cron.services.yml in core/modules/ automated_cron/ automated_cron.services.yml 
- core/modules/automated_cron/automated_cron.services.yml
1 service uses AutomatedCron
- automated_cron.subscriber in core/modules/ automated_cron/ automated_cron.services.yml 
- Drupal\automated_cron\EventSubscriber\AutomatedCron
File
- core/modules/ automated_cron/ src/ EventSubscriber/ AutomatedCron.php, line 15 
Namespace
Drupal\automated_cron\EventSubscriberView source
class AutomatedCron implements EventSubscriberInterface {
  /**
   * The cron service.
   *
   * @var \Drupal\Core\CronInterface
   */
  protected $cron;
  /**
   * The cron configuration.
   *
   * @var \Drupal\Core\Config\Config
   */
  protected $config;
  /**
   * The state key value store.
   *
   * @var \Drupal\Core\State\StateInterface
   */
  protected $state;
  /**
   * Constructs a new automated cron runner.
   *
   * @param \Drupal\Core\CronInterface $cron
   *   The cron service.
   * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
   *   The config factory.
   * @param \Drupal\Core\State\StateInterface $state
   *   The state key-value store service.
   */
  public function __construct(CronInterface $cron, ConfigFactoryInterface $config_factory, StateInterface $state) {
    $this->cron = $cron;
    $this->config = $config_factory
      ->get('automated_cron.settings');
    $this->state = $state;
  }
  /**
   * Run the automated cron if enabled.
   *
   * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event
   *   The Event to process.
   */
  public function onTerminate(TerminateEvent $event) {
    $interval = $this->config
      ->get('interval');
    if ($interval > 0) {
      $cron_next = $this->state
        ->get('system.cron_last', 0) + $interval;
      if ((int) $event
        ->getRequest()->server
        ->get('REQUEST_TIME') > $cron_next) {
        $this->cron
          ->run();
      }
    }
  }
  /**
   * Registers the methods in this class that should be listeners.
   *
   * @return array
   *   An array of event listener definitions.
   */
  public static function getSubscribedEvents() {
    return [
      KernelEvents::TERMINATE => [
        [
          'onTerminate',
          100,
        ],
      ],
    ];
  }
}Members
| Name   | Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| AutomatedCron:: | protected | property | The cron configuration. | |
| AutomatedCron:: | protected | property | The cron service. | |
| AutomatedCron:: | protected | property | The state key value store. | |
| AutomatedCron:: | public static | function | Registers the methods in this class that should be listeners. | |
| AutomatedCron:: | public | function | Run the automated cron if enabled. | |
| AutomatedCron:: | public | function | Constructs a new automated cron runner. | 
