private function getQueueSizeSimpleDB($queue) { $ds = SimpleDBMQ::get()->filter(array("QueueName" => $queue)); if (!$ds) { return 0; } return $ds->Count(); }
/** * @param String $queue * @param <type> $interfaceConfig * @param <type> $options * @return <type> */ public function receive($queue, $interfaceConfig, $options) { $result = new ArrayList(); $limit = $options && isset($options["limit"]) ? $options["limit"] : null; $conn = DB::getConn(); // Work within a transaction if ($conn->supportsTransactions()) { $conn->transactionStart(); } try { $msgs = SimpleDBMQ::get(); if ($queue) { $msgs = $msgs->filter(array("QueueName" => $queue)); } if ($limit) { $msgs = $msgs->limit($limit, 0); } if (!$msgs) { return $result; } foreach ($msgs as $do) { $result->push(new MessageFrame($do->Message, unserialize($do->Header), $do->QueueName)); $do->delete(); $do->flushCache(); } // Commit transaction if ($conn->supportsTransactions()) { $conn->transactionEnd(); } } catch (Exception $e) { // Rollback if ($conn->supportsTransactions()) { $conn->transactionRollback(); } throw $e; } return $result; }
/** * PHP shutdown function for processing any queues that need to be processed/consumed * after the PHP process is done. For each queue that needs to be processed, * it starts a new sub-process for queue. */ static function consume_on_shutdown() { if (!self::$queues_to_flush_on_shutdown) { return; } if (self::$debugging_path) { $stdout = ">> " . self::$debugging_path . "/msgq.stdout"; $stderr = "2>>" . self::$debugging_path . "/msgq.stderr"; } else { $stdout = "> /dev/null"; $stderr = "2> /dev/null"; } // If we're debugging, dump the simpleDBMQ messages to the output. // This is the typical case, obviously not applicable if not using // simpledbmq, but the interface can't provide a guaranteed method to // get the messages without consuming them. if (self::$debugging_path) { $msgs = SimpleDBMQ::get(); if ($msgs) { `echo "messages currently in queue:\n" {$stdout}`; foreach ($msgs as $msg) { `echo " queue={$msg->QueueName} msg={$msg->Message}\n" {$stdout}`; } } } foreach (self::$queues_to_flush_on_shutdown as $queue => $dummy) { $config = MessageQueue::get_queue_config($queue); if (!isset($config["send"]) || !is_array($config["send"])) { throw new Exception("MessageQueue: unexpectedly invalid/absent send config on onShutdown"); } $sendConf = $config["send"] ? $config["send"] : array(); $opts = isset($sendConf["onShutdown"]) ? $sendConf["onShutdown"] : ""; if (is_string($opts)) { $opts = explode(",", $opts); } if (in_array("none", $opts)) { $opts = array(); } if (in_array("all", $opts)) { $opts = array("flush", "consume"); } $actions = implode(",", $opts); $maxMessagesPerProc = isset($sendConf["onShutdownMessageLimit"]) ? $sendConf["onShutdownMessageLimit"] : 0; $limitClause = $maxMessagesPerProc == 0 ? "" : "limit={$maxMessagesPerProc}"; $retriggerClause = isset($sendConf["retrigger"]) ? "retrigger=" . $sendConf["retrigger"] : ""; switch (self::$onshutdown_option) { case "sake": $exec = Director::getAbsFile("framework/sake"); `{$exec} MessageQueue_Process queue={$queue} actions={$actions} {$limitClause} {$retriggerClause} {$stdout} {$stderr} &`; break; case "phppath": $php = self::$onshutdown_arg; $framework = Director::getAbsFile("framework"); $cmd = "{$php} {$framework}/cli-script.php MessageQueue_Process queue={$queue} actions={$actions} {$limitClause} {$retriggerClause} {$stdout} {$stderr} &"; `{$cmd}`; if (self::$debugging_path) { `echo "queue is {$queue}\n" {$stdout}`; `echo "command was {$cmd}\n" {$stdout}`; } break; default: throw new Exception("MessageQueue::consume_on_shutdown: invalid option " . self::$queues_to_flush_on_shutdown); } } }