File indexing completed on 2024-12-22 05:33:57

0001 <?php
0002 
0003 /**
0004  *  ocs-webserver
0005  *
0006  *  Copyright 2016 by pling GmbH.
0007  *
0008  *    This file is part of ocs-webserver.
0009  *
0010  *    This program is free software: you can redistribute it and/or modify
0011  *    it under the terms of the GNU Affero General Public License as
0012  *    published by the Free Software Foundation, either version 3 of the
0013  *    License, or (at your option) any later version.
0014  *
0015  *    This program is distributed in the hope that it will be useful,
0016  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
0017  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0018  *    GNU Affero General Public License for more details.
0019  *
0020  *    You should have received a copy of the GNU Affero General Public License
0021  *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
0022  **/
0023 class Queue_CommandController extends Local_Controller_Action_CliAbstract
0024 {
0025 
0026     const DEFAULT_MSG_TIMEOUT = 600000; // default timeout in microseconds
0027     const DEFAULT_MSG_COUNT = 1;
0028     const QUEUE_TYPE = 'validate';
0029 
0030     /** @var  Zend_Queue */
0031     protected $queue;
0032     /** @var  Zend_Config */
0033     protected $config;
0034     /** @var int $timeout */
0035     protected $timeout;
0036     /** @var int $message_count */
0037     protected $message_count;
0038 
0039     public function __construct(
0040         Zend_Controller_Request_Abstract $request,
0041         Zend_Controller_Response_Abstract $response,
0042         array $invokeArgs = array()
0043     ) {
0044         parent::__construct($request, $response, $invokeArgs);
0045         $this->config = Zend_Registry::get('config');
0046         $this->timeout = isset($this->config->queue->general->timeout) ? $this->config->queue->general->timeout
0047             : self::DEFAULT_MSG_TIMEOUT;
0048         $this->message_count =
0049             isset($this->config->queue->general->message_count) ? $this->config->queue->general->message_count
0050                 : self::DEFAULT_MSG_COUNT;
0051     }
0052 
0053     public function runAction()
0054     {
0055         $queue = $this->initQueue($this->getParam('q'));
0056 
0057         /** @var Zend_Queue_Message_Iterator $messages */
0058         $messages = $queue->receive($this->message_count, $this->timeout);
0059         /** @var Zend_Queue_Message $message */
0060         foreach ($messages as $message) {
0061             $cmdObject = unserialize($message->body);
0062             if ($cmdObject instanceof Local_Queue_CommandInterface) {
0063                 try {
0064                     $cmdObject->doCommand();
0065                 } catch (Exception $e) {
0066                     Zend_Registry::get('logger')->err(__METHOD__ . " - " . PHP_EOL . 'MESSAGE::    ' . $e->getMessage()
0067                         . PHP_EOL . 'ENVIRONMENT::' . APPLICATION_ENV . PHP_EOL . 'TRACE_STRING::' . PHP_EOL
0068                         . $e->getTraceAsString() . print_r($cmdObject, true) . PHP_EOL)
0069                     ;
0070                 }
0071                 $queue->deleteMessage($message);
0072             } else {
0073                 Zend_Registry::get('logger')->err(__METHOD__ . " - Unknown command - " . print_r($message->body, true)
0074                     . PHP_EOL)
0075                 ;
0076 
0077                 $queue->deleteMessage($message);
0078                 trigger_error('Unknown command: ' . print_r($message->body, true), E_USER_ERROR);
0079             }
0080         }
0081     }
0082 
0083     /**
0084      * @param string $identifier
0085      *
0086      * @return Zend_Queue
0087      */
0088     protected function initQueue($identifier)
0089     {
0090         return Local_Queue_Factory::getQueue($identifier);
0091     }
0092 
0093 }