Sunday, January 14, 2018

Customizing cron.php

Customize cron.php without replacing the stock file

I wanted to add functionality where I would be emailed if any of the cron jobs failed for any reason.  I found in $sugar_config there is an option to override the cron class that is used to run scheduled jobs and normal cron related things. You can add it to the config_override.php file like this...



<?php
/***CONFIGURATOR***/
$sugar_config['cron_class'] = 'CustomSugarCronJobs';
/***CONFIGURATOR***/
So once you have that set up, just create a file called custom/include/SugarQueue/CustomSugarCronJobs.php like this
<?php
if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
//Include the original class
require_once 'include/SugarQueue/SugarCronJobs.php';
/**
* CRON driver for job queue
* @api
*/
class CustomSugarCronJobs extends SugarCronJobs
{
/**
* What to do if one of the jobs failed
* @param SchedulersJob $job
*/
protected function jobFailed($job = null)
{
//Do my custom stuff
if(!empty($job)) {
$message = "Job {$job->id} ({$job->name}) failed in CRON run";
//Email me the message
$GLOBALS['developerCommunication']->send($message, '', true);
}
//run the parent function
parent::jobFailed($job);
}
}
The Class name should match the file name, 'CustomSugarCronJobs' in this case. You could override any function, here I override the function jobFailed(), or you could add a function.

2 comments:

  1. Hello Kenneth,

    Could you share the code of the $GLOBALS['developerCommunication'] class and how to put it as $GLOBAL?

    Thank you.

    Regards,

    David.

    ReplyDelete
    Replies
    1. Yes, its kinda crap at the moment, just a global function. Let me turn it into something a little better and I'll release it too. I should be able to do that tonight.

      Delete