12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273 |
- <?php
- declare(strict_types=0);
- //此行代码会导致bug
- //ini_set('default_socket_timeout', -1);
- class queuehandler
- {
- private $_stop = false;
- public function run()
- {
- global $other_config;
- $queue_name = $other_config['net_queue']['name'];
- $host = $other_config['net_queue']['host'];
- $port = $other_config['net_queue']['port'];
- if (ob_get_level()) ob_end_clean();
- // pcntl_signal(SIGINT, [$this,'sig_handler']);
- // pcntl_signal(SIGHUP, [$this,'sig_handler']);
- // pcntl_signal(SIGQUIT, [$this,'sig_handler']);
- // pcntl_signal(SIGTERM, [$this,'sig_handler']);
- $logic_queue = new queue_logic();
- $worker = new QueueServer($queue_name,$host,$port);
- $queues = $worker->scan();
- while (true)
- {
- pcntl_signal_dispatch();
- try
- {
- if ($this->_stop) break;
- $content = $worker->pop($queues, 1);
- if(is_array($content))
- {
- $method = key($content);
- $arg = current($content);
- $argstr = json_encode($arg,JSON_UNESCAPED_UNICODE);
- Log::record("method={$method} args={$argstr}",Log::DEBUG);
- $result = $logic_queue->$method($arg);
- if ($result['code'] != 0) {
- Log::record("{$method} run error: {$result['msg']}",Log::ERR);
- }
- }
- }
- catch (Exception $e)
- {
- $err = $e->getMessage();
- $code = $e->getCode();
- Log::record("QueueDB pop err: code={$code} err={$err}",Log::DEBUG);
- break;
- }
- }
- }
- private function sig_handler($signo)
- {
- Log::record("queue quit at sig_handler.",Log::DEBUG);
- switch($signo) {
- case SIGINT:
- case SIGHUP:
- case SIGQUIT:
- case SIGTERM:
- $this->_stop = true;
- break;
- default:
- break;
- }
- }
- }
|