1

I created a custom module that uses the Taleo Web API to get data from the external Taleo database and make nodes from this data. This works great.

But now I want to run this update script every night... The script works great whem I logged in into Drupal.

What would be the safest way to allow deleting and adding nodes programmatically when logged out?

function taleo_menu() {
  $items['taleo-update'] = array(
    'title' => 'Taleo Update page',
    'description' => 'Taleo Update page',
    'page callback' => '_taleo_update',
    'access arguments' => array('view content'),
        'type' => MENU_NORMAL_ITEM,
  );    
    return $items;
}

function _taleo_insert($jobs) {
    foreach ($jobs as $job_object) {

        $node = new stdClass();
        $node->type = 'job';
        $node->title    = $job_object->title;
        $node->language = LANGUAGE_NONE;

        node_object_prepare($node);
        $node = node_submit($node); // Prepare node for a submit
        node_save($node); // After this call we'll get a nid
    }

    drupal_set_message(t('Added %count jobs.', array('%count' => count($jobs))));   
    return array();
}
  • Please provide code of hook_menu() function of your module – Eugene Fidelin Oct 11 '12 at 15:35
  • The hook_menu won't be the problem, my module adds new nodes programmatically, that's the problem I guess. – screaming SiLENCE Oct 11 '12 at 16:50
0

If you are looking for the safest way, I would say your current approach is not how most of us get things done in Drupal.

Basically, you can make use of a hook that wakes up your module, check last run's status and if suitable, start its next run.

Check out hook_cron. You can check last run status easily from a variable_get (you need to set the value first using variable_set). Cron runs anonymously regardless of a specific page call. Even if the cron runs frequently, you can use some condition to check last run timestamp and compare it to current system time. If it has exceeded the interval (1 day), you can now run the "create nodes" function.

If nodes are saved correctly, you will see a $node->nid set. use that as a condition and if all given jobs are done, you can set the last run variable to the current time.

  • Ayesh K is right , you can write your function in hook_cron, and set your cron to run 'every night'. You can set cron job at admin/config/system/cron – Anoop Joseph Oct 12 '12 at 5:43
  • Thanks, I will take a look at this! Ayesh, what do you mean with 'your current approach is not how we do things in Drupal'? Any suggestions for improvement for doing this? – screaming SiLENCE Oct 12 '12 at 10:30
  • Ayesh, you mean it's unusual to use hook_menu for this kind of task instead of hook_cron? I just did it to test the module. Seems that hook_cron is doing what I want it to do, I'm testing right now! – screaming SiLENCE Oct 12 '12 at 11:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.