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 ('[email protected]',
'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 ('[email protected]',
'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 ('[email protected]',
'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 ('[email protected]',
'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.