1

I am new to drupal, now I have function get_node_from_rss() which the file is inside mysite\sites\all\modules\node\node_rss\node_from_rss.module the function will update node from the rss link every 1 hour. Now, I dun know how to make this function add to the cron task. Can I run it without using cron.php? I search in google, they something like simple change the function name like get_node_from_rss_cron(), I also putting some code like:

$interval = variable_get('get_node_from_rss_interval', 60 * 60);
if (time() >= variable_get('get_node_from_rss_next_execution', 0)) {
   watchdog('get_node_from_rss', 'get_node_from_rss ran');
   if (!empty($GLOBALS['wowfi_locamusic_addnode_show_status_message'])) {
     drupal_set_message(t('wowfi_locamusic_addnode executed at %time', array('%time' => date_iso8601(time(0)))));
     drupal_set_message(t('wowfi_locamusic_addnode executed at %time', array('%time' => date_iso8601(time(0)))));
    }
    variable_set('wowfi_locamusic_addnode_next_execution', time() + $interval);
  }

inside my code, but those still not working for me, Any solution?

3
  • 1
    So you've written this in a custom module? Or you written it in the core node module?
    – Chapabu
    Commented Dec 19, 2012 at 10:30
  • Change get_node_from_rss_cron() to node_rss_cron() and node_from_rss.module filename to node_rss.module. And don't put your custom module inside core modules. Commented Dec 19, 2012 at 10:33
  • 4
    get_node_from_rss_cron() this is incorrect. Cron Functions should be of format MODULE_NAME_cron(). Any hook should follow this format.
    – GoodSp33d
    Commented Dec 19, 2012 at 10:34

1 Answer 1

3

A hook implementation is simply a my_module_name_function implentation. Your module's internal name is what you have used in the .module file's name.

So for in this case, it's node_from_rss (I'd recommend you to rename the folder name to node_from_rss as well so it reduces any confusion).

If you rename your cron function to comply with the above my_module_name_function pattern, function name has to be node_from_rss_cron

function node_from_rss_cron() {
 $interval = variable_get('get_node_from_rss_interval', 60 * 60);
 if (time() >= variable_get('get_node_from_rss_next_execution', 0)) {
   watchdog('get_node_from_rss', 'get_node_from_rss ran');
   if (!empty($GLOBALS['wowfi_locamusic_addnode_show_status_message'])) {
     drupal_set_message(t('wowfi_locamusic_addnode executed at %time', array('%time' => date_iso8601(time(0)))));
     drupal_set_message(t('wowfi_locamusic_addnode executed at %time', array('%time' => date_iso8601(time(0)))));
    }
    variable_set('wowfi_locamusic_addnode_next_execution', time() + $interval);
  }
}

Other than these changes, here are some of the Drupal best practices.

Drupal 7 has a REQUEST_TIME constant defined at the beginning of page processing. If this code is for D7, use it instead of time().

drupal_set_message() function sets a message to the screen. but cron functions will run without use interaction. You will not see this message unless you are running cron manually at admin/reports/status page.

Avoid placing folders such as 'node' inside sites/all/modules folder. Technically it's completely ok to do so. But I think it will confuse you and others in the long run. Usually we place modules like:

sites/all/modules/MY_MODULE/MY_MODULE.module
3
  • 1
    Just an FYI drupal_set_message is of no use inside a cron function unless you are debugging and calling cron function explicitly. Better to use watchdog all time
    – GoodSp33d
    Commented Dec 19, 2012 at 13:18
  • that mean i should put as mysite\sites\all\modules\node\node_from_rss.module? i thought all custom module should put like mysite\sites\all\modules\node\node_rss\node_from_rss.module, cause i got another 2 custom module also put at the folder. Another question, if this module disable at admin module setting the cron still working?
    – Ice Cream
    Commented Dec 20, 2012 at 14:14
  • If you disable it, no hook will be fired. You can't use functions in disabled modules either.
    – AKS
    Commented Dec 20, 2012 at 16:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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