1

I am brand new to module development, fresh off the D7 tutorial boat. What I want to do cannot be that hard.

I have created my first module called get_weather, with get_weather.info and get_weather.module properly installed and drupal recognized.

I created another simple script that has one function, which I want to run when cron runs. 'update_weather' the ONLY thing this does is return the temperature for that time and store it in the database.

There is nothing to display, there are no blocks to render. However, this does not work:

function get_weather_cron() { update_weather(); }

What's going on here? I get no errors, but nothing happens at all? How do I get my module to run my custom function when cron runs?

Note that my custom function works fine if I run it from a script outside of a Drupal module environment.

Here is code based on comments feedback:

include('weather.php');
function get_weather_cron(){
    watchdog('get_weather_debug', 'Cron function triggered');
    update_weather();
}

Also here is update_weather, which is included in the weather.php include file:

function update_weather() {
    $page['weather'] = scrape_weather();
    //insert into db code here;
}

Again, when I simply run this php normally outside of the drupal enviroment, everything works fine. All I want to do is run this same script when cron runs so this is done automatically. No function in my entire library for this functionality has anything to do with Drupal. I am simply trying to activate it by using cron.

  • Did you clear the caches since implementing hook_cron()? – Clive Aug 7 '12 at 10:46
  • yea, several times. One thing I am confused about in learning about modules - I have to define the function - function get_weather_cron(){}. Well, in normal php that is just a definition. Don't I have to call it? I did put get_weather_cron() right after the definition, but still nothing. thoughts? thanks! – blue928 Aug 7 '12 at 10:48
  • 2
    The hook_cron() will be automatically called by Drupal, so the answer to your question is - no, you don't have to call it directly yourself. Try adding the following code to the get_weather_cron() function: watchdog('get_weather_debug', 'Cron function triggered'); After that, clean cache, run the cron manually and then go to the log to see if the "Cron function triggered" message is saved there. – Aram Boyajyan Aug 7 '12 at 10:50
  • Also make doubly sure that your module is installed and enabled – Clive Aug 7 '12 at 10:53
  • 1
    Can you post your exact code? – googletorp Aug 7 '12 at 12:23
1

Welcome to drupal! One of the first things that will learn is that Drupal keeps an internal cache of all hooks implemented on the site. Just because you implement hook_cron() in your custom module, and then run cron, does not mean it will work.

Steps I would suggest to help fix

  1. Install drush (this is Drupal's Swiss Army knife)
  2. run drush cc all
  3. run drush cron

Hopefully it will all be resolved

To check if your module is installed and enabled correctly you can

  • drush pml | grep get_weather (or alternatively look at the modules list in the UI)

Or to check if there is something wrong with your function update_weather();, you can run it in the context of drupal (i.e. alter a full drupal bootstrap with all modules loaded)

  • drush php-eval "print update_weather();" - you can also place additional logging or printing in your function to see what is going on.

Of course, don't forget to define all you functions that you want to execute in the .module file, as every enabled module, has it's .module file included on every request. On a side note, why don't you do away with calling another function from hook cron?

function get_weather_cron() { update_weather(); }

Simply use something similar to the following one.

function get_weather_cron() {
  // more actual weather getting code here
  variable_set('get_weather_temp', 23);
}

It should be easier to read for the next person too.

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.