0

I am trying to use the Job Scheduler Module's API to automatically run a script every 6 hours but I'm having problems getting it working.

This is the code:

function jobbank_cron_job_scheduler_info() {
  $schedulers = array();
  $schedulers['custom'] = array(
    'worker callback' => 'custom_job',
    'jobs' => array(
       array('crontab' => '6 * * * *', 'periodic' => TRUE),
    )
  );
  return $schedulers;
}


function get_job_bank() {
  //Save the (long) job bank URL
  $jobBankURL = "https://www.jobbank.gc.ca/xmllite/en/?q=city_id:33821";
  $jobURL = "https://www.jobbank.gc.ca/xmllite/en/";
  $jobs = [];
  $jobInfo = null;

  //Get the XML data into an object
  $jobsInfo = simplexml_load_file($jobBankURL);

  //Send a message and exit if it fails getting the XML
  if($jobsInfo === false) {
    mail ('webmastercolin@gmail.com',
          'Job Central SM Job Bank CRONjob issue',
          'Getting the $jobsInfo XML data failed (line 9) at '.date('H:i:s \o\n F j, Y'));
    exit();
  }

  //Iterate through the objects
  foreach($jobsInfo->Documents->Document as $job) {
    //Get the job data from Job Bank
    $jobInfo = simplexml_load_file($jobURL.$job->jobs_id.".xml");

    //Grab just the information for the job and put it in the array
    $jobs[] = $jobInfo->Documents->Document;
  }

  //Encode the array as JSON
  $json = json_encode($jobs);

  //Send a message and exit if it fails encoding the JSON
  if($json === false) {
    mail ('webmastercolin@gmail.com',
          'Job Central SM Job Bank CRONjob issue',
          'JSON encoding the array failed (line 23) at '.date('H:i:s \o\n F j, Y'));
    exit();
  }

  //Save new data to json file, overwriting existing
  $writeResult = file_put_contents($base_path . 'sites/default/files/jobbank/job-bank-data.json',$json,LOCK_EX);

  //Send a message and exit if overwriting the file failed
  if($writeResult === false) {
    mail ('webmastercolin@gmail.com',
          'Job Central SM Job Bank CRONjob issue',
          'Writing to the JSON file failed (line 31) at '.date('H:i:s \o\n F j, Y'));
    exit();
  }

  //Send a message on success, we are done
  mail ('webmastercolin@gmail.com',
        'Job Central SM Job Bank CRONjob Success!',
        'JSON creation succeeded at '.date('H:i:s \o\n F j, Y'));
  exit();
}

I appreciate the help.

0
  1. If you want to schedule it every 6 hours, then it's 'crontab' => '0 /6 * * *'

    The README.txt contains examples:

 /* Declare scheduler. */

  function example_cron_job_scheduler_info() {
    $schedulers = array();
    $schedulers['example_unpublish'] = array(
      'worker callback' => 'example_unpublish_nodes',
    );
    return $schedulers;
  }

 /* Add a job. */

  $job = array(
    'type' => 'story',
    'id' => 12,
    'crontab' => '0 /6 * * *', // I changed this line.
    'periodic' => TRUE,
  );
  JobScheduler::get('example_unpublish')->set($job);

 /* Work off a job. */

  function example_unpublish_nodes($job) {
    // Do stuff.
  }

 /* Remove a job. */

  $job = array(
    'type' => 'story',
    'id' => 12,
  );
  JobScheduler::get('example_unpublish')->remove($job);
  1. 'worker callback' => 'custom_job' it's bad practice to not include the name of your module in your callback. This is to prevent collisions. You should name it module_name_custom_callback. In your case it would be 'callback' => 'jobbank_custom_job'

  2. The Job Scheduler is only going to schedule it, not run it. It will get run whenever cron runs. So if you have cron run set to once a day, it will only run your scheduled task once a day. Therefore, you will have to set cron to run every 6 hours or fewer.

  3. Alternatively, I suggest you use a module like Ultimate Cron which allows you to schedule different cron run times for different cron tasks. So you can set it to every 6 hours here.

  4. This is the proper Drupal way to programmatically send e-mails How to programmatically send an email?

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.