HttpServerRedisToSql.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. use App\Lib\ModelBase;
  10. use App\Http\Response\Response;
  11. use Illuminate\Database\Capsule\Manager as DB;
  12. use datainf\lib\GlobConfigs;
  13. use swoole;
  14. class HttpServerRedisToSql
  15. {
  16. private $httpserver;
  17. private $config;
  18. private $redisonfig = [];
  19. const SQLKEY = 'ALLSQLKEY';
  20. public function __construct($config)
  21. {
  22. $this->httpserver = new \swoole\http\server($config['host'], $config['port']);
  23. $this->httpserver->set($config['sets']);
  24. $this->config = $config;
  25. $this->redisonfig = GlobConfigs::getKey('redis');
  26. $this->httpserver->account = new \swoole\Atomic();
  27. $this->httpserver->taskWorkingNum = new \swoole\Atomic();
  28. $this->httpserver->on('request', array($this, 'OnRequest'));
  29. $this->httpserver->on('WorkerStart', array($this, 'onWorkerStart'));
  30. $this->httpserver->on('task', array($this, 'onTask'));
  31. $this->httpserver->on('finish', array($this, 'onFinish'));
  32. }
  33. public function onWorkerStart($serv, $worker_id)
  34. {
  35. if (!$serv->taskworker) {
  36. ModelBase::init();
  37. Swoole\Timer::tick(500, function ($id) {
  38. $this->BatchSqlRedisToDB();
  39. });
  40. }
  41. if ($serv->worker_id == 0) {
  42. Swoole\Timer::tick(60000, function () {
  43. $this->logRunStatus();
  44. });
  45. }
  46. }
  47. private function logRunStatus()
  48. {
  49. echo date('Y-m-d H:i:s') . " 总请求数:" . $this->httpserver->account->get() . ' 运行任务数:' . $this->httpserver->taskWorkingNum->get();
  50. echo ' work_id:' . $this->httpserver->worker_id . " 内存使用量:" . (memory_get_usage() / 1000) . 'k 峰值:' . (memory_get_peak_usage() / 1000) . "k\n";
  51. }
  52. //sql批量写入数据库
  53. private function BatchSqlRedisToDB()
  54. {
  55. $redisconfig = $this->redisonfig;
  56. //go(function () use ($redisconfig) {
  57. $begint = microtime(true);
  58. $redis = new Swoole\Coroutine\Redis();
  59. $ret = $redis->connect($redisconfig['host'], $redisconfig['port']);
  60. if (!$ret) {
  61. echo "redis 连接失败!";
  62. return;
  63. }
  64. if (!empty($redisconfig['passwd'])) {
  65. $ret = $redis->auth($redisconfig['passwd']);
  66. if (!$ret) {
  67. echo "redis auth 失败!";
  68. return;
  69. }
  70. }
  71. $redis->select($redisconfig['db']);
  72. if ($redis->llen(self::SQLKEY) <= 0) {
  73. return;
  74. }
  75. $this->httpserver->account->add();
  76. $batchsql = [];
  77. $max = 1000;
  78. for ($i = 0; $i < $max; $i++) {
  79. $now = $redis->rpop(self::SQLKEY);
  80. if (empty($now)) {
  81. break;
  82. } else {
  83. $batchsql[] = str_replace(";", ":", trim(trim($now), ";"));
  84. }
  85. }
  86. if (!empty($batchsql)) {
  87. $pdo = DB::getPdo();
  88. $sqlstr = implode(";", $batchsql);
  89. $erowcount = 0;
  90. try {
  91. $erowcount = $pdo->exec($sqlstr);
  92. } catch (\Exception $e) {
  93. echo "发生异常:" . $e->getCode() . ' ---- ' . print_r($e->getMessage(), true) . "\n";
  94. }
  95. if (!$erowcount) {
  96. echo "\n发生错误:" . $pdo->errorCode() . ' ---- ' . print_r($pdo->errorInfo(), true) . "\n";
  97. echo "错误sql: " . $sqlstr . "\n\n";
  98. } else {
  99. echo "成功运行:" . count($batchsql) . " 条!\n";
  100. }
  101. }
  102. $redis->close();
  103. echo "总请求数" . $this->httpserver->account->get() . " 单个线程消耗时间: " . (microtime(true) - $begint) . " s \n";
  104. return;
  105. // });
  106. }
  107. public function OnRequest($request, $response)
  108. {
  109. $data = Response::generate('', 1, '', '任务正常运行.');
  110. $response->end($data);
  111. return;
  112. }
  113. public function onTask($serv, $task)
  114. {
  115. }
  116. public function onFinish($serv, int $task_id, $data)
  117. {
  118. }
  119. public function start()
  120. {
  121. $this->httpserver->start();
  122. }
  123. }