1

In one of my modules I would like to create two different cron jobs to handle two different procedures. What I did was to use the Ultimate Cron module, and by implementing the cron api, to schedule one of my cron jobs.

My problem is that in /admin/config/system/cron/jobs only the default from the hook_cron appears and not from the hook_cronapi. This is what I wrote.

/**
 * Implements hook_cron().
 */
function MODULENAME_cron() {
  //code
}

/**
 * Implements hook_cronapi().
 */
function MODULENAME_cronapi() {
  $items = array();

  $items['generate_excel_files'] = array(
    'title' => 'Table Excel Generate',
    'callback' => 'generate_excel_files_callback',
    'scheduler' => array(
      'name' => 'crontab',
      'crontab' => array(
        'rules' => array('0 3 * * *'),
      ),
    ),
  );

  return $items;
}

function generate_excel_files_callback(){
  //code
}

2 Answers 2

2

Apparently cron api dosn't work with Drupal 8 at this time. What I did to fix my issue was to create one new module to handle only the cron job that I wanted. Then from the UI of Ultimate Cron I would add the rule to schedule my cron job.

use Drupal\MODULENAME\Controller\ClassName;

/**
 * Implements hook_cron().
 */
function NEW_MODULENAME_cron() {
 generate_excel_files_callback();
}

function generate_excel_files_callback(){
 $obj = new ClassName();
 $obj->generateXLSX();
}
2
  • 1
    I followed this method, but the cron is not running on the scheduled time. The cron runs when I manually hit Run from Cron Jobs.Is there any configuration that I missed?
    – developer
    Commented Jul 17, 2020 at 13:50
  • @developer I am also having the same issues.
    – Xab Ion
    Commented Apr 5, 2023 at 4:15
1

In reply to "but the cron is not running on the scheduled time." From what I understand, the cron will still only run if a request is sent. So any request to any page. If no request are received from the server, the cron will never run, regardless of the time you set. If a request is received, it will check the time lapsed since the last time cron ran and compare with your set value to decide if it needs running again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.