2 * Implements hook_cron().
4 function {{ machine_name }}_cron() {
5 // Short-running operation example, not using a queue:
6 // Delete all expired records since the last cron run.
7 $expires = \Drupal::state()->get('mymodule.last_check', 0);
8 \Drupal::database()->delete('mymodule_table')
9 ->condition('expires', $expires, '>=')
11 \Drupal::state()->set('mymodule.last_check', REQUEST_TIME);
13 // Long-running operation example, leveraging a queue:
14 // Queue news feeds for updates once their refresh interval has elapsed.
15 $queue = \Drupal::queue('aggregator_feeds');
16 $ids = \Drupal::entityManager()->getStorage('aggregator_feed')->getFeedIdsToRefresh();
17 foreach (Feed::loadMultiple($ids) as $feed) {
18 if ($queue->createItem($feed)) {
19 // Add timestamp to avoid queueing item more than once.
20 $feed->setQueuedTime(REQUEST_TIME);
24 $ids = \Drupal::entityQuery('aggregator_feed')
25 ->condition('queued', REQUEST_TIME - (3600 * 6), '<')
28 $feeds = Feed::loadMultiple($ids);
29 foreach ($feeds as $feed) {
30 $feed->setQueuedTime(0);