123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- class QueueMonitor
- {
- protected $monSocket = null;
-
- public function stats($key, $owners=array())
- {
- $this->ping(array('counter' => $key,
- 'owners' => $owners));
- }
-
- public function logState($threadId, $state, $substate='')
- {
- $this->ping(array('thread_id' => $threadId,
- 'state' => $state,
- 'substate' => $substate,
- 'ts' => microtime(true)));
- }
-
- protected function ping($data)
- {
- $target = common_config('queue', 'monitor');
- if (empty($target)) {
- return;
- }
- $data = $this->prepMonitorData($data);
- if (substr($target, 0, 4) == 'udp:') {
- $this->pingUdp($target, $data);
- } else if (substr($target, 0, 5) == 'http:') {
- $this->pingHttp($target, $data);
- } else {
- common_log(LOG_ERR, __METHOD__ . ' unknown monitor target type ' . $target);
- }
- }
- protected function pingUdp($target, $data)
- {
- if (!$this->monSocket) {
- $this->monSocket = stream_socket_client($target, $errno, $errstr);
- }
- if ($this->monSocket) {
- $post = http_build_query($data, '', '&');
- stream_socket_sendto($this->monSocket, $post);
- } else {
- common_log(LOG_ERR, __METHOD__ . " UDP logging fail: $errstr");
- }
- }
- protected function pingHttp($target, $data)
- {
- $client = new HTTPClient();
- try {
- $result = $client->post($target, array(), $data);
-
- if (!$result->isOk()) {
- common_log(LOG_ERR, __METHOD__ . ' HTTP ' . $result->getStatus() . ': ' . $result->getBody());
- }
- } catch (NoHttpResponseException $e) {
- common_log(LOG_ERR, __METHOD__ . ':'.$e->getMessage());
- } catch (HTTP_Request2_Exception $e) {
- common_log(LOG_ERR, __CLASS__ . ": Invalid $code redirect from $url to $target");
- }
- }
- protected function prepMonitorData($data)
- {
-
-
-
-
- return $data;
- }
- }
|