HttpDangerTimer.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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(10000, function ($id) {
  41. $this->HandleMatch();
  42. });
  43. }
  44. if ($serv->worker_id == 2) {
  45. Swoole\Timer::tick(10000, 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 = post_curls($furl, ['token' => $token]);
  59. $this->writeLog(['data' => ['HandleOrder']], $ret);
  60. echo "HandleMatch=>" . $furl . ' -- ret ' . $ret . "\n";
  61. });
  62. }
  63. //每5秒请求一次危险球审核接口
  64. private function HandleMatch()
  65. {
  66. $config = $this->config;
  67. $token = $config['token'];
  68. $url = $config['HandleMatch'];
  69. go(function () use ($token, $url) {
  70. $furl = $url . '?token=' . $token;
  71. $ret = post_curls($furl, ['token' => $token]);
  72. $this->writeLog(['data' => ['HandleMatch']], $ret);
  73. echo "HandleMatch=>" . $furl . '--ret ' . $ret . "\n";
  74. });
  75. }
  76. //每5秒请求一次 处理时间段内赛事已取消的注单1
  77. private function HandleOrderInvalid()
  78. {
  79. $config = $this->config;
  80. $token = $config['token'];
  81. $url = $config['HandleOrderInvalid'];
  82. go(function () use ($token, $url) {
  83. $furl = $url . '?token=' . $token;
  84. $ret = post_curls($furl, ['token' => $token]);
  85. $this->writeLog(['data' => ['HandleOrderInvalid']], $ret);
  86. echo "HandleMatch=>" . $furl . '--ret ' . $ret . "\n";
  87. });
  88. }
  89. public function OnRequest($request, $response)
  90. {
  91. return;
  92. }
  93. public function onTask($serv, $task)
  94. {
  95. }
  96. public function onFinish($serv, int $task_id, $data)
  97. {
  98. }
  99. public function start()
  100. {
  101. $this->httpserver->start();
  102. }
  103. private function writeLog($body, $ret)
  104. {
  105. go(function () use ($body, $ret) {
  106. $json_data = json_encode($body, JSON_UNESCAPED_UNICODE);
  107. $data = json_decode($body['data'], true);
  108. $game_code = isset($data['game_code']) ? $data['game_code'] : 'Timer';
  109. $title = isset($data['title']) ? $data['title'] : 'Timer';
  110. $msg = is_string($ret) ? $ret : json_encode($ret, 256);
  111. $now = explode(" ", microtime());
  112. $wdata = date("Y-m-d", $now[1]);
  113. $path = LOG_PATH . DS . $wdata . DS . $game_code . DS;
  114. if (!file_exists($path)) {
  115. $ret = mkdir($path, '0755', true);
  116. if (!$ret) {
  117. echo "$path --- Log File Create false \n";
  118. return;
  119. }
  120. }
  121. $lasttxt = date('Y-m-d H:i:s', $now[1]) . substr($now[0], 1, 5) . ' - ' . $msg . ' - ' . $json_data . "\n\n";
  122. $file = $path . DS . $game_code . '_' . $title . '.log';
  123. file_put_contents($file, $lasttxt, FILE_APPEND | LOCK_EX);
  124. return;
  125. });
  126. return;
  127. }
  128. }