views_send.cron.inc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /**
  3. * @file
  4. * Views Send cron rotuines.
  5. *
  6. * @ingroup views_send
  7. */
  8. /**
  9. * Process the spool queue at cron run.
  10. */
  11. function views_send_send_from_spool() {
  12. $limit = variable_get('views_send_throttle', 20);
  13. $ok = $fail = $check = 0;
  14. // Get PHP maximum execution time. 30 seconds default.
  15. $max_execution_time = ini_get('max_execution_time') ? ini_get('max_execution_time') : VIEWS_SEND_MAX_EXECUTION_TIME;
  16. // Reset a Drupal timer.
  17. timer_start('views_send');
  18. // Retrieve messages to be send.
  19. $query = "SELECT * FROM {views_send_spool} WHERE status = :status ORDER BY tentatives ASC, timestamp ASC";
  20. $result = $limit ? db_query_range($query, 0, $limit, array(':status' => 0)) : db_query($query, array(':status' => 0));
  21. foreach ($result as $message) {
  22. // Send the message.
  23. $status = views_send_deliver($message);
  24. if ($status) {
  25. // Update the spool status.
  26. db_query("UPDATE {views_send_spool} SET status = :status WHERE eid = :eid", array(':status' => 1, ':eid' => $message->eid));
  27. if (variable_get('views_send_debug', FALSE)) {
  28. watchdog('views_send', 'Message sent to %mail.', array('%mail' => $message->to_mail));
  29. }
  30. if (module_exists('rules')) {
  31. rules_invoke_event('views_send_email_sent', $message);
  32. }
  33. $ok++;
  34. }
  35. else {
  36. // Increment tentatives so that next time this message
  37. // will be scheduled with low priority.
  38. db_query("UPDATE {views_send_spool} SET tentatives = tentatives + 1 WHERE eid = :eid", array(':eid' => $message->eid));
  39. $fail++;
  40. }
  41. // Check the elapsed time and break if we've spent more than 80%.
  42. // We check every 50 messages.
  43. if (++$check >= 50) {
  44. // Reset the counter.
  45. $check = 0;
  46. // Break if exceded.
  47. if (timer_read('views_send') / 1000 > .8 * $max_execution_time) {
  48. watchdog('views_send', 'PHP maximum execution time almost exceeded. Remaining e-mail messages will be sent during the next cron run. If this warning occurs regularly you should reduce the cron throttle setting.', NULL, WATCHDOG_WARNING);
  49. break;
  50. }
  51. }
  52. }
  53. if ($ok + $fail > 0) {
  54. // Log results and exit.
  55. watchdog('views_send', '%ok messages sent in %sec seconds, %fail failed sending.',
  56. array('%ok' => $ok, '%sec' => timer_read('views_send') / 1000, '%fail' => $fail)
  57. );
  58. }
  59. }
  60. /**
  61. * Clear the expired items from spool.
  62. */
  63. function views_send_clear_spool() {
  64. // TODO: Drupal 7: replace time() with REQUEST_TIME.
  65. $expiration_time = time() - variable_get('views_send_spool_expire', 0) * 86400;
  66. db_query("DELETE FROM {views_send_spool} WHERE status = :status AND timestamp <= :expiry", array(':status' => 1, 'expiry' => $expiration_time));
  67. }