| 32 | |
| 33 | == !CronHelper |
| 34 | |
| 35 | In order to assist with some of the common tasks associated with Drupal cron tasks a new !CronHelper class has been created. |
| 36 | |
| 37 | === Functions |
| 38 | |
| 39 | 1. Initialise CiviCRM |
| 40 | 1. Switch the user used for thr cron job from the anonymous user to the Cron System User for the duration of the processing. |
| 41 | 1. Ascertain if the cron processing is due to run. |
| 42 | |
| 43 | === Usage |
| 44 | |
| 45 | The following code shows how the cron Helper should used. |
| 46 | |
| 47 | {{{#!php |
| 48 | // Implementation of Drupal cron hook |
| 49 | function module_cron() { |
| 50 | |
| 51 | $helper = new CronHelper('JobName', variable_get('MODULE_CRON_FREQUENCY', CronHelper::FREQUENCY_WEEKLY)); |
| 52 | |
| 53 | if (!$helper->is_due()) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | $helper->start(); |
| 58 | |
| 59 | try { |
| 60 | if |
| 61 | // Do stuff here |
| 62 | |
| 63 | } catch (Exception $ex) { |
| 64 | throw $ex; |
| 65 | } finally { |
| 66 | $helper->end(); |
| 67 | } |
| 68 | |
| 69 | } |
| 70 | }}} |