Parcourir la source

feat(console): 添加API日志清理定时任务

- 在控制台配置中注册deleteApiLog命令
- 创建DeleteApiLog类实现日志清理功能
- 设置定时删除超过10天的API请求日志
- 使用数据库查询限制每次删除1000条记录
- 添加命令描述为"定时删除api日志"
- 实现基于时间戳的日志清理逻辑
shichen il y a 4 mois
Parent
commit
92696db730
2 fichiers modifiés avec 41 ajouts et 0 suppressions
  1. 40 0
      app/command/DeleteApiLog.php
  2. 1 0
      config/console.php

+ 40 - 0
app/command/DeleteApiLog.php

@@ -0,0 +1,40 @@
1
+<?php
2
+
3
+namespace app\command;
4
+
5
+use app\common\model\shared\SharedProsperityPartnerConsumeUpdateLog;
6
+use app\common\repositories\shared\SharedProsperityRecommendConfigRepository;
7
+use think\console\Command;
8
+use think\console\Input;
9
+use think\console\Output;
10
+use think\facade\Db;
11
+
12
+class DeleteApiLog extends Command
13
+{
14
+    // 上级
15
+    const DAY = 10;
16
+
17
+    protected function configure()
18
+    {
19
+        // 指令配置
20
+        $this->setName('deleteApiLog')
21
+            ->setDescription('定时删除api日志');
22
+    }
23
+
24
+    protected function execute(Input $input, Output $output)
25
+    {
26
+        $output->writeln('定时任务执行中...');
27
+
28
+        $this->delete();
29
+
30
+        $output->writeln('定时任务执行完成');
31
+    }
32
+
33
+    private function delete()
34
+    {
35
+        $time = time() - self::DAY * 24 * 60 * 60;
36
+        $time = date('Y-m-d H:i:s', $time);
37
+        Db::name('api_request_log')->where('create_time', '<', $time)->limit(1000)->delete();
38
+    }
39
+
40
+}

+ 1 - 0
config/console.php

@@ -36,5 +36,6 @@ return [
36 36
         'wiwibaoproductsync' => 'app\command\WiwibaoProductSync',
37 37
         'sharedPartnerConsumeUpdate' => 'app\command\SharedPartnerConsumeUpdate',
38 38
         'productPriceUpdate' => 'app\command\ProductPriceUpdate',
39
+        'deleteApiLog' => 'app\command\DeleteApiLog',
39 40
     ],
40 41
 ];