Index: subscriptions_mail.module
===================================================================
--- subscriptions_mail.module	(revision 281)
+++ subscriptions_mail.module	(working copy)
@@ -5,16 +5,24 @@
   global $user;
   include_once drupal_get_path('module', 'subscriptions_mail') .'/subscriptions_mail.templates.inc';
 
-  $number_of_mails = variable_get('subscriptions_number_of_mails', 200);
   $from = variable_get('site_mail', ini_get('sendmail_from'));
   $old_uid = 0;
-  $counter = 0;
+  $single_count = 0;
+  $digest_count = 0;
   $loaded_objects = array();
   $users = array();
   $fields = array();
   $mails = array();
+  
+  // Strategy for cron:
+  // Use 50% of the remaining time to process queue items, send single messages, and assemble digest messages;
+  // send the digest messages in the other half and hopefully leave some time for other modules. 
+  $total_seconds = ini_get('max_execution_time');
+  $lost_seconds = timer_read('page')/1000;
+  $available_seconds = $total_seconds - $lost_seconds; 
+  
   session_save_session(FALSE);
-  while ($counter++ < $number_of_mails) {
+  while ($total_seconds - timer_read('page')/1000 > $available_seconds*variable_get('subscriptions_cron_percent', 50)/100) {
     subscriptions_mail_mail_edit_variables($null = NULL);  // clear cache
     $result = db_query_range('SELECT * FROM {subscriptions_queue} WHERE last_sent + send_interval < %d ORDER BY sqid', time(), 0, 1);
     if ($s = db_fetch_array($result)) {
@@ -109,6 +117,7 @@
           else {
             subscriptions_mail_mail_edit_variables($mailvars);
             _subscriptions_mail_send($mailkey, $s['name'], $s['mail'], $subject, $body, $from, $s['uid']);
+            ++$single_count;
           }
         }
       }
@@ -143,8 +152,17 @@
       $body = strtr(subscriptions_mail_template_preprocess($body_template, $mailvars), $mailvars);
       subscriptions_mail_mail_edit_variables($mailvars);
       _subscriptions_mail_send(SUBSCRIPTIONS_DIGEST_MAILKEY, $s['name'], $s['mail'], $subject, $body, $s['from'], $uid);
+      ++$digest_count;
     }
   }
+  watchdog('cron', t("!module sent !single_count single and !digest_count digest messages in !used_seconds of !available_seconds available seconds; !remaining_items queue items left.", array(
+    '!module'            => 'Subscriptions',
+    '!single_count'      => $single_count,
+    '!digest_count'      => $digest_count,
+    '!used_seconds'      => (integer) (timer_read('page')/1000 - $lost_seconds),
+    '!available_seconds' => (integer) $available_seconds . ($lost_seconds > 5*$total_seconds/100 ? " ($total_seconds)" : ''),
+    '!remaining_items'   => db_result(db_query("SELECT COUNT(*) FROM {subscriptions_queue}")),
+  )));
 }
 
 /**
