Пример #1
0
 protected function _getDownloadListFromWorker()
 {
     $zmsg = new Zmsg($this->_socketToWorker);
     $zmsg->recv();
     $this->_downloadList = unserialize($zmsg->body());
     $this->log('received downloadlist from worker, nr of elements: %d', count($this->_downloadList));
 }
Пример #2
0
 public function getInstance(\ZMQ\Zmsg $zmsg)
 {
     $message = unserialize($zmsg->body());
     $class = get_class($message);
     $handlerClass = str_replace('Message', 'MessageHandler', $class);
     if (!isset($this->_messageHandlerCache[$handlerClass])) {
         if (false === class_exists($handlerClass)) {
             throw new \UnexpectedValueException(sprintf('invalid handlerClass %s', $handlerClass));
         }
         $handler = new $handlerClass($this->_config, $this->_queueManager);
         $this->_messageHandlerCache[$handlerClass] = $handler;
     }
     $this->_messageHandlerCache[$handlerClass]->setMessage($zmsg, $message);
     return $this->_messageHandlerCache[$handlerClass];
 }
Пример #3
0
 protected function _processingLoop()
 {
     $poll = new \ZMQPoll();
     $poll->add($this->_socketToQueueManager, \ZMQ::POLL_IN);
     $pollTimeout = $this->_config->get('worker.pollTimeout');
     while (true) {
         $readable = $writable = array();
         $events = $poll->poll($readable, $writable, $pollTimeout);
         if ($events) {
             foreach ($readable as $socket) {
                 $zmsg = new Zmsg($socket);
                 $zmsg->recv();
                 $msg = unserialize($zmsg->body());
                 if ($msg instanceof \Daemon\Message\Shutdown) {
                     $this->log('received shutdown message');
                     $this->_initShutdown();
                     return;
                 }
                 try {
                     /** @var $task \Daemon\Task\AbstractTask */
                     $task = $msg->task;
                     $this->_currentTask = $task;
                     $this->log('starting to execute %s', get_class($task));
                     $task->setContext($this->_context);
                     $task->setConfig($this->_config);
                     $task->setProcess($this);
                     $task->run();
                     $this->_currentTask = null;
                     $this->log('execution of %s finished', get_class($task));
                     $response = new Message\Task\Finished(array('task' => $task, 'workerAddress' => $this->_identity));
                     $this->_responseToQueueManager($zmsg, $response);
                     if ($task->hasMessagesToQueueManager()) {
                         $responseMessages = $task->getMessagesToQueueManager();
                         foreach ($responseMessages as $responseMessage) {
                             $this->_responseToQueueManager($zmsg, $responseMessage);
                         }
                     }
                 } catch (\Exception $e) {
                     $response = new Message\Task\Failed(array('task' => $task, 'exception' => $e, 'workerAddress' => $this->_identity));
                     $this->_responseToQueueManager($zmsg, $response);
                 }
             }
         }
         $this->_sendHeartbeat();
     }
 }
Пример #4
0
 /**
  * Wait for replies from the server after the jobs have been processed and
  * then return an array of processed jobs.
  *
  * @param ZMQSocket $req
  * @param int $totalJobs
  *
  * @return array
  */
 protected function waitForReplies(ZMQSocket $req, $totalJobs)
 {
     $completed = array();
     $read = $write = array();
     $poll = new ZMQPoll();
     $poll->add($req, ZMQ::POLL_IN);
     while (true) {
         $events = $poll->poll($read, $write, 10000);
         if ($events) {
             $zmsg = new Zmsg($req);
             $zmsg->recv();
             $this->log(__CLASS__, "Job complete reply received");
             $job = unserialize($zmsg->body());
             $completed[] = $job;
             if (count($completed) == $totalJobs) {
                 $this->log(__CLASS__, "All jobs done");
                 return $completed;
             }
         }
     }
 }
Пример #5
0
 protected function _run()
 {
     $this->_forkDownloader();
     $bindTo = sprintf('ipc:///tmp/downloader-%d.ipc', $this->_data['download_bundle_id']);
     $socketToDownloader = $this->_context->getSocket(\ZMQ::SOCKET_PAIR);
     $socketToDownloader->bind($bindTo);
     $this->_process->log('bind socket to %s', $bindTo);
     $zmsg = new Zmsg($socketToDownloader);
     $zmsg->body_set(serialize($this->_data['download_list']));
     $zmsg->send();
     $zmsg->recv();
     $this->_process->log('received message from downloader');
     /** @var $msg \SAP\Daemon\Message\Download\CheckResult */
     $msg = unserialize($zmsg->body());
     if (!$msg->hasNotReachableDownloads()) {
         $this->_setResult(array('success' => true, 'message' => 'downloads started successfully'));
     } else {
         $this->_setResult(array('success' => false, 'message' => 'reachability check of downloads failed', 'not-reachable-downloads' => $msg->getNotReachableDownloadsWithReason()));
     }
     unset($socketToDownloader);
 }
Пример #6
0
 protected function _shutdownProcessingLoop()
 {
     while (count($this->_registeredWorkers) > 0) {
         $this->_sendShutdownMessageToAllAvailableWorkers();
         $poll = $this->_generatePoll();
         $readable = $writable = array();
         $events = $poll->poll($readable, $writable);
         if ($events) {
             foreach ($readable as $socket) {
                 $zmsg = new Zmsg($socket);
                 $zmsg->recv();
                 $msg = unserialize($zmsg->body());
                 $allowedMessages = array('Daemon\\Message\\Worker\\Shutdown\\Regular', 'Daemon\\Message\\Worker\\Shutdown\\Unexpected', 'Daemon\\Message\\Task\\Finished', 'Daemon\\Message\\Task\\Failed');
                 $msgClass = get_class($msg);
                 if (!in_array($msgClass, $allowedMessages)) {
                     $this->_sendNotAvailableWhileShutdownMessage($zmsg);
                     $this->log('received message from type %s while shutdown is running', $msgClass);
                     continue;
                 }
                 $msgHandler = $this->_messageHandlerFactory->getInstance($zmsg);
                 $msgHandler->handle();
                 $this->log('still %d workers registered', count($this->_registeredWorkers));
             }
         }
     }
 }
Пример #7
0
 /**
  * Run a self test of the Zmsg class.
  *
  * @return boolean
  * @todo See if assert returns
  */
 public static function test()
 {
     $result = true;
     $context = new ZMQContext();
     $output = new ZMQSocket($context, ZMQ::SOCKET_XREQ);
     $output->bind("inproc://zmsg_selftest");
     $input = new ZMQSocket($context, ZMQ::SOCKET_XREP);
     $input->connect("inproc://zmsg_selftest");
     //  Test send and receive of single-part message
     $zmsgo = new Zmsg($output);
     $zmsgo->body_set("Hello");
     $result &= assert($zmsgo->body() == "Hello");
     $zmsgo->send();
     $zmsgi = new Zmsg($input);
     $zmsgi->recv();
     $result &= assert($zmsgi->parts() == 2);
     $result &= assert($zmsgi->body() == "Hello");
     echo $zmsgi;
     //  Test send and receive of multi-part message
     $zmsgo = new Zmsg($output);
     $zmsgo->body_set("Hello");
     $zmsgo->wrap("address1", "");
     $zmsgo->wrap("address2");
     $result &= assert($zmsgo->parts() == 4);
     echo $zmsgo;
     $zmsgo->send();
     $zmsgi = new Zmsg($input);
     $zmsgi->recv();
     $result &= assert($zmsgi->parts() == 5);
     $zmsgi->unwrap();
     $result &= assert($zmsgi->unwrap() == "address2");
     $zmsgi->body_fmt("%s%s", 'W', "orld");
     $result &= assert($zmsgi->body() == "World");
     //  Pull off address 1, check that empty part was dropped
     $zmsgi->unwrap();
     $result &= assert($zmsgi->parts() == 1);
     //  Check that message body was correctly modified
     $part = $zmsgi->pop();
     $result &= assert($part == "World");
     $result &= assert($zmsgi->parts() == 0);
     echo $result ? "OK" : "FAIL", PHP_EOL;
     return $result;
 }