Changes between Version 6 and Version 7 of Drupal HowTo Write Cron Tasks for CiviCRM


Ignore:
Timestamp:
05/30/14 15:02:26 (10 years ago)
Author:
Richard Bramley
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • Drupal HowTo Write Cron Tasks for CiviCRM

    v6 v7  
    3030
    3131However, for an automated process this isn't what we want, because cron jobs will be assigned to whichever user happens to be logged in at the time the job is triggered. Instead, the LCBRU module creates a CiviCRM contact called 'Cron System' (LCBRU Staff sub-type), and all the functions and activities performed by cron tasks should be explicitly assigned to that specific contact ID. This will require the cron job to look up the Cron System in order to obtain the correct contact ID.
     32
     33== !CronHelper
     34
     35In 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
     391. Initialise CiviCRM
     401. Switch the user used for thr cron job from the anonymous user to the Cron System User for the duration of the processing.
     411. Ascertain if the cron processing is due to run.
     42
     43=== Usage
     44
     45The 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}}}