| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- /**
- * Created by PhpStorm.
- * User: Administrator
- * Date: 2019/6/26
- * Time: 12:03
- */
- namespace datainf\logic;
- /*
- use App\Lib\ModelBase;
- use App\Http\Response\Response;
- use Illuminate\Database\Capsule\Manager as DB;
- */
- use datainf\lib\GlobConfigs;
- use swoole;
- class HttpDangerTimer
- {
- private $httpserver;
- private $config;
- private $redisonfig = [];
- public function __construct($config)
- {
- $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
- $this->httpserver->set($config['sets']);
- $this->config = $config;
- $this->redisonfig = GlobConfigs::getKey('redis');
- $this->httpserver->on('request', array($this, 'OnRequest'));
- $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart'));
- $this->httpserver->on('task', array($this, 'onTask'));
- $this->httpserver->on('finish', array($this, 'onFinish'));
- }
- public function onWorkerStart($serv, $worker_id)
- {
- if ($serv->worker_id == 0) {
- Swoole\Timer::tick(5000, function ($id) {
- $this->HandleOrder();
- });
- }
- if ($serv->worker_id == 1) {
- Swoole\Timer::tick(10000, function ($id) {
- $this->HandleMatch();
- });
- }
- if ($serv->worker_id == 2) {
- Swoole\Timer::tick(10000, function ($id) {
- $this->HandleOrderInvalid();
- });
- }
- }
- //每5秒请求一次危险球审核接口
- private function HandleOrder()
- {
- $config = $this->config;
- $token = $config['token'];
- $url = $config['HandleOrder'];
- go(function () use ($token, $url) {
- $furl = $url . '?token=' . $token;
- $ret = post_curls($furl, ['token' => $token]);
- $this->writeLog(['data' => ['HandleOrder']], $ret);
- echo "HandleMatch=>" . $furl . ' -- ret ' . $ret . "\n";
- });
- }
- //每5秒请求一次危险球审核接口
- private function HandleMatch()
- {
- $config = $this->config;
- $token = $config['token'];
- $url = $config['HandleMatch'];
- go(function () use ($token, $url) {
- $furl = $url . '?token=' . $token;
- $ret = post_curls($furl, ['token' => $token]);
- $this->writeLog(['data' => ['HandleMatch']], $ret);
- echo "HandleMatch=>" . $furl . '--ret ' . $ret . "\n";
- });
- }
- //每5秒请求一次 处理时间段内赛事已取消的注单1
- private function HandleOrderInvalid()
- {
- $config = $this->config;
- $token = $config['token'];
- $url = $config['HandleOrderInvalid'];
- go(function () use ($token, $url) {
- $furl = $url . '?token=' . $token;
- $ret = post_curls($furl, ['token' => $token]);
- $this->writeLog(['data' => ['HandleOrderInvalid']], $ret);
- echo "HandleMatch=>" . $furl . '--ret ' . $ret . "\n";
- });
- }
- public function OnRequest($request, $response)
- {
- return;
- }
- public function onTask($serv, $task)
- {
- }
- public function onFinish($serv, int $task_id, $data)
- {
- }
- public function start()
- {
- $this->httpserver->start();
- }
- private function writeLog($body, $ret)
- {
- go(function () use ($body, $ret) {
- $json_data = json_encode($body, JSON_UNESCAPED_UNICODE);
- $data = json_decode($body['data'], true);
- $game_code = isset($data['game_code']) ? $data['game_code'] : 'Timer';
- $title = isset($data['title']) ? $data['title'] : 'Timer';
- $msg = is_string($ret) ? $ret : json_encode($ret, 256);
- $now = explode(" ", microtime());
- $wdata = date("Y-m-d", $now[1]);
- $path = LOG_PATH . DS . $wdata . DS . $game_code . DS;
- if (!file_exists($path)) {
- $ret = mkdir($path, '0755', true);
- if (!$ret) {
- echo "$path --- Log File Create false \n";
- return;
- }
- }
- $lasttxt = date('Y-m-d H:i:s', $now[1]) . substr($now[0], 1, 5) . ' - ' . $msg . ' - ' . $json_data . "\n\n";
- $file = $path . DS . $game_code . '_' . $title . '.log';
- file_put_contents($file, $lasttxt, FILE_APPEND | LOCK_EX);
- return;
- });
- return;
- }
- }
|