function hook_cron

Same name in other branches
  1. 7.x modules/system/system.api.php \hook_cron()
  2. 9 core/core.api.php \hook_cron()
  3. 8.9.x core/core.api.php \hook_cron()
  4. 11.x core/core.api.php \hook_cron()

Perform periodic actions.

Modules that require some commands to be executed periodically can implement hook_cron(). The engine will then call the hook whenever a cron run happens, as defined by the administrator. Typical tasks managed by hook_cron() are database maintenance, backups, recalculation of settings or parameters, automated mailing, and retrieving remote data.

Short-running or non-resource-intensive tasks can be executed directly in the hook_cron() implementation.

Long-running tasks and tasks that could time out, such as retrieving remote data, sending email, and intensive file tasks, should use the queue API instead of executing the tasks directly. To do this, first define one or more queues via a \Drupal\Core\Annotation\QueueWorker plugin. Then, add items that need to be processed to the defined queues.

See also

Queue operations

Related topics

17 functions implement hook_cron()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

announcements_feed_cron in core/modules/announcements_feed/announcements_feed.module
Implements hook_cron().
comment_cron in core/modules/comment/comment.module
Implements hook_cron().
common_test_cron in core/modules/system/tests/modules/common_test/common_test.module
Implements hook_cron().
common_test_cron_helper_cron in core/modules/system/tests/modules/common_test_cron_helper/common_test_cron_helper.module
Implements hook_cron().
dblog_cron in core/modules/dblog/dblog.module
Implements hook_cron().

... See full list

File

core/core.api.php, line 1923

Code

function hook_cron() {
    // Short-running operation example, not using a queue:
    // Delete all expired records since the last cron run.
    $expires = \Drupal::state()->get('my_module.last_check', 0);
    $request_time = \Drupal::time()->getRequestTime();
    \Drupal::database()->delete('my_module_table')
        ->condition('expires', $expires, '>=')
        ->execute();
    \Drupal::state()->set('my_module.last_check', $request_time);
    // Long-running operation example, leveraging a queue:
    // Queue news feeds for updates once their refresh interval has elapsed.
    $queue = \Drupal::queue('my_module.feeds');
    $ids = \Drupal::entityTypeManager()->getStorage('my_module_feed')
        ->getFeedIdsToRefresh();
    foreach (Feed::loadMultiple($ids) as $feed) {
        if ($queue->createItem($feed)) {
            // Add timestamp to avoid queueing item more than once.
            $feed->setQueuedTime($request_time);
            $feed->save();
        }
    }
    $ids = \Drupal::entityQuery('my_module_feed')->accessCheck(FALSE)
        ->condition('queued', $request_time - 3600 * 6, '<')
        ->execute();
    if ($ids) {
        $feeds = Feed::loadMultiple($ids);
        foreach ($feeds as $feed) {
            $feed->setQueuedTime(0);
            $feed->save();
        }
    }
}

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