HttpDangerTimer.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Administrator
  5. * Date: 2019/6/26
  6. * Time: 12:03
  7. */
  8. namespace datainf\logic;
  9. /*
  10. use App\Lib\ModelBase;
  11. use App\Http\Response\Response;
  12. use Illuminate\Database\Capsule\Manager as DB;
  13. */
  14. use datainf\lib\GlobConfigs;
  15. use swoole;
  16. class HttpDangerTimer
  17. {
  18. private $httpserver;
  19. private $config;
  20. private $redisonfig = [];
  21. public function __construct($config)
  22. {
  23. $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
  24. $this->httpserver->set($config['sets']);
  25. $this->config = $config;
  26. $this->redisonfig = GlobConfigs::getKey('redis');
  27. $this->httpserver->on('request', array($this, 'OnRequest'));
  28. $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart'));
  29. $this->httpserver->on('task', array($this, 'onTask'));
  30. $this->httpserver->on('finish', array($this, 'onFinish'));
  31. }
  32. public function onWorkerStart($serv, $worker_id)
  33. {
  34. if ($serv->worker_id == 0) {
  35. Swoole\Timer::tick(5000, function ($id) {
  36. $this->HandleOrder();
  37. });
  38. }
  39. if ($serv->worker_id == 1) {
  40. Swoole\Timer::tick(60000, function ($id) {
  41. $this->HandleMatch();
  42. });
  43. }
  44. if ($serv->worker_id == 2) {
  45. Swoole\Timer::tick(60000, function ($id) {
  46. $this->HandleOrderInvalid();
  47. });
  48. }
  49. }
  50. //每5秒请求一次危险球审核接口
  51. private function HandleOrder()
  52. {
  53. $config = $this->config;
  54. $token = $config['token'];
  55. $url = $config['HandleOrder'];
  56. go(function () use ($token, $url) {
  57. $furl = $url . '?token=' . $token;
  58. $ret = file_get_contents($furl);
  59. $this->writeLog(['data' => json_encode(['HandleOrder']), 'ret' => $ret], '');
  60. });
  61. }
  62. //每5秒请求一次危险球审核接口
  63. private function HandleMatch()
  64. {
  65. $config = $this->config;
  66. $token = $config['token'];
  67. $url = $config['HandleMatch'];
  68. go(function () use ($token, $url) {
  69. $furl = $url . '?token=' . $token;
  70. $ret = file_get_contents($furl);
  71. $this->writeLog(['data' => json_encode(['HandleMatch']), 'ret' => $ret], '');
  72. });
  73. }
  74. //每5秒请求一次 处理时间段内赛事已取消的注单1
  75. private function HandleOrderInvalid()
  76. {
  77. $config = $this->config;
  78. $token = $config['token'];
  79. $url = $config['HandleOrderInvalid'];
  80. go(function () use ($token, $url) {
  81. $furl = $url . '?token=' . $token;
  82. $ret = file_get_contents($furl);
  83. $this->writeLog(['data' => json_encode(['HandleOrderInvalid']), 'ret' => $ret], '');
  84. });
  85. }
  86. public function OnRequest($request, $response)
  87. {
  88. return;
  89. }
  90. public function onTask($serv, $task)
  91. {
  92. }
  93. public function onFinish($serv, int $task_id, $data)
  94. {
  95. }
  96. public function start()
  97. {
  98. $this->httpserver->start();
  99. }
  100. private function writeLog($body, $ret)
  101. {
  102. go(function () use ($body, $ret) {
  103. $json_data = json_encode($body, JSON_UNESCAPED_UNICODE);
  104. $data = json_decode($body['data'], true);
  105. $game_code = isset($data['game_code']) ? $data['game_code'] : 'Timer';
  106. $title = isset($data['title']) ? $data['title'] : 'Timer';
  107. $msg = is_string($ret) ? $ret : json_encode($ret, 256);
  108. $now = explode(" ", microtime());
  109. $wdata = date("Y-m-d", $now[1]);
  110. $path = LOG_PATH . DS . $wdata . DS . $game_code . DS;
  111. if (!file_exists($path)) {
  112. $ret = mkdir($path, '0755', true);
  113. if (!$ret) {
  114. echo "$path --- Log File Create false \n";
  115. return;
  116. }
  117. }
  118. $lasttxt = date('Y-m-d H:i:s', $now[1]) . substr($now[0], 1, 5) . ' - ' . $msg . ' - ' . $json_data . "\n\n";
  119. $file = $path . DS . $game_code . '_' . $title . '.log';
  120. file_put_contents($file, $lasttxt, FILE_APPEND | LOCK_EX);
  121. return;
  122. });
  123. return;
  124. }
  125. }