public function postAction() { $line = $_REQUEST['msg']; $id = $_REQUEST['id']; $msg_id = msg_get_queue($this->_key, 0600); die(json_encode(msg_send($msg_id, 1, "{$id}!{$line}", true, true, $msg_err))); }
/** * @param $strMessage - message to send to the queue * @param $intErrorCode - reference to error-code, which is set in case of error * * @throw \Exception - if message is not a string * * @returns (boolean) true, if message was successfully send to queue * @returns (boolean) false, if error occured * * send the given message to the queue. if the message is to big, * the sending is blocked, until there is enough space in queue * to send the message. * * if an error occurs, the parameter $intErrorCode will contain * the error-code. * **/ public function send($strMessage, &$intErrorCode = null) { if (!is_string($strMessage)) { throw new \Exception("given parameter must be a string"); } return msg_send($this->resQueue, 1, $strMessage, false, true, $intErrorCode); }
public function send($message, $type = 1) { if (!@msg_send($this->queue_id, $type, $message, true, false, $err)) { throw new Exception("From msg_send: {$err}"); } return true; }
public function shutdown() { $this->queue = msg_get_queue(ftok($this->file, 'R'), 0777); for ($i = 0; $i < $this->numThreads * 2; $i++) { msg_send($this->queue, 100 + $i, 'shutdown'); } }
public static function log($message, $type = '') { $contents = trim($message); if (!$contents) { return false; } if (empty($type)) { $type = 'common'; } $queue = self::getInstance(); $msg = json_encode(array('msg' => $contents, 'type' => $type)); msg_send($queue, 1, $msg, false, false, $errorcode); $unix_time = time(); $Y = date("Y", $unix_time); $m = date("m", $unix_time); $d = date("d", $unix_time); $H = date("H", $unix_time); $log_dir = self::LC_LOG_PATH . "/" . $type . "/{$Y}/{$m}/{$d}"; if (!is_dir($log_dir)) { @mkdir($log_dir, 0777, true); } $log = $log_dir . "/" . $type . '.' . $H . '.' . "log"; file_put_contents($log, "{$contents}\n", FILE_APPEND); return true; }
/** * {@inheritdoc} */ public function push($item, $eta = null) { $eta = QueueUtils::normalizeEta($eta); if (!msg_send($this->getQueue(), $eta, $item, $this->serialize, false, $errorCode)) { throw new QueueException($this, self::getErrorMessage($errorCode), $errorCode); } }
function put($data) { $this->init(); if (!@msg_send($this->seg, 1, serialize($data), false, false, $errno)) { throw new Scalr_System_Ipc_Exception($errno ? self::$msgsnd_errors[$errno] : "Cannot send message", $errno); } return true; }
public function send($message) { $result = msg_send($this->queue, 1, $message); if ($result == False) { print "end error" . PHP_EOL; } return $result; }
/** Put a Message Into the Queue */ static function mqSend($name, $type, $mess) { $mq = msg_get_queue(self::_ftok($name)); $block = true; $error = null; if (msg_send($mq, $type, $mess, true, $block, $error)) { return true; } }
public function send($data, $block = false, $serialize = false) { $error = 0; $type = 1; $r = msg_send($this->msg, $type, $data, $serialize, $block, $error); if (!$r) { } return $r; }
/** * Sends message * @param integer $type * @param string $msg * @return void */ public function send($type, $msg) { $err = 0; if (!msg_send($this->queue, $type, $msg, true, false, $err)) { $this->logger->critical("Message of type {$type} not sent: {$err}", (array) $msg); } else { $this->logger->debug("Message of type {$type} sent", (array) $msg); } }
public function putMessage(&$message) { $messageId = $this->storage->saveMessage($message); // notify queue via ipc $ipcStat = msg_stat_queue($this->ipcChannel); if (is_array($ipcStat) && $ipcStat['msg_qnum'] < 5) { msg_send($this->ipcChannel, 1, '*', false, false, $ipcError); } return $messageId; }
/** * addMessage: Given a key, store a new message into our queue. * * @param $key string - Reference to the message (PK) * @param $data array - Some data to pass into the message */ public static function addMessage($key, $data = array()) { # What to send $message = new Message($key, $data); # Try to send the message if (msg_send(self::$queue, QUEUE_TYPE_START, $message)) { print_r(msg_stat_queue(self::$queue)); } else { echo "Error adding to the queue"; } }
public static function send($queue, $msg = '') { $queue_id = self::get_queue_id($queue); if (!$queue_id) { return false; } $mqueue = self::get_msg_queue($queue_id); if (!$mqueue) { return false; } return msg_send($mqueue, 1, $msg, false, false); }
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 handle() { $messageQueueKey = ftok(App::path('cache') . "/queue/daemon.queue", "a"); $messageQueue = msg_get_queue($messageQueueKey, 0666); for ($i = 0; $i < 60; $i++) { $message = new MessageQueue(); $message->pid = $this->getPID(); $message->name = "IdleDaemon"; $message->timestamp = time(); msg_send($messageQueue, 1, $message); sleep(1); } }
function printt() { sem_acquire($this->msgSemKey); $mode = $this->settings['error_mode']['mode']; if ($this->id == 0) { if ($mode == 'ERROR') { sem_release($this->msgSemKey); return; } else { if ($mode == 'WARNING') { sem_release($this->msgSemKey); return; } else { if ($mode == 'INFO') { sem_release($this->msgSemKey); return; } } } } else { if ($this->id == 1) { if ($mode == 'ERROR') { sem_release($this->msgSemKey); return; } else { if ($mode == 'WARNING') { sem_release($this->msgSemKey); return; } } } else { if ($this->id == 3) { if ($mode == 'ERROR') { sem_release($this->msgSemKey); return; } } } } $x = $this->info; $time = microtime(true); $dFormat = "m/d/Y - H:i:s:"; $mSecs = $time - floor($time); $mSecs = substr($mSecs, 2, 4); $date = sprintf('%s%s', date($dFormat), $mSecs); $type = $this->settings['error_types'][$this->id]; msg_send($this->queKey, 1, "{$date} {$type} {$x}\n"); sem_release($this->msgSemKey); $this->checkConsumer(); }
/** * Sends a message to a message queue. * * @param Message $message A Message instance * @param bool $serialize If true, $message will be serialized * @param bool $blocking If true, the process will be blocked until another process reads messages * from the queue and frees enough space for your message to be sent, * otherwise will be thrown OverflowException * * @throws \OverflowException When a message queue is overwoled * @throws \RuntimeException When could not send a $message */ public function send(Message $message, $serialize = true, $blocking = true) { $this->checkState(); if (!@msg_send($this->handle, $message->getType(), $message->getData(), $serialize, $blocking, $errCode)) { if ($errCode === MSG_EAGAIN) { throw new \OverflowException(sprintf('Overflow a message queue with key "%s", not enough the space for a new message.', $this->key)); } $detail = null; if ($errCode === 22) { $detail = 'Maybe the message size is too big.'; } throw new \RuntimeException(sprintf('Could not send the message to a message queue with key "%s" (error code: %d). %s', $this->key, $errCode, $detail)); } }
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); }
public function publish($message) { if (empty($message)) { throw new Exception\EmptyMessageException(); } $queueResource = $this->queue->getResource(); $serializeMessage = true; $isBlocking = true; $errorCode = 0; $result = @msg_send($queueResource, self::PRODUCER_DEFAULT_MESSAGE_TYPE, $message, $serializeMessage, $isBlocking, $errorCode); if (false === $result) { $this->throwExceptionForErrorCode($errorCode); } return true; }
/** * Collect issue * @param IssueInstance $issue */ public function collectIssue(IssueInstance $issue) { $error_code = 0; // Send messages along to the message queue // that is hopefully being listened to by a // ParallelParentCollector. $success = msg_send($this->message_queue_resource, ParallelParentCollector::MESSAGE_TYPE_ISSUE, $issue, true, true, $error_code); // Send a signal to the parent process that we // sent a message and it may wish to collect it // posix_kill(posix_getppid(), SIGUSR1); // pcntl_signal_dispatch(); // Make sure that the message was successfully // sent assert($success, "msg_send failed with error code '{$error_code}'"); }
public function publish() { $resend = array(); if (is_array($this->message)) { foreach ($this->message as $value) { if (!msg_send($this->queue, $this->msgtype_send, $value, $this->serialize_needed, $this->block_send, $err)) { $resend[] = $value; } } } else { if (!msg_send($this->queue, $this->msgtype_send, $this->message, $this->serialize_needed, $this->block_send, $err)) { $resend[] = $value; } } return $resend; }
function sig_handler_parent($signo) { global $log; global $fork_pids; global $pid_logger; global $pid_contacts; global $msg_queue; global $config; if ($signo == SIGTERM || $signo == SIGHUP || $signo == SIGINT) { /* Terminate the daemon and all the associated threads We send a shutdown command for each thread and then wait for them to cleanly terminate. */ $log->debug("Shutdown POSIX signal recieved"); for ($i = 0; $i < count($fork_pids); $i++) { msg_send($msg_queue, MESSAGE_CTRL, "shutdown"); } foreach ($fork_pids as $pid) { $log->debug("Waiting for child processes to complete... ({$pid})"); pcntl_waitpid($pid, $status, WUNTRACED); } /* Clean Wrapup */ $log->debug("[master] Peak memory usage of " . memory_get_peak_usage() . " bytes"); $log->info("[master] Clean Shutdown"); // send shutdown to contacts fork if (!empty($pid_contacts)) { posix_kill($pid_contacts, SIGTERM); pcntl_waitpid($pid_contacts, $status, WUNTRACED); } // send shutdown to logger fork posix_kill($pid_logger, SIGTERM); pcntl_waitpid($pid_logger, $status, WUNTRACED); // remove message queue msg_remove_queue($msg_queue); unlink($config["SMStoXMPP"]["app_lock"]); exit; } else { print "Unknown signal of {$signo}!\n"; $log->info("Recieved unproccessible POSIX signal of \"{$signo}\", ignoring"); } }
/** * @inheritdoc */ public function send($message, $category = null) { $queueMessage = $this->createMessage($message); if ($this->beforeSend($queueMessage) !== true) { Yii::log(Yii::t('NfyModule.app', "Not sending message '{msg}' to queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_INFO, 'nfy'); return; } $success = msg_send($this->getQueue(), 1, $queueMessage, true, false, $errorcode); if (!$success) { Yii::log(Yii::t('NfyModule.app', "Failed to save message '{msg}' in queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_ERROR, 'nfy'); if ($errorcode === MSG_EAGAIN) { Yii::log(Yii::t('NfyModule.app', "Queue {queue_label} is full.", array('{queue_label}' => $this->label)), CLogger::LEVEL_ERROR, 'nfy'); } return false; } $this->afterSend($queueMessage); Yii::log(Yii::t('NfyModule.app', "Sent message '{msg}' to queue {queue_label}.", array('{msg}' => $queueMessage->body, '{queue_label}' => $this->label)), CLogger::LEVEL_INFO, 'nfy'); }
public function ping($key) { try { $q = msg_get_queue($this->id, ONPHP_IPC_PERMS); } catch (BaseException $e) { // race return false; } $type = $msg = null; // YANETUT if (msg_receive($q, $key, $type, 2, $msg, false, MSG_IPC_NOWAIT)) { try { msg_send($q, $key, 1, false, false); } catch (BaseException $e) { /* lost key due to race */ } return true; } return false; }
/** * 开发环境将当前进程使用的文件写入消息队列,用于FileMonitor监控文件更新 * @return void */ protected function writeFilesListToQueue() { if (!Master::getQueueId()) { return; } $error_code = 0; $flip_file_list = array_flip(get_included_files()); $file_list = array_diff_key($flip_file_list, $this->includeFiles); $this->includeFiles = $flip_file_list; if ($file_list) { foreach (array_chunk($file_list, 10, true) as $list) { msg_send(Master::getQueueId(), self::MSG_TYPE_FILE_MONITOR, array_keys($list), true, false, $error_code); } } }
/** * Send signal to opponent to read data. */ private function sendPollSignal() { msg_send($this->queue, $this->opponentPID, $this->currentPID, false); posix_kill($this->opponentPID, POLL_MSG); }
exit(1); } $queue = msg_get_queue($token); var_dump(msg_queue_exists($token)); $pid = pcntl_fork(); if ($pid == 0) { $q = msg_get_queue($token); msg_send($q, 2, "start"); msg_receive($q, 1, $type, 100, $msg); msg_send($q, 2, $msg); // echo exit(0); } msg_receive($queue, 2, $type, 100, $msg); var_dump($msg); msg_send($queue, 1, "ok"); msg_receive($queue, 2, $type, 100, $msg); var_dump($msg); $ret = @msg_send($queue, 0, 'msg', false, false, $s_error_code); var_dump($ret); var_dump(22 === $s_error_code); // 22 - invalid argument $ret = msg_receive($queue, 0, $type, 100, $msg, false, MSG_IPC_NOWAIT, $r_error_code); var_dump($ret); var_dump(MSG_ENOMSG === $r_error_code); $ret = msg_stat_queue($queue); var_dump($ret[$s_msg_qnum]); msg_set_queue($queue, array("msg_perm.mode" => 0666)); msg_remove_queue($queue); pcntl_waitpid($pid, $status); unlink($filename);
break; case "情感分析": $buf = '2'; break; case "声纹分析": $buf = '3'; break; case "关键词检索": $buf = '4'; break; } $filename_w = $temp . $buf . ".txt"; if (copy($filename_r, $filename_w) == FALSE) { echo "Creating tasklist failed."; } if (!msg_send($message_queue, 1, $msg . $buf, true, true, $msg_err)) { echo "Msg not sent because {$msg_err}\n"; } unlink($filename_r); //重新开一个消息队列,防止出现php接受错误的消息 $message_queue_key = ftok($filename_w, 'a'); $message_queue = msg_get_queue($message_queue_key, 0777); if (msg_receive($message_queue, 2, $msg_type, 1024, $msg_result, true, 0, $msg_error)) { } else { echo "Received {$msg_error} fetching message\n"; } msg_remove_queue($message_queue); session_start(); $_SESSION["filename"] = $msg . $buf; header("Location: result.php"); } else {
public function put($queueName, $workload) { msg_send($this->getQueue($queueName), 1, $workload); }