HttpServerRedisToSql.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. $obj = $this;
  38. Swoole\Timer::tick(500, function ($id) use ($obj) {
  39. $obj->BatchSqlRedisToDB();
  40. });
  41. }
  42. if ($serv->worker_id == 0) {
  43. Swoole\Timer::tick(60000, function () {
  44. $this->logRunStatus();
  45. });
  46. }
  47. }
  48. private function logRunStatus()
  49. {
  50. echo date('Y-m-d H:i:s') . " 总请求数:" . $this->httpserver->account->get() . ' 运行任务数:' . $this->httpserver->taskWorkingNum->get();
  51. echo $this->httpserver->worker_id . " 内存使用量:" . (memory_get_usage() / 1000) . 'k 峰值:' . (memory_get_peak_usage() / 1000) . "k\n";
  52. }
  53. //sql批量写入数据库
  54. private function BatchSqlRedisToDB()
  55. {
  56. $redisconfig = $this->redisonfig;
  57. go(function () use ($redisconfig) {
  58. $redis = new Swoole\Coroutine\Redis();
  59. $redis->connect($redisconfig['host'], $redisconfig['port']);
  60. if ($redis->llen(self::SQLKEY) <= 0) {
  61. return;
  62. }
  63. $this->httpserver->account->add();
  64. $batchsql = [];
  65. $max = 5000;
  66. for ($i = 0; $i < $max; $i++) {
  67. $now = $redis->rpop(self::SQLKEY);
  68. if (empty($now)) {
  69. break;
  70. } else {
  71. $batchsql[] = str_replace(";", ":", trim(trim($now), ";"));
  72. }
  73. }
  74. if (!empty($batchsql)) {
  75. $pdo = DB::getPdo();
  76. $sqlstr = implode(";", $batchsql);
  77. $pdo->exec($sqlstr);
  78. }
  79. return;
  80. });
  81. }
  82. public function OnRequest($request, $response)
  83. {
  84. $data = Response::generate('', 1, '', '任务正常运行.');
  85. $response->end($data);
  86. return;
  87. }
  88. public function onTask($serv, $task)
  89. {
  90. }
  91. public function onFinish($serv, int $task_id, $data)
  92. {
  93. }
  94. public function start()
  95. {
  96. $this->httpserver->start();
  97. }
  98. }