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
}
1

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();
}
  • 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 Jul 17 '20 at 13:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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