Utility for throttling execution of tasks.

Scheduled Tasks

The module emits events for hourly, daily, weekly, and monthly intervals that can be subscribed to.

MyModuleEventSubscriber {

  public static function getSubscribedEvents() {
    $events = [];

    $events[PeriodicEvents::HOUR] = ['hourlyTask'];
    $events[PeriodicEvents::DAY] = ['dailyTask'];

    return $events;
  }

  public function hourlyTask() {
    // Do hourly task.
  }

  public function dailyTask() {
    // Do daily task.
  }

}

Note: These events are triggered by cron, so will execute on the next cron run following the interval's expiry.

Custom Tasks

Custom tasks can be throttled by arbitrary intervals.

function mymodule_cron() {
  $periodManager = \Drupal::service('periodic.manager');

  // Limit task to every six hours.
  if ($periodManager->execute('mymodule.mytask', 21600)) {
    // Do custom task.
  }

  // Delay first execution of a task.
  if ($periodManager->execute('mymodule.mydelayedtask', 21600, FALSE)) {
    // Do custom task.
  }
}

Project information

Releases