Index: cron_control.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/cron_control/cron_control.module,v
retrieving revision 1.14.2.8
diff -u -p -r1.14.2.8 cron_control.module
--- cron_control.module	30 Jan 2011 20:37:52 -0000	1.14.2.8
+++ cron_control.module	1 Feb 2011 08:29:34 -0000
@@ -13,127 +13,31 @@
  */
 
 /**
- * Implements hook_cron().
+ * Implements hook_module_implements_alter().
+ *
+ * Rewrite implementations for hook_cron
  */
-function cron_control_cron() {
-  static $first_call = TRUE;
-
-  if ($first_call) {
-    // set cron_control to be the first hook_cron to be called
-    cron_control_adjust_module_weight();
-
-    $first_call = FALSE;
+function cron_control_module_implements_alter(&$implementations, $hook) {
+  if ($hook == 'cron') {
     $server_addr = isset($_SERVER['SERVER_ADDR']) ? $_SERVER['SERVER_ADDR'] : '127.0.0.1';
-    $modules_invoked = array();
 
-    foreach (module_implements('cron') as $module) {
-      if ('cron_control' != $module) {
-        if ($result = db_query("SELECT active FROM {cron_control_jobs} WHERE module = :module AND server_addr = :server_addr",
-                               array(':module' => $module, ':server_addr' => $server_addr))) {
-          if ($job = $result->fetchObject()) {
-            if ($job->active) {
-              module_invoke($module, 'cron');
-              $modules_invoked[] = $module;
-            }
-          }
-          else {
-            $job = array(
-              'module' => $module,
-              'server_addr' => $server_addr,
-            );
-
-            if (drupal_write_record('cron_control_jobs', $job)) {
-              watchdog('cron', 'Detected new cron job %module on server %server_addr.', array('%module' => $module, '%server_addr' => $server_addr), WATCHDOG_NOTICE);
-            }
+    foreach ($implementations as $module => $group) {
+      if ($result = db_query("SELECT active FROM {cron_control_jobs} WHERE module = :module AND server_addr = :server_addr", array(':module' => $module, ':server_addr' => $server_addr))) {
+        if ($job = $result->fetchObject()) {
+          if (!$job->active) {
+            unset($implementations[$module]);
           }
         }
-      }
-    }
-  }
-  else {
-    return;
-  }
-
-  // Now we have to avoid any cron job running twice.
-  // Therefore, we have to terminate the execution.
-  // But first we have to do the same cleanup that would happen without cron control
-
-  // Record cron time
-  /**
-   * @see drupal_cron_run()
-   */
-  variable_set('cron_last', REQUEST_TIME);
-  watchdog('cron', 'Cron run completed on server %server_addr. Modules invoked: %modules_invoked', array('%server_addr' => $server_addr, '%modules_invoked' => implode(', ', $modules_invoked)), WATCHDOG_NOTICE);
-
-  // Release cron lock.
-  /**
-   * @see drupal_cron_run()
-   */
-  lock_release('cron');
-
-  cron_control_handle_queues();
-
-  // now we have to do different things depending on how the cron has been started
-  if (strpos($_SERVER['SCRIPT_FILENAME'], 'cron.php') !== FALSE) {
-    // terminate cron.php
-    exit();
-  }
-  elseif (strpos($_SERVER['SCRIPT_FILENAME'], 'drush.php') !== FALSE) {
-    // cron has been started by Drush
-    /**
-     * @see drush_core_cron()
-     */
-    drush_log(dt('Cron run successfully.'), 'success');
-
-    /**
-     * @see drush_dispatch()
-     */
-    drush_log(dt('Command dispatch complete'), 'notice');
-
-    /**
-     * @see drush_main()
-     */
-    if (drush_get_context('DRUSH_DEBUG')) {
-      drush_print_timers();
-    }
-    drush_log(dt('Peak memory usage was !peak', array('!peak' => drush_format_size(memory_get_peak_usage()))), 'memory');
-    drush_set_context("DRUSH_EXECUTION_COMPLETED", TRUE);
-    exit('');
-  }
-  else {
-    // cron has been started via admin/reports/status/run-cron
-    /**
-     * @see system_menu()
-     * @see system_run_cron()
-     */
-    drupal_set_message(t('Cron ran successfully.'));
-    drupal_goto('admin/reports/status');
-    exit();
-  }
-}
-
-
-function cron_control_cron_queue_info_alter($queues) {
-  cron_control_handle_queues($queues);
-}
-
-
-function cron_control_handle_queues($set_queues = NULL) {
-  static $queues = NULL;
-
-  if (!is_null($set_queues)) {
-    $queues = $set_queues;
-    return;
-  }
+        else {
+          $job = array(
+            'module' => $module,
+            'server_addr' => $server_addr,
+          );
 
-  if (!is_null($queues)) {
-    foreach ($queues as $queue_name => $info) {
-      $function = $info['worker callback'];
-      $end = time() + (isset($info['time']) ? $info['time'] : 15);
-      $queue = DrupalQueue::get($queue_name);
-      while (time() < $end && ($item = $queue->claimItem())) {
-        $function($item->data);
-        $queue->deleteItem($item);
+          if (drupal_write_record('cron_control_jobs', $job)) {
+            watchdog('cron', 'Detected new cron job %module on server %server_addr.', array('%module' => $module, '%server_addr' => $server_addr), WATCHDOG_NOTICE);
+          }
+        }
       }
     }
   }
@@ -147,8 +51,8 @@ function cron_control_menu() {
   $items = array();
 
   $items['admin/config/system/cron/cron'] = array(
-    'title' => t('Cron'),
-    'description' => t('Manage automatic site maintenance tasks.'),
+    'title' => 'Cron',
+    'description' => 'Manage automatic site maintenance tasks.',
     'page callback' => 'drupal_get_form',
     'page arguments' => array('system_cron_settings'),
     'access arguments' => array('administer site configuration'),
@@ -196,25 +100,6 @@ function cron_control_menu() {
   return $items;
 }
 
-/**
- * Helper function to set cron_control's weight to the lowest of all modules.
- */
-function cron_control_adjust_module_weight() {
-  // set cron_control to be the first hook_cron to be called
-  if ($result = db_query("SELECT MIN(weight) FROM {system} WHERE name <> :name", array(':name' => 'cron_control'))) {
-    $weight = $result->fetchField() - 1;
-    // TODO Please review the conversion of this statement to the D7 database API syntax.
-    /* db_query("UPDATE {system} SET weight = %d WHERE name = 'cron_control' AND weight <> %d", $weight, $weight) */
-    db_update('system')
-  ->fields(array(
-      'weight' => $weight,
-    ))
-  ->condition('name', 'cron_control')
-  ->condition('weight', $weight, '<>')
-  ->execute();
-  }
-}
-
 
 /**
  * Implements hook_requirements().
