Browse Source

chore: 常驻

master
liyang 4 weeks ago
parent
commit
46921a85b1
  1. 19
      app/command/ApiLogCommand.php
  2. 34
      app/command/SignStockCommand.php
  3. 1
      config/console.php
  4. 5
      crontab.md

19
app/command/ApiLogCommand.php

@ -10,7 +10,6 @@ use think\console\Input;
use think\console\Output; use think\console\Output;
use think\facade\Cache; use think\facade\Cache;
use think\facade\Log; use think\facade\Log;
use think\facade\Db;
class ApiLogCommand extends Command class ApiLogCommand extends Command
{ {
@ -21,7 +20,6 @@ class ApiLogCommand extends Command
protected function execute(Input $input, Output $output) protected function execute(Input $input, Output $output)
{ {
while (true) {
try { try {
$redis = Cache::store('redis')->handler(); $redis = Cache::store('redis')->handler();
$result = $redis->blpop(['api_log'], 5); $result = $redis->blpop(['api_log'], 5);
@ -29,7 +27,7 @@ class ApiLogCommand extends Command
if ($result) { if ($result) {
$logData = json_decode($result[1], true); $logData = json_decode($result[1], true);
if (!is_array($logData) || empty($logData)) { if (!is_array($logData) || empty($logData)) {
continue; return;
} }
$logData['params'] = json_encode($logData['params'] ?? [], JSON_UNESCAPED_UNICODE); $logData['params'] = json_encode($logData['params'] ?? [], JSON_UNESCAPED_UNICODE);
@ -38,24 +36,15 @@ class ApiLogCommand extends Command
try { try {
ApiLogModel::create($logData); ApiLogModel::create($logData);
} catch (\Throwable $e) { } catch (\Throwable $e) {
// 如果是 MySQL 2006 错误,则重连后再执行 $redis->lpush('api_log', $result[1]);
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; throw $e;
} }
}
} else { } else {
sleep(10); // 没数据时休眠 return;
} }
} catch (\Throwable $e) { } catch (\Throwable $e) {
Log::error('ApiLogCommand error: ' . $e->getMessage()); Log::error('ApiLogCommand error: ' . $e->getMessage());
sleep(3);
}
} }
return;
} }
} }

34
app/command/SignStockCommand.php

@ -0,0 +1,34 @@
<?php
declare(strict_types=1);
namespace app\command;
use app\admin\service\setting\IPOService;
use think\console\Command;
use think\console\Input;
use think\console\Output;
class SignStockCommand extends Command
{
protected function configure()
{
// 指令配置
$this->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);
}
}
}

1
config/console.php

@ -7,5 +7,6 @@ return [
'commands' => [ 'commands' => [
\app\command\ApiLogCommand::class, \app\command\ApiLogCommand::class,
\app\command\TestCommand::class, \app\command\TestCommand::class,
\app\command\SignStockCommand::class,
], ],
]; ];

5
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
Loading…
Cancel
Save