class AutomatedCron

Same name and namespace in other branches
  1. 9 core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php \Drupal\automated_cron\EventSubscriber\AutomatedCron
  2. 8.9.x core/modules/automated_cron/src/EventSubscriber/AutomatedCron.php \Drupal\automated_cron\EventSubscriber\AutomatedCron
  3. 10 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 extends \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 17

Namespace

Drupal\automated_cron\EventSubscriber
View source
class AutomatedCron implements EventSubscriberInterface {
  public function __construct(#[AutowireServiceClosure('cron')] protected readonly \Closure $cron, protected readonly ConfigFactoryInterface $configFactory, protected StateInterface $state) {
  }
  
  /**
   * Run the automated cron if enabled.
   *
   * @param \Symfony\Component\HttpKernel\Event\TerminateEvent $event
   *   The Event to process.
   */
  public function onTerminate(TerminateEvent $event) : void {
    $interval = $this->configFactory
      ->get('automated_cron.settings')
      ->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() : array {
    return [
      KernelEvents::TERMINATE => [
        [
          'onTerminate',
          100,
        ],
      ],
    ];
  }

}

Members

Title Sort descending Modifiers Object type Summary
AutomatedCron::getSubscribedEvents public static function Registers the methods in this class that should be listeners.
AutomatedCron::onTerminate public function Run the automated cron if enabled.
AutomatedCron::__construct public function

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.