I am attempting to write a module to extend the functionality of pablobm's excellent LDAP authentication modules. He now has split them into 3 pieces, and they gather data, get group info and authenticate to a LDAP server. What I want to do is synchronize a few drupal roles to LDAP groups and do this daily or at scheduled times. This I thought would involve "hook_cron". I have the code working, dislaying what it does on the settings page, but now I want it to be called at timed intervals. I grabbed the cron chunk of code from aggregator.module, and changed it to look like this...

/**
 * Implementation of hook_cron().
 *
 * checks for differences between ldap and drupal user database.
 */
function ldapsync_cron() {

  watchdog('cron', "Hello cron hooked...", WATCHDOG_WARNING);
  sync_ldap_to_drupal();

//  $result = db_query('SELECT * FROM {aggregator_feed} WHERE checked + refresh < %d', time());
//  while ($feed = db_fetch_array($result)) {
//    aggregator_refresh($feed);
//  }
}

I am right in thinking I am supposed to put " function module-name_cron() { " to use the hook, right? Is there any special voodoo magic I need to do to enable it? Any GLOBAL include, etc? How does cron know which modules have a hook in it? I have tried disabling and enabling the module, I have done a forum search, to no avail. I am just expecting a watchdog entry to say that it ran. Cron is working for my site, as my search index is at 100%.
I will be putting all the code up here when I get it working, because it makes things like a web searchable directory (Office, e-mail,phone,etc) available for those who already have the info stored in an LDAP without requiring everyone to log in to drupal to create the account information, and keeps it up to date from the LDAP. Any help would be appreciated, Thanks!

Liveoutloud2day

Comments

styro’s picture

Silly question but - Are you sure that cron.php had actually run while your module was enabled?

cron hooks only get run when cron.php runs as far as I can tell. You can run it manually to make sure it works.

--
Anton
New to Drupal? | Forum posting tips | Troubleshooting FAQ

liveoutloud2day’s picture

I am pretty sure that it is running. I have /var/www/drupal-4.7.4/scripts/cron-lynx.sh scheduled every minute, and I also modified cron-lynx.sh to do a sleep 30 then re-run the command, so it is actually running every 30 seconds. The cron link in the admin page shows that it had run less than 30 seconds before, every time I look at it. Any other ideas? Thanks!

liveoutloud2day

Scott Reynolds’s picture

Here is the handbook page on hook_cron to help you out. When i was havin problems with a cron hook a just typed in the address bar (www.example.com/cron.php) into Firefox and watched for php messages (which is what I was getting). Maybe your calling watchdog wrong or something. But I HIGHLY suggest that you run cron.php through your web browser to see if your getting php errors.

liveoutloud2day’s picture

I hadn't thought of running it in the browser, and when I do run cron.php in konquerer logged in another tab as the #1 user, it puts out no output whatsoever to the screen, but the hook in my module works! It also puts a "Cron run completed" in watchdog, so now I know that the other one wasn't finishing somehow. It was doing enough to update that it had run in the admin menu, but hadn't actually finished running. I then ran lynx interactively and once again a blank screen (after I accepted the cookie) and then nothing showed up in watchdog. Does it somehow have to be run as the #1 user, or is it something to do with who is running the cron entry?

liveoutloud2day’s picture

OK I got it - I had been running the ldapsync module code during development of the module, and one of the times it couldn't get in touch with the LDAP server and deleted every user in the entire drupal database. I have since fixed that bug, but apparently I deleted user 0 which is anonymous, and is what allows cron to work. I found this - http://drupal.org/node/98880 and that pointed me to http://devbee.com/all_important_user_zero which fixed the problem. I now have a fairly functional module, although it needs cleaning up, and it is specific to ActiveDirectory and how we are implementing it. I have no other way to test, so I will send it in for pablobm to look at it. BTW I would never have found it without trying to run cron in a browser (Thanks to Scott Reynolds). Thanks to all for kicking in!

kreaper’s picture

liveoutloud2day

pablom no longer maintains the ldap_integration module. Would you mind submitting the patch as a feature request in the ldap_integration queue ? . Ideally, it should be implemented in the ldapdata module as it does the job of fetching the ldap data and sticking it in the profile module, but we can look at the code and see if it needs an individual module.

kreaper