How to declare jobs in hook_cron_job_scheduler_info?

Created on 5 June 2021, over 3 years ago
Updated 28 March 2023, over 1 year ago

Problem/Motivation

The README gives this example for declaring a job inside hook_cron_job_scheduler_info():

  // This will create a job that will be triggered from monday to friday, from january to july, every two hours
  function example_cron_job_scheduler_info() {
    $schedulers = [];
    $schedulers['example_unpublish'] = [
      'worker callback' => 'example_unpublish_nodes',
      'jobs' => [
         ['type' => 'story', 'id' => 12, 'crontab' => '0 */2 * january-july mon-fri', 'periodic' => TRUE],
      ],
    ];
    return $schedulers;
  }

So I configured a job in my module, made sure that the worker callback is a valid function, and then rebuilt the cache. However, I don't see the job in the job schedule table in the database.

I also tried re-installing my custom module, but the jobs did not get added.

If I create a job using the service like this:

  $job = [
    'name' => 'example_unpublish',
    'type' => 'story',
    'id' => 12,
    'period' => 3600,
    'periodic' => TRUE,
  ];
  $service = \Drupal::service('job_scheduler.manager');
  $service->set($job)

then the job is created and saved in the database.

So what do I need to do to "activate" (create in the database) the jobs that I defined in hook_cron_job_scheduler_info()?

Steps to reproduce

My actual code:

function MYMODULE_cron_job_scheduler_info() {
  $schedulers = [];
  $schedulers['MYMODULE_notifications_by_interval'] = [
    'worker callback' => 'MYMODULE_notifications_by_interval',
    'jobs' => [
      ['type' => 'notifications', 'id' => 0000, 'crontab' => '0 0 * * *', 'periodic' => TRUE],
      ['type' => 'notifications', 'id' => 0015, 'crontab' => '15 0 * * *', 'periodic' => TRUE],
      ['type' => 'notifications', 'id' => 0030, 'crontab' => '30 0 * * *', 'periodic' => TRUE],
      ['type' => 'notifications', 'id' => 0045, 'crontab' => '45 0 * * *', 'periodic' => TRUE],
      ['type' => 'notifications', 'id' => 0100, 'crontab' => '0 1 * * *', 'periodic' => TRUE],
      ['type' => 'notifications', 'id' => 0115, 'crontab' => '15 1 * * *', 'periodic' => TRUE],
      ['type' => 'notifications', 'id' => 0130, 'crontab' => '30 1 * * *', 'periodic' => TRUE],
      ['type' => 'notifications', 'id' => 0145, 'crontab' => '45 1 * * *', 'periodic' => TRUE],
      ['type' => 'notifications', 'id' => 1200, 'crontab' => '0 12 * * *', 'periodic' => TRUE],
    ]
  ];
  return $schedulers;
}
πŸ’¬ Support request
Status

Fixed

Version

3.1

Component

Documentation

Created by

πŸ‡―πŸ‡΅Japan ptmkenny

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • πŸ‡ΊπŸ‡¦Ukraine Ressinel πŸ‡ΊπŸ‡¦ Rivne

    I had the same issue.
    You can fix it by adding hook_install() to your module .install file with rebuilding the needed scheduler:

    /**
     * Implements hook_install().
     */
    function example_install() {
      // Make sure the job scheduler is registered.
      /** @var \Drupal\job_scheduler\JobScheduler $scheduler_manager */
      $scheduler_manager = \Drupal::service('job_scheduler.manager');
      $schedulers = example_cron_job_scheduler_info();
      foreach ($schedulers as $id => $scheduler) {
        $scheduler_manager->rebuild($id, $scheduler);
      }
    }
    
    
Production build 0.71.5 2024