From a4bb8acdd995c494ed41a9bdb1d7c5a5e6e67ad0 Sat, 13 Aug 2011 12:46:02 +0200
From: Jorrit Schippers <jorrit@ncode.nl>
Date: Sat, 13 Aug 2011 12:41:17 +0200
Subject: [PATCH] Add a time limit of two seconds for every row that is added using cron.

diff --git a/user_import.module b/user_import.module
index a185e06..407d8c5 100644
--- a/user_import.module
+++ b/user_import.module
@@ -119,16 +119,26 @@
  * Implementation of hook_cron().
  */
 function user_import_cron() {
-    
-    $imports = _user_import_settings_select();    
-    if (!$imports) return;
-    
-    foreach ($imports as $import) {
-        
-        if ($import['setting'] == 'test' || $import['setting'] == 'import') _user_import_process($import);
+  $imports = _user_import_settings_select();    
+  if (!$imports) return;
+
+  // For each cron run, allow 2 seconds for each row
+  $timelimit = variable_get('user_import_max', 250) * 2;
+
+  foreach ($imports as $import) {
+    if ($import['setting'] == 'test' || $import['setting'] == 'import') {
+      watchdog('user_import', 'Cron for file @filename started', array('@filename' => $import['filename']), WATCHDOG_INFO);
+
+      // Allow each import to run for 2 seconds per user
+      _user_import_set_time_limit($timelimit);
+      _user_import_process($import);
+
+      watchdog('user_import', 'Cron for file @filename ended', array('@filename' => $import['filename']), WATCHDOG_INFO);
     }
-        
-    return;
+  }
+
+  // Set the default cron time limit
+  _user_import_set_time_limit(240);
 }
 
 // - - - - - - - -  FORMS - - - - - - - - 
@@ -1637,6 +1647,18 @@
   
   return $settings_updated;
 }
+
+/**
+ * Sets the time limit if this is allowed by the server
+ * configuration.
+ * 
+ * @param int $timelimit New time limit in seconds
+ */
+function _user_import_set_time_limit($timelimit) {
+  if (function_exists('set_time_limit') && !ini_get('safe_mode')) {
+    set_time_limit($timelimit);
+  }
+}
 
 /**
  * Loads the hooks for the supported modules.

