diff --git a/app/command/ApiLogCommand.php b/app/command/ApiLogCommand.php index d4818d5a..1b8f9416 100644 --- a/app/command/ApiLogCommand.php +++ b/app/command/ApiLogCommand.php @@ -10,7 +10,6 @@ use think\console\Input; use think\console\Output; use think\facade\Cache; use think\facade\Log; -use think\facade\Db; class ApiLogCommand extends Command { @@ -21,41 +20,31 @@ class ApiLogCommand extends Command protected function execute(Input $input, Output $output) { - while (true) { - try { - $redis = Cache::store('redis')->handler(); - $result = $redis->blpop(['api_log'], 5); - - if ($result) { - $logData = json_decode($result[1], true); - if (!is_array($logData) || empty($logData)) { - continue; - } - - $logData['params'] = json_encode($logData['params'] ?? [], JSON_UNESCAPED_UNICODE); - $logData['response'] = json_encode($logData['response'] ?? [], JSON_UNESCAPED_UNICODE); - - try { - ApiLogModel::create($logData); - } catch (\Throwable $e) { - // 如果是 MySQL 2006 错误,则重连后再执行 - if (strpos($e->getMessage(), '2006 MySQL server has gone away') !== false) { - Log::warning('MySQL connection lost, reconnecting...'); - Db::purge(); - // Db::disconnect(); // 清理旧连接 - // Db::reconnect(); // 重新连接 - ApiLogModel::create($logData); // 重试一次 - } else { - throw $e; - } - } - } else { - sleep(10); // 没数据时休眠 + try { + $redis = Cache::store('redis')->handler(); + $result = $redis->blpop(['api_log'], 5); + + if ($result) { + $logData = json_decode($result[1], true); + if (!is_array($logData) || empty($logData)) { + return; } - } catch (\Throwable $e) { - Log::error('ApiLogCommand error: ' . $e->getMessage()); - sleep(3); + + $logData['params'] = json_encode($logData['params'] ?? [], JSON_UNESCAPED_UNICODE); + $logData['response'] = json_encode($logData['response'] ?? [], JSON_UNESCAPED_UNICODE); + + try { + ApiLogModel::create($logData); + } catch (\Throwable $e) { + $redis->lpush('api_log', $result[1]); + throw $e; + } + } else { + return; } + } catch (\Throwable $e) { + Log::error('ApiLogCommand error: ' . $e->getMessage()); } + return; } } diff --git a/app/command/SignStockCommand.php b/app/command/SignStockCommand.php new file mode 100644 index 00000000..5bfc6d6e --- /dev/null +++ b/app/command/SignStockCommand.php @@ -0,0 +1,34 @@ +setName('sign:stock') + ->setDescription('the sign stock command'); + } + /** + * Execute the console command. + * + * @param Input $input + * @param Output $output + * @return void + */ + protected function execute(Input $input, Output $output) + { + $market_list = [3, 4, 5, 6, 7, 9, 12, 14, 15, 16, 17, 18]; + foreach ($market_list as $market_type) { + (new IPOService())->signStockIPO($market_type); + } + } +} diff --git a/config/console.php b/config/console.php index 5ef2d1bf..46f421aa 100644 --- a/config/console.php +++ b/config/console.php @@ -7,5 +7,6 @@ return [ 'commands' => [ \app\command\ApiLogCommand::class, \app\command\TestCommand::class, + \app\command\SignStockCommand::class, ], ]; diff --git a/crontab.md b/crontab.md new file mode 100644 index 00000000..f2d91ac7 --- /dev/null +++ b/crontab.md @@ -0,0 +1,5 @@ +# 处理股票IPO中签 +*/10 * * * * cd /www/bourse_p2 && /usr/bin/php think sign:stock >> /www/bourse_p2/runtime/log/cron.log 2>&1 + +# 记录api 日志 +*/1 * * * * cd /www/bourse_p2 && /usr/bin/php think api_log >> /www/bourse_p2/runtime/log/api.log 2>&1 \ No newline at end of file