Changes between Version 11 and Version 12 of Drupal HowTo Write Cron Tasks for CiviCRM
- Timestamp:
- 06/02/14 13:59:38 (10 years ago)
Legend:
- Unmodified
- Added
- Removed
- Modified
-
Drupal HowTo Write Cron Tasks for CiviCRM
v11 v12 47 47 48 48 {{{#!php 49 50 49 // Implementation of Drupal cron hook 50 function module_cron() { 51 51 52 $helper = new CronHelper('JobName', variable_get('MODULE_CRON_FREQUENCY', CronHelper::FREQUENCY_WEEKLY)); 53 54 if (!$helper->is_due()) { 55 return; 56 } 57 58 $helper->start(); 59 60 try { 61 62 // Do stuff here 63 64 } catch (Exception $ex) { 65 throw $ex; 66 } finally { 67 $helper->end(); 68 } 69 70 } 52 $helper = new CronHelper('JobName'); 53 $helper->runCron(function() { 54 // Do stuff here 55 }); 56 } 71 57 }}} 72 58 73 59 Things to note are: 74 * The job name and frequency parameters for the constructor are optional and are only used if the helper is being used to check if the job is due. 75 * The variable get for the frequency uses a default value from the constants defined in the !CronHelper class. 76 * The start() method should be called before any processing occurs. 77 * The end() method should *always* be called after processing. Therefore, in the example all processing is placed in a try block with the end() method called from the matching finally block. 78 60 * The jobName parameter cannot be NULL or empty. 61 * The following additional option constructor parameters can be used to change the default enabled status and frequency: 62 * $defaultEnabled - takes values true or false. The default is false. 63 * $defaultFrequency - takes one of the CronHelper frequency constant values: 64 * CronHelper::FREQUENCY_EVERY_CRON 65 * CronHelper::FREQUENCY_DAILY 66 * CronHelper::FREQUENCY_WEEKLY (default) 67 * CronHelper::FREQUENCY_MONTHLY 68 * The runCron function takes either an anonymous function or a function name as it's argument. 79 69 === Config Usage 80 70 71 The values for the enable status and frequency can be changed by the user using a module configuration form. CronHelper provides a utility function to add the setting fields to a configuration form. For example 72 73 {{{#!php 74 function _module_config_form($form, &$form_state) { 75 76 $helper = new CronHelper('JobName'); 77 78 $helper->addSettingsToForm($form); 79 80 return system_settings_form($form); 81 } 82 83 }}} 84