/**
  * @param array $message The data
  * @param Exception $ex The problem
  * @param int| null $retryDate If provided, retry after this timestamp
  * @return int ID of message in damaged database
  */
 protected function sendToDamagedStore($message, Exception $ex, $retryDate = null)
 {
     if ($retryDate) {
         Logger::notice('Message not fully baked. Sticking it back in the oven, to ' . "retry at {$retryDate}", $message);
     } else {
         Logger::error('Error processing message, moving to damaged store.', $message, $ex);
     }
     return $this->damagedDb->storeMessage($message, $this->queueName, $ex->getMessage(), $ex->getTraceAsString(), $retryDate);
 }
 public function setUp()
 {
     parent::setUp();
     Context::initWithLogger(QueueTestConfiguration::instance());
     $this->queue = BaseQueueConsumer::getQueue('test');
     $this->queue->createTable('test');
     $damagedDb = DamagedDatabase::get();
     $damagedDb->createTable();
     $this->damaged = $damagedDb->getDatabase();
 }
 public function testDeleteMessage()
 {
     $uniq = mt_rand();
     $queue = 'test_queue';
     $message1 = $this->getTestMessage($uniq);
     // Store a second message for a good time, and make sure we delete the
     // right one.
     $message2 = $this->getTestMessage($uniq);
     $this->db->storeMessage($message1, $queue);
     // store message 2 with a
     $this->db->storeMessage($message2, $queue);
     // Confirm work without using the API.
     $pdo = $this->db->getDatabase();
     $result = $pdo->query("\n\t\t\tSELECT * FROM damaged\n\t\t\tWHERE gateway='test'\n\t\t\t\tAND order_id = '{$message1['order_id']}'");
     $rows = $result->fetchAll(PDO::FETCH_ASSOC);
     $this->assertEquals(2, count($rows), 'Both records were stored.');
     $this->assertNotNull($rows[0]['id'], 'Record includes a primary row id');
     $this->assertNotEquals($rows[0]['id'], $rows[1]['id'], 'Records have unique primary ids');
     $message2['damaged_id'] = $rows[1]['id'];
     $this->db->deleteMessage($message2);
     // Confirm work without using the API.
     $pdo = $this->db->getDatabase();
     $result = $pdo->query("\n\t\t\tSELECT * FROM damaged\n\t\t\tWHERE gateway='test'\n\t\t\t\tAND order_id = '{$message1['order_id']}'");
     $rowsAfter = $result->fetchAll(PDO::FETCH_ASSOC);
     $this->assertEquals(1, count($rowsAfter), 'Not only one row deleted.');
     $this->assertEquals($rowsAfter[0]['id'], $rows[0]['id'], 'Deleted the wrong row.');
 }
 /**
  * Do the actual work of the script.
  */
 public function execute()
 {
     $this->damagedDatabase = DamagedDatabase::get();
     $messages = $this->damagedDatabase->fetchRetryMessages($this->getOption('max-messages'));
     $stats = array();
     $config = Configuration::getDefaultConfig();
     foreach ($messages as $message) {
         $queueName = $message['original_queue'];
         // FIXME: getting it by alias, this will be annoying cos -new
         $queue = BaseQueueConsumer::getQueue($queueName);
         unset($message['original_queue']);
         $queue->push($message);
         $this->damagedDatabase->deleteMessage($message);
         if (isset($stats[$queueName])) {
             $stats[$queueName]++;
         } else {
             $stats[$queueName] = 1;
         }
     }
     foreach ($stats as $queueName => $count) {
         Logger::info("Requeued {$count} messages to {$queueName}.");
     }
 }