public function processQueueOnShutdown($newsletterID) { if (class_exists('MessageQueue')) { //start processing of email sending for this newsletter ID after shutdown MessageQueue::send("newsletter", new MethodInvocationMessage('NewsletterSendController', "process_queue_invoke", $newsletterID)); MessageQueue::consume_on_shutdown(); } else { // Do the sending in real-time, if there is not MessageQueue to do it out-of-process. // Caution: Will only send the first batch (see $items_to_batch_process), // needs to be continued manually afterwards, e.g. through the "restart queue processing" // in the admin UI. $this->processQueue($newsletterID); } }
/** * Test consume_all_queues using SimpleDB. We use a counter static method * and count the number of times its called, for messages on multiple * calls. */ function testMultipleConsume() { MessageQueue::add_interface("default", array("queues" => array("testmainqueue1", "testmainqueue2", "testmainqueue3"), "implementation" => "SimpleDBMQ", "encoding" => "php_serialize", "send" => array("onShutdown" => "none"), "delivery" => array("onerror" => array("requeue")))); $this->assertTrue($this->getQueueSizeSimpleDB("testmainqueue1") == 0, "Queue 1 is empty before we put anything in it"); $this->assertTrue($this->getQueueSizeSimpleDB("testmainqueue2") == 0, "Queue 2 is empty before we put anything in it"); $this->assertTrue($this->getQueueSizeSimpleDB("testmainqueue3") == 0, "Queue 3 is empty before we put anything in it"); self::$countedCalls = 0; MessageQueue::send("testmainqueue1", new MethodInvocationMessage("MessageQueueTest", "doCountCalls")); MessageQueue::send("testmainqueue2", new MethodInvocationMessage("MessageQueueTest", "doCountCalls")); MessageQueue::send("testmainqueue3", new MethodInvocationMessage("MessageQueueTest", "doCountCalls")); $this->assertTrue(MessageQueue::consume_all_queues("default") == 3, "Consumed messages off 3 queues"); $this->assertTrue(self::$countedCalls == 3, "3 messages were delivered"); }
public function triggerReindex(LoggerInterface $logger, $batchSize, $taskName, $classes = null) { $queue = Config::inst()->get(__CLASS__, 'reindex_queue'); $logger->info('Queuing message'); MessageQueue::send($queue, new MethodInvocationMessage('SolrReindexMessageHandler', 'run_reindex', $batchSize, $taskName, $classes)); }
/** * Determine how to deliver the message. This depends on what's in the interface configuration, delivery section. * If a callback is supplied, that is executed. * @param <type> $msg * @param <type> $conf * @TODO: would be easy to generalise onerror into a list of these actions that are * done in sequence. e.g. log an error and then requeue it. * @TODO: would be good to have a failure count on the error message. Not sure how to * represent this generally. */ static function deliver_message($msgframe, $conf) { if (!$conf || !isset($conf["delivery"])) { throw new Exception("deliver_message failed because it was not passed valid configuration with delivery section"); } $del = $conf["delivery"]; try { if (isset($del["requeue"])) { // delivery means stick it in another queue. This is expected to be an array with at least a queue name if (!is_array($del["requeue"])) { throw new Exception("delivery of message failed because it specifies requeue, but it is not the expected array"); } $newQueue = isset($del["requeue"]["queue"]) ? $del["requeue"]["queue"] : null; if (!$newQueue) { throw new Exception("delivery of message failed because it specified requeue, but doesn't specify a queue"); } $newConf = MessageQueue::get_queue_config($newQueue); if (isset($del["requeue"]["immediate"]) && $del["requeue"]["immediate"]) { // Immediate execution - get the configuration for the queue, and recurse to deliver immediately. MessageQueue::deliver_message($msgframe, $newConf); } else { // Not immediate, so put this message on the specified queue, and it will hopefully get delivered at // some later time. MessageQueue::send($newConf, $msgframe); } } else { if (isset($del["callback"])) { // delivery is via a callback call_user_func_array($del["callback"], array($msgframe, $conf)); } else { if (is_object($msgframe->body) && $msgframe->body instanceof MessageExecutable) { $msgframe->body->execute($msgframe, $conf); } else { throw new Exception("delivery of message failed because there is no specification of what to do with it"); } } } } catch (Exception $e) { // Look at the config to determine what to do with a failed message. $onerror = isset($del["onerror"]) ? $del["onerror"] : "drop"; if (!is_array($onerror)) { $list = array($onerror); } else { $list = $onerror; } // There are two types of entry. Those with numeric keys have the action // as the value. Those with non-numeric keys have the key as the action, // and the value as an argument. foreach ($list as $key => $value) { if (is_numeric($key)) { $action = $value; $arg = null; } else { $action = $key; $arg = $value; } switch ($action) { case "drop": break; // do nothing // do nothing case "requeue": if (!$arg) { $arg = $msgframe->queue; } // requeue on the same queue if not specified. MessageQueue::send($arg, $msgframe->body, $msgframe->header); break; case "log": SS_Log::log($e, null); echo $e->getMessage(); break; case "callback": if (!$arg) { throw new Exception("delivery of message failed with error callback indicated, but no callback function supplied"); } call_user_func_array($arg, array($e, $msgframe)); break; default: throw new Exception("Invalid onerror action '{$action}'"); } } } }
public function triggerProcessing() { MessageQueue::send(Config::inst()->get('SearchMessageQueueUpdater', 'reindex_queue'), new MethodInvocationMessage($this, "process")); }