2

I've created a new Drupal 7 module, which has been installed and enabled.

I have this function in the module.

function remove_entries_cron() {
  $query = "INSERT INTO {search_dataset} (sid,type,data) VALUES (22061986, node, hello)";
  db_query($query);
}

I have cleared the cache, and ran the cron manually but the record doesn't appear in the database.

Is there a way to check if the cron hook is working?

6
  • Try to debug in hook_cron() function. Write any thing print and die; statement and check whether function is called or not.. – mohit_rocks Jul 29 '13 at 4:26
  • Hi, in terms of checking if the function is called or not, you can use also the "watchdog" function. – Oleg Videnov Jul 29 '13 at 4:36
  • I've tried to echo and exit(); but the cron job was still successfully and redirected me back to the status page. I have also added watchdog('file system', 'hello', "" , WATCHDOG_INFO); to the function above but nothing pops up in the logs – John Jul 29 '13 at 4:41
  • Other Cron hooks are working in other modules. I wonder what i am doing wrong? – John Jul 29 '13 at 4:46
  • 1
    Got it! The cron hook needs to be the same name as the module name. eg search_remove_entries.module function search_remove_entries_cron() Thanks for all the help guys. – John Jul 29 '13 at 4:52
4

To check if a cron hook is being invoked, you need to output something to an existing database, for example using one of the following functions:

  • watchdog() requires you enabled the Database logging module; otherwise, no entry will be added to the watchdog table, and admin/reports/dblog would not show new entries

  • variable_set() always set a value in a database table, but the value returned from variable_get() could be the value set in the settings.php file for the $conf array

  • The database API functions can be used, but you need to be use the database table exists; for example, use a database table installed by a module, even if it a module of yours

In the second case, don't use a Drupal variable (e.g. this_is_a_wrong_variable), for which the following code exists in settings.php.

$conf['this_is_a_wrong_variable'] = 'This value will be always returned from variable_get().'

If you do so, whatever you set with variable_set('this_is_a_wrong_variable', 'My hook is invoked: ' . time()) will not be returned from variable_get() which would return 'This value will be always returned from variable_get().', in the example I made.

As you discovered, every Drupal hook must have a name prefixed by the module short name. In fact, the code used by Drupal to invoke a specific hook used by all the modules is the following one.

  foreach (module_implements($hook) as $module) {
    $function = $module . '_' . $hook;
    if (function_exists($function)) {
      $result = call_user_func_array($function, $args);
      if (isset($result) && is_array($result)) {
        $return = array_merge_recursive($return, $result);
      }
      elseif (isset($result)) {
        $return[] = $result;
      }
    }
  }

As you see the function name for the function implementating a hook is $module . '_' . $hook. For the Node module, the hook_cron() implementation would be node_cron(); for an hypothetical module_with_a_long_name module, the hook_cron() implementation would be module_with_a_long_name_cron().

Bear in mind that, if you edit the code of an enabled module to add a new hook, you need to disable and re-enable it, or Drupal will not see you added a new hook has been added to the module, since the list of hooks implemented by a module is cached. See module_implements().

  // Fetch implementations from cache.
  if (empty($implementations)) {
    $implementations = cache_get('module_implements', 'cache_bootstrap');
    if ($implementations === FALSE) {
      $implementations = array();
    }
    else {
      $implementations = $implementations->data;
    }
  }

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.