1 cron_example.module cron_example_cron()

Implements hook_cron().

hook_cron() is the traditional hook for doing "background" processing. It gets called every time the Backdrop cron runs and must decide what it will do.

In this example, it calls watchdog() after the time stored in the cron_example_next_execution state value passed and sets that value to a time in the future.

Related topics

File

modules/examples/cron_example/cron_example.module, line 194
Hook implementations for the Cron Example module.

Code

function cron_example_cron() {
  // Default to an hourly interval. Of course, cron has to be running at least
  // hourly for this to work.
  $interval = config_get('cron_example.settings', 'interval');

  // We usually don't want to act every time cron runs (which could be every
  // minute), so we store the time for the next run.
  if (time() >= state_get('cron_example_next_execution', 0)) {
    // This is a silly example of a cron job. It just makes it obvious that the
    // job has run without making any changes to your database.
    watchdog('cron_example', 'cron_example ran');

    if (!empty($GLOBALS['cron_example_show_status_message'])) {
      backdrop_set_message(t('cron_example executed at %time', array('%time' => cron_example_date_iso8601(time(0)))));
    }

    state_set('cron_example_next_execution', time() + $interval);
  }
}