Example #1
1
 /**
  * {@inheritdoc}
  */
 public function pop()
 {
     if (!msg_receive($this->getQueue(), -time(), $eta, $this->itemMaxLength, $item, $this->serialize, MSG_IPC_NOWAIT, $errorCode)) {
         throw MSG_ENOMSG === $errorCode ? new NoItemAvailableException($this) : new QueueException($this, self::getErrorMessage($errorCode), $errorCode);
     }
     return $item;
 }
 public function actionDequeue($qId, $data)
 {
     $q = msg_get_queue($qId);
     $r = msg_receive($q, 1, $msgType, 10000, $msg);
     echo "Result: " . $r . "<br/>";
     echo " Got msg: " . $msg;
 }
Example #3
1
 function peek()
 {
     $this->init();
     $flags = $this->blocking && !$this->blockingTimeout ? 0 : MSG_IPC_NOWAIT;
     if (!$this->blockingTimeout || !$this->blocking) {
         msg_receive($this->seg, 1, $msgtype, $this->maxMsgSize, $message, false, $flags, $errno);
     } else {
         $timeout = new Scalr_Util_Timeout($this->blockingTimeout);
         try {
             while (!$message && !$timeout->reached()) {
                 if (!msg_receive($this->seg, 1, $msgtype, $this->maxMsgSize, $message, false, $flags, $errno)) {
                     $timeout->sleep(10);
                 }
             }
         } catch (Scalr_Util_TimeoutException $e) {
             return null;
         }
     }
     if ($message) {
         return unserialize($message);
     } else {
         if ($errno == MSG_ENOMSG && !$this->blocking) {
             return null;
         }
         if ($errno == 22) {
             return null;
         }
         throw new Scalr_System_Ipc_Exception($errno ? self::$msgrcv_errors[$errno] : "Cannot receive message", $errno);
     }
 }
 /**
  * 从消息队列中获取要监控的文件列表
  * @param bool $block
  * @return void
  */
 protected function collectFiles($block = false)
 {
     $msg_type = $message = null;
     $flag = $block ? 0 : MSG_IPC_NOWAIT;
     if (@msg_receive(\Man\Core\Master::getQueueId(), self::MSG_TYPE_FILE_MONITOR, $msg_type, 10000, $message, true, $flag)) {
         // 被排除的路径
         $exclude_path = array();
         // 因为配置可能会被更改,所以每次都会重新从配置中查找排除路径
         $config_exclude_path = $this->getExcludeFiles();
         foreach ($config_exclude_path as $path) {
             if ($real_path = realpath($path)) {
                 $exclude_path[] = $real_path;
             }
         }
         foreach ($message as $file) {
             $is_exclude_file = false;
             foreach ($exclude_path as $path) {
                 // 是被排除的文件
                 if (0 === strpos($file, $path)) {
                     $is_exclude_file = true;
                     break;
                 }
             }
             if (!$is_exclude_file && !isset($this->filesToInotify[$file])) {
                 $stat = @stat($file);
                 $mtime = isset($stat['mtime']) ? $stat['mtime'] : 0;
                 $this->filesToInotify[$file] = $mtime;
             }
         }
     }
 }
 /**
  *
  */
 public function readQueuedIssues()
 {
     // Read the entire queue and write all issues to the
     // base collector
     // Get the status of the queue
     $status = msg_stat_queue($this->message_queue_resource);
     // Read messages while there are still messages on
     // the queue
     while ($status['msg_qnum'] > 0) {
         $message = null;
         $message_type = 0;
         // Receive the message, populating $message by
         // reference
         if (false !== msg_receive($this->message_queue_resource, self::MESSAGE_TYPE_ISSUE, $message_type, 2048, $message, true)) {
             assert($message instanceof IssueInstance, "Messages must be of type IssueInstance.");
             // Cast the message to an IssueInstance
             if ($message instanceof IssueInstance) {
                 $this->collectIssue($message);
             }
         } else {
             break;
         }
         $status = msg_stat_queue($this->message_queue_resource);
     }
 }
Example #6
0
 /**
  * Waits for and receives a message
  * @param  integer $type
  * @return mixed
  */
 public function receive($type)
 {
     $rcvType = 0;
     $msg = '';
     msg_receive($this->queue, $type, $rcvType, $this->size, $msg);
     return $msg;
 }
Example #7
0
File: IPC.php Project: edoceo/radix
 static function mqRead($name, $want = 0, $wait = true)
 {
     $mq = msg_get_queue(self::_ftok($name));
     $type = null;
     // Recieved Message Type
     $size = 8192;
     // Max Message Size
     $mess = null;
     // Recieved Message Data
     $unser = true;
     $flags = 0;
     $error = null;
     if ($wait == false) {
         $flags |= MSG_IPC_NOWAIT;
     }
     if (msg_receive($mq, $want, $type, $size, $mess, $unser, $flags, $error)) {
         return $mess;
     }
     Radix::dump($mq);
     Radix::dump($want);
     Radix::dump($type);
     Radix::dump($size);
     Radix::dump($mess);
     Radix::dump($unser);
     Radix::dump($flags);
     Radix::dump($error);
     exit;
 }
Example #8
0
 private function process()
 {
     $messageType = NULL;
     $messageMaxSize = 1024;
     while (TRUE) {
         if (count($this->childs) < $this->max) {
             echo count($this->childs) . " ";
             if (msg_receive($this->queue, QUEUE_TYPE_START, $messageType, $messageMaxSize, $this->message)) {
                 $pid = pcntl_fork();
                 if ($pid == -1) {
                     die('could not fork' . PHP_EOL);
                 } else {
                     if ($pid) {
                         $this->childs[$pid] = TRUE;
                         $messageType = NULL;
                         $this->message = NULL;
                     } else {
                         sleep(3);
                         $this->complete($messageType, $this->message);
                         exit;
                     }
                 }
                 foreach ($this->childs as $pid => $value) {
                     if (pcntl_waitpid($pid, $status, WNOHANG)) {
                         if (pcntl_wifexited($status)) {
                             unset($this->childs[$pid]);
                         }
                     }
                 }
             }
         }
         sleep(1);
     }
 }
Example #9
0
 public function pickup()
 {
     if (msg_receive($this->queue, $this->msgtype_receive, $msgtype_erhalten, $this->maxsize, $daten, $this->serialize_needed, $this->option_receive, $err) === true) {
         return $daten;
     } else {
         //var_dump($err);
     }
 }
Example #10
0
 function pop()
 {
     $ret = msg_receive($this->msg, 0, $this->msgtype, 65525, $data);
     if ($ret) {
         return $data;
     }
     return false;
 }
Example #11
0
 public function receive($block = false, $serialize = false)
 {
     $data = [];
     $error = 0;
     $type = 1;
     $block = $block ? MSG_NOERROR : MSG_IPC_NOWAIT;
     $res = msg_receive($this->msg, $type, $type, 1024, $data, $serialize, $block, $error);
     return $res ? $data : null;
 }
Example #12
0
 /**
  * @return  string
  * @throws  \RuntimeException
  */
 protected function receiveMessage()
 {
     $msgtype = $message = null;
     // argument #3: specify the maximum number of bytes allowsed in one message queue.
     $success = msg_receive($this->id, 1, $msgtype, $this->stat['msg_qbytes'], $message, false);
     if (!$success) {
         throw new \RuntimeException('failed to receive message.');
     }
     return $message;
 }
Example #13
0
 public function receive($type = 0, &$msgtype = null)
 {
     if (!msg_receive($this->queue, $type, $msgtype, 65535, $msgdata, true, MSG_IPC_NOWAIT, $error)) {
         if ($error == MSG_ENOMSG) {
             return null;
         }
         debug("Error reading from IPC queue 0x%x: %s", $this->key, $error);
         return null;
     }
     return $msgdata;
 }
Example #14
0
 public function readAction()
 {
     $msg_id = msg_get_queue($this->_key, 0600);
     while (true) {
         if (msg_receive($msg_id, 1, $msg_type, 16384, $msg, true, 0, $msg_error)) {
             list($id, $msg) = split('!', $msg);
             echo json_encode(array('id' => $id, 'msg' => $msg));
         }
     }
     msg_remove_queue($msg_id);
 }
Example #15
0
 public function handle()
 {
     $messageQueueKey = ftok(App::path('cache') . "/queue/daemon.queue", "a");
     $messageQueue = msg_get_queue($messageQueueKey, 0666);
     $count = 0;
     while ($count < 5) {
         msg_receive($messageQueue, 0, $messageType, 1024, $message, true, MSG_IPC_NOWAIT);
         File::appendText(App::path('cache') . "/" . __CLASS__ . ".txt", $message);
         sleep(2);
     }
 }
Example #16
0
 public function get($queueName, $timeout = null)
 {
     if (null !== $timeout) {
         throw new \LogicException("Semaphore provider doesn't support timeout");
     }
     if (msg_receive($this->getQueue($queueName), 1, $type, 1024, $workload, true, MSG_IPC_NOWAIT)) {
         return $workload;
     } else {
         return null;
     }
 }
Example #17
0
 public function sendMessage($data)
 {
     //Generate a random ID for this request
     $id = rand();
     $message = [$id, $data];
     msg_send($this->queue, $this->serverId, $message, true, false);
     msg_receive($this->queue, $id, $msgtype, 1000000, $msg, true);
     foreach ($msg[0] as $header) {
         header($header);
     }
     return $msg[1];
 }
 public function read($desiredTopic = 0)
 {
     $this->initialize();
     switch ($this->method) {
         case null:
             throw new Exception('You have to setMethod() first');
             break;
         case self::QM_MSGQUEUE:
             $topic = 0;
             $msg = null;
             if (msg_receive($this->queueRes, $desiredTopic, &$topic, 16384, &$msg, true, MSG_IPC_NOWAIT | MSG_NOERROR)) {
                 return $msg;
             }
             return false;
             break;
         case self::QM_ZEROMQ:
             break;
         case self::QM_LIBEVENT:
             break;
         case self::QM_FILESOCKET:
             $socket = @socket_create(AF_UNIX, SOCK_STREAM, 0);
             if (!$socket) {
                 return false;
             }
             if (!@socket_connect($socket, $this->queuePath . 'queue.' . $this->queueId)) {
                 $err = socket_last_error();
                 if (ECONNREFUSED == $err) {
                     throw new Exception('Connection error. Queue-server (service.php) not running?');
                 }
             }
             if (false === socket_write($socket, serialize(array('topic' => $topic, 'msg' => $msg)))) {
                 return false;
             }
             socket_close($socket);
             break;
         case self::QM_FILESYS:
             $files = glob($this->queuePath . $desiredTopic . DS . 'q.*', GLOB_NOSORT);
             if (empty($files)) {
                 return null;
             }
             natsort($files);
             $file = array_shift($files);
             $fileContent = file_get_contents($file);
             @unlink($file);
             if (false !== $fileContent) {
                 return unserialize($fileContent);
             }
             return false;
             break;
     }
     //switch
 }
 /**
  * 从队列获取一个
  * @return bool
  * @throws Exception
  */
 public function get()
 {
     $queue_status = $this->status();
     if ($queue_status['msg_qnum'] > 0) {
         if (\msg_receive($this->queue, $this->msg_type, $msgtype_erhalten, $this->maxsize, $data, $this->serialize_needed, $this->option_receive, $err) === true) {
             return $data;
         } else {
             throw new \Exception($err);
         }
     } else {
         return false;
     }
 }
Example #20
0
 private function populateFromQueue()
 {
     $type = 1;
     $data = NULL;
     while (false !== msg_receive($this->queue, 1, $type, 16384, $data, false, MSG_IPC_NOWAIT)) {
         $uid = Common::substrUntil($data, ':');
         $message = Common::substrFrom($data, ':');
         echo "GOT {$uid}:{$message}\n";
         if (false !== ($socket = $this->getSocketByResourceB($uid))) {
             $frame = WebSocketFrame::create(WebSocketOpcode::TextFrame, $message);
             $socket->write($frame->encode());
         }
     }
 }
Example #21
0
 /**
  * 从消息队列中获取要监控的文件列表
  * @param bool $block
  * @return void
  */
 protected function collectFiles($block = false)
 {
     $msg_type = $message = null;
     $flag = $block ? 0 : MSG_IPC_NOWAIT;
     if (msg_receive(\Man\Core\Master::getQueueId(), self::MSG_TYPE_FILE_MONITOR, $msg_type, 10000, $message, true, $flag)) {
         foreach ($message as $file) {
             if (!isset($this->filesToInotify[$file])) {
                 $stat = @stat($file);
                 $mtime = isset($stat['mtime']) ? $stat['mtime'] : 0;
                 $this->filesToInotify[$file] = $mtime;
             }
         }
     }
 }
Example #22
0
 public function run()
 {
     $message_queue_key = ftok(__FILE__, 'a');
     $message_queue = msg_get_queue($message_queue_key, 0666);
     do {
         if (($msgsock = socket_accept($this->socket)) < 0) {
             $this->addLog("socket_accept() failed! reason: " . socket_strerror($msgsock));
             break;
         } else {
             $zhanArr = array();
             $this->addLog("parent start, pid:" . getmypid());
             for ($i = 0; $i < 3; $i++) {
                 $pid = pcntl_fork();
                 if ($pid == -1) {
                     die("cannot fork");
                 } else {
                     if ($pid > 0) {
                         $this->addLog("parent continue, pid:" . getmypid());
                         while (true) {
                             $buf = socket_read($msgsock, 10000);
                             if (!empty($buf)) {
                                 $sendRet = msg_send($message_queue, 1, $buf);
                                 if ($sendRet) {
                                     $this->addLog("send msg to queue ok!send message : " . $buf);
                                 } else {
                                     $this->addLog("send msg to queue failed!!!");
                                 }
                             } else {
                                 $this->addLog("no message");
                                 sleep(2);
                             }
                         }
                     } else {
                         if ($pid == 0) {
                             while (true) {
                                 msg_receive($message_queue, 0, $message_type, 1024, $message, true, MSG_IPC_NOWAIT);
                                 if (!empty($message)) {
                                     $this->addLog("receive message:" . $message);
                                     $this->writeFile(WORK_PATH . '/abc.txt', $message);
                                 } else {
                                     sleep(10);
                                 }
                             }
                         }
                     }
                 }
             }
         }
     } while (true);
 }
Example #23
0
 function _initialize()
 {
     $IPCS = array();
     $_IPCS = explode('|', $this->ipcs);
     foreach ($_IPCS as $k => $v) {
         if ($k % $_GET['total'] == $_GET['mod']) {
             $IPCS[] = $v;
         }
     }
     print_r($IPCS);
     foreach ($IPCS as $ipcs) {
         $ic = $cs = 0;
         $seg = msg_get_queue($ipcs, 0600);
         $msgtype = 1;
         $msg_array = array();
         $monitor = array();
         //读取队列数据
         while (msg_receive($seg, $msgtype, $msgtype, 1024 * 1024 * 5, $msg_array, true, MSG_IPC_NOWAIT)) {
             $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['uptype'] = $msg_array['uptype'];
             if ($msg_array['uptype'] == 'replace') {
                 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['count'] = $msg_array['num'];
             } else {
                 $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['count'] += $msg_array['num'];
             }
             //最大耗时
             $monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['diff_time'] = max($monitor[date('Y-m-d H', strtotime($msg_array['time']))][$msg_array['v1']][$msg_array['v2']][$msg_array['v3']][$msg_array['v4']][$msg_array['v5']]['diff_time'], $msg_array['diff_time']);
             if ($ic++ > 15 * 10000) {
                 break;
             }
         }
         //压缩回去
         foreach ($monitor as $time => $vtype) {
             foreach ($vtype as $type => $vhost) {
                 foreach ($vhost as $host => $vact) {
                     foreach ($vact as $act => $vkey) {
                         foreach ($vkey as $key => $vhostip) {
                             foreach ($vhostip as $hostip => $v) {
                                 $cs++;
                                 _status($v['count'], $type, $host, $act, $key, $hostip, abs($v['diff_time']), $v['uptype'], strtotime($time . ":00:00"));
                             }
                         }
                     }
                 }
             }
         }
         _status(($ic - $cs) / $ic * 100, VHOST . '(WEB日志分析)', '队列', '压缩比例', $ipcs, VIP, 0, 'replace');
         unset($monitor);
     }
 }
 public function drop()
 {
     try {
         $q = msg_get_queue($this->id, ONPHP_IPC_PERMS);
     } catch (BaseException $e) {
         // removed in race
         return true;
     }
     if (!msg_remove_queue($q)) {
         // trying to flush manually
         $type = $msg = null;
         while (msg_receive($q, 0, $type, 2, $msg, false, MSG_IPC_NOWAIT)) {
             // do nothing
         }
     }
     return true;
 }
Example #25
0
 public function run($callback, $msg_expected = false)
 {
     if (!is_callable($callback)) {
         throw new Exception("Invalid callback argument");
     }
     $flags = $this->wait_for_message ? 0 : MSG_IPC_NOWAIT;
     // Starts to listen for messages
     while (1) {
         if (!msg_receive($this->queue_id, 0, $type, 16384, $message, true, $flags, $err)) {
             throw new Exception("From msg_receive: {$err}");
             continue;
         }
         if ($callback($message, $type) === false) {
             break;
         }
     }
 }
Example #26
0
 private function showAllDaemon()
 {
     $messageQueueKey = ftok(App::path('cache') . "/queue/daemon.queue", "a");
     $messageQueue = msg_get_queue($messageQueueKey, 0666);
     $messageQueueState = msg_stat_queue($messageQueue);
     $msgCount = $messageQueueState['msg_qnum'];
     if (0 === $msgCount) {
         Console::line("None service is running.");
     }
     while ($msgCount > 0) {
         /** @var MessageQueue $message */
         msg_receive($messageQueue, 0, $messageType, 1024, $message, true, MSG_IPC_NOWAIT);
         Console::line("PID:{$message->pid},NAME:{$message->name},TIME:" . date("Y-m-d H:i:s", $message->timestamp) . "Alive.");
         $messageQueueState = msg_stat_queue($messageQueue);
         $msgCount = $messageQueueState['msg_qnum'];
     }
 }
Example #27
0
 public function consume(callable $callback)
 {
     $queueResource = $this->queue->getResource();
     $receivedMessageType = null;
     $messageMaxSize = $this->queue->getMessageAllowedSize();
     $receivedMessage = null;
     $unserializeMessage = true;
     $flags = 0;
     $errorCode = 0;
     $result = @msg_receive($queueResource, self::CONSUMER_DEFAULT_MESSAGE_TYPE, $receivedMessageType, $messageMaxSize, $receivedMessage, $unserializeMessage, $flags, $errorCode);
     if (false === $result) {
         throw new Exception\MessageBufferSizeOverflowException();
     }
     if (self::CONSUMER_DEFAULT_MESSAGE_TYPE != $receivedMessageType) {
         continue;
     }
     call_user_func_array($callback, array($receivedMessage, $this->eventDispatcher));
 }
Example #28
0
 public static function receive($queue)
 {
     $queue_id = self::get_queue_id($queue);
     if (!$queue_id) {
         return false;
     }
     $mqueue = self::get_msg_queue($queue_id);
     if (!$mqueue) {
         return false;
     }
     $msg = '';
     $msg_type = 0;
     if (msg_receive($mqueue, 1, $msg_type, 65535, $msg, false)) {
         return $msg;
     } else {
         return false;
     }
 }
 /**
  * The class destructor.
  */
 public function __destruct()
 {
     parent::__destruct();
     if ($this->iAmParent()) {
         $desiredType = 1;
         $option_receive = MSG_IPC_NOWAIT;
         $stats = msg_stat_queue($this->queueResourceResponse);
         $queueMessageSize = $stats['msg_qbytes'];
         // the Report
         $messageReport = '';
         // receive the report
         $stats = msg_stat_queue($this->queueResourceResponse);
         while ($stats['msg_qnum']) {
             //for($i=0;count($all_servers);$i++){
             $status = msg_receive($this->queueResourceResponse, $desiredType, $type, $queueMessageSize, $mixed, true, $option_receive);
             if ($status == true) {
                 $messageReport .= $mixed['message'];
                 if ($mixed['return'] < 0) {
                     // check if server counldn't do the backups because shutdown doesn't work
                     if ($mixed['error'] && $mixed['error']['error']['error'] == '_ERR_VM_BACKUP_STILL_RUNNING_') {
                         $etva_server = $mixed['server'];
                         $this->log("[WARN] Receive error that VM '" . $etva_server->getName() . "' couldn't make backup beacuse is still running, so i will try start again...");
                         $etva_node = $etva_server->getEtvaNode();
                         if ($etva_node) {
                             $res_start = $etva_node->soapSend(EtvaServer_VA::SERVER_START, array('uuid' => $etva_server->getUuid(), 'name' => $etva_server->getName()));
                             $this->log("[WARN] Start '" . $etva_server->getName() . "' VM and receive the following message: " . print_r($res_start, true));
                         }
                     }
                 }
             } else {
                 $err_m = "[ERROR] Receive error when wait for response... " . print_r($err, true);
                 $messageReport .= $err_m . "\r\n";
                 $this->log($err_m);
             }
             $stats = msg_stat_queue($this->queueResourceResponse);
         }
         msg_remove_queue($this->queueResourceResponse);
         //$this->log($messageReport);
         $this->sendReport($messageReport);
     }
 }
Example #30
0
 private function createFork($msgid, $lifetime = 10)
 {
     $pid = pcntl_fork();
     sleep(0.001);
     if (!$pid) {
         set_time_limit($lifetime);
         while (true) {
             //Remove previously sent headers
             header_remove();
             msg_receive($this->queue, $msgid, $msgtype, 1024 * 50, $message, true);
             if ($message == 'shutdown') {
                 exit;
             }
             list($id, $data) = $message;
             $output = $this->application->accept($msgid, $data['sessionId'], $data['get'], $data['post'], $data['server'], $data['files'], $data['cookie']);
             msg_send($this->queue, $id, [headers_list(), $output], true, true);
             //Force GC to prevent memory leaks! Without this, the process will grow and grow
             gc_collect_cycles();
         }
     }
 }