Process mail queue immediately after a mail posted in it

Registered by Andrejs

I propose to replace a cron based mail processing by event based. The solution increases an email speed delivery. I have done small changes on my site and ready to share its. At the same time changes allows safely deliver emails in multi node (redundant) environment where a file lock does not work.

Blueprint information

Status:
Not started
Approver:
None
Priority:
Undefined
Drafter:
None
Direction:
Needs approval
Assignee:
None
Definition:
New
Series goal:
None
Implementation:
Unknown
Milestone target:
None

Related branches

Sprints

Whiteboard

To initiate mail delivery we have to add following code into class "mail_queue" method "add"

        $fp = fopen(APP_BASE_URL . '/process_mail_queue.php', 'rb');
        if ($fp) {
          fclose($fp) ;
        }

process_mail_queue.php changes:

@@ -75,13 +74,27 @@
     exit(0);
 }

-if (!Lock::acquire('process_mail_queue')) {
- $pid = Lock::getProcessID('process_mail_queue');
- fwrite(STDERR, "ERROR: There is already a process (pid=$pid) of this script running.");
- fwrite(STDERR, "If this is not accurate, you may fix it by running this script with '--fix-lock' as the only parameter.\n");
- exit(1);
+$res = DB_Helper::getInstance()->getCol("SELECT GET_LOCK('process_mail_queue',0)");
+if (PEAR::isError($res)) {
+ Error_Handler::logError(array($res->getMessage(), $res->getDebugInfo()), __FILE__, __LINE__);
+ exit(1);
 }

+if ($res[0]==0) {
+ echo "can't get the lock\n" ;
+ exit(1);
+}
+
+# send "CLOSE" command to a browser
+ignore_user_abort();
+header("Content-Length: 0");
+header('Connection: close');
+echo "\n" ;
+flush();
+
+# wait few seconds, possibly new emails will be enqueued
+sleep(3);
+
 // handle only pending emails
 $limit = 50;
 Mail_Queue::send('pending', $limit);
@@ -90,4 +103,4 @@
 $limit = 50;
 Mail_Queue::send('error', $limit);

-Lock::release('process_mail_queue');
+DB_Helper::getInstance()->query("SELECT RELEASE_LOCK('process_mail_queue')");

(?)

Work Items

This blueprint contains Public information 
Everyone can see this information.

Subscribers

No subscribers.