[development] problems with shutdown function in search module

Augustin (Beginner) drupal.beginner at wechange.org
Mon Aug 6 13:25:10 UTC 2007

On Monday 06 August 2007 19:34, Gerhard Killesreiter wrote:
> Hi there,
> our search module uses a shutdown function in its cron hook. While
> re-indexing scratch.d.o to test Doug's patch, I encountered the
> following situation:
> 1) There are runnign several update routines from search_update_totals
> in parallel.
> 2) The final select query in that function run for hours.
> I believe that 1) is caused by the fact that the shutdown function isn't
> governed by our cron semaphore. That is, it runs until it is done and it
> won't stop other cron jobs from running. So if there are enough dirty
> workds to update, it is possible that there are several of them running
> in parallel. I've seen as many as 5. Does anybody consider this a problem?

I am not sure I understand the problem.
If you want to make sure only one process runs at a time, we can use a locking 
mechanism like I do in one module:

Making sure we get the lock prevents a race condition, a problem that make 
poormanscron.module currently unuseable (see link in code comment).

 * Implementation of hook_cron().
function hook_cron() {
  // Check if we have another instance of cron running.
  if (variable_get('hook_cron_running', 0)) {
  // hook_cron is not already running. 
  // See if we can get the lock before another process does.
  // Algorithm found here: http://drupal.org/node/43511.
  $result = db_query("UPDATE {variable} SET value = 'i:1;' WHERE name = 
'hook_cron_running' AND value = 'i:0;'");
  if (db_affected_rows() != 1) {
    // We didn't get the lock. Another process outran us.
    watchdog('hook_cron', 'This cron process was outraced.');
  // Do stuff here...

  // then, at the end:
  // Unlock.
  variable_set('hook_cron_running', 0);

Because we and the world need to change.
Intimate Relationships, peace and harmony in the couple.

More information about the development mailing list