rkljw 1 napja
szülő
commit
9e8136aa18

+ 34 - 0
app/Task/HistoryTodayTask.php

@@ -0,0 +1,34 @@
+<?php
+
+declare (strict_types = 1);
+
+namespace App\Task;
+
+use Hyperf\Contract\StdoutLoggerInterface;
+use Hyperf\DbConnection\Db;
+use Hyperf\Di\Annotation\Inject;
+use Psr\Log\LoggerInterface;
+use App\Controller\PublicController;
+
+class HistoryTodayTask
+{
+    /**
+     * @Inject
+     * @var StdoutLoggerInterface
+     */
+    private $logger;
+    private $publicController;
+    public function __construct(LoggerInterface $logger,  PublicController $publicController)
+    {
+        date_default_timezone_set('Asia/Shanghai');
+        $this->logger = $logger;
+        $this->publicController = $publicController;
+    }
+    public function __invoke()
+    {
+        $this->logger->info('HistoryTodayTask');
+        $this->publicController->addHistoryToday();
+
+    }
+
+}

+ 1 - 1
app/Tools/PublicData.php

@@ -257,7 +257,7 @@ class PublicData
         }else{
             $api_url = "https://apis.tianapi.com/msdl/index?key=44c4f9b3d66ce06900a098c78b90478f&num=10";
         }
-//        var_dump("请求地址:",$api_url);
+        var_dump("请求地址:",$api_url);
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $api_url);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

+ 1 - 0
composer.json

@@ -25,6 +25,7 @@
         "hyperf/config": "~3.1.0",
         "hyperf/config-nacos": "^3.1",
         "hyperf/constants": "^3.1",
+        "hyperf/crontab": "^3.1",
         "hyperf/database": "~3.1.0",
         "hyperf/db-connection": "~3.1.0",
         "hyperf/di": "^3.1",

+ 78 - 1
composer.lock

@@ -4,7 +4,7 @@
         "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
         "This file is @generated automatically"
     ],
-    "content-hash": "1720e871d24f37be6d7a2842f61de2fc",
+    "content-hash": "38b67762f870df573a4d5ca8969673c8",
     "packages": [
         {
             "name": "aws/aws-crt-php",
@@ -2465,6 +2465,83 @@
             ],
             "time": "2024-09-25T02:54:12+00:00"
         },
+        {
+            "name": "hyperf/crontab",
+            "version": "v3.1.42",
+            "source": {
+                "type": "git",
+                "url": "https://github.com/hyperf/crontab.git",
+                "reference": "be1187515aabbfe96b2f6a5330b4ddd742e971c7"
+            },
+            "dist": {
+                "type": "zip",
+                "url": "https://api.github.com/repos/hyperf/crontab/zipball/be1187515aabbfe96b2f6a5330b4ddd742e971c7",
+                "reference": "be1187515aabbfe96b2f6a5330b4ddd742e971c7",
+                "shasum": "",
+                "mirrors": [
+                    {
+                        "url": "https://mirrors.aliyun.com/composer/dists/%package%/%reference%.%type%",
+                        "preferred": true
+                    }
+                ]
+            },
+            "require": {
+                "hyperf/conditionable": "~3.1.0",
+                "hyperf/framework": "~3.1.0",
+                "hyperf/support": "~3.1.0",
+                "hyperf/tappable": "~3.1.0",
+                "hyperf/utils": "~3.1.0",
+                "nesbot/carbon": "^2.0",
+                "php": ">=8.1"
+            },
+            "suggest": {
+                "hyperf/command": "Required to use command trigger.",
+                "hyperf/process": "Auto register the Crontab process for server."
+            },
+            "type": "library",
+            "extra": {
+                "hyperf": {
+                    "config": "Hyperf\\Crontab\\ConfigProvider"
+                },
+                "branch-alias": {
+                    "dev-master": "3.1-dev"
+                }
+            },
+            "autoload": {
+                "psr-4": {
+                    "Hyperf\\Crontab\\": "src/"
+                }
+            },
+            "notification-url": "https://packagist.org/downloads/",
+            "license": [
+                "MIT"
+            ],
+            "description": "A crontab component for Hyperf.",
+            "homepage": "https://hyperf.io",
+            "keywords": [
+                "crontab",
+                "hyperf",
+                "php",
+                "swoole"
+            ],
+            "support": {
+                "docs": "https://hyperf.wiki",
+                "issues": "https://github.com/hyperf/hyperf/issues",
+                "pull-request": "https://github.com/hyperf/hyperf/pulls",
+                "source": "https://github.com/hyperf/hyperf"
+            },
+            "funding": [
+                {
+                    "url": "https://hyperf.wiki/#/zh-cn/donate",
+                    "type": "custom"
+                },
+                {
+                    "url": "https://opencollective.com/hyperf",
+                    "type": "open_collective"
+                }
+            ],
+            "time": "2024-09-25T02:54:12+00:00"
+        },
         {
             "name": "hyperf/database",
             "version": "v3.1.47",

+ 18 - 0
config/autoload/crontab.php

@@ -0,0 +1,18 @@
+<?php
+
+declare(strict_types=1);
+
+
+use Hyperf\Crontab\Crontab;
+return [
+    // 是否开启定时任务
+    'enable' => true,
+    'crontab' => [
+        (new Crontab())
+            ->setName('HistoryTodayTask')
+            ->setRule('0 0 0  * * *') // 每天凌晨执行一次
+            ->setCallback([App\Task\HistoryTodayTask::class, '__invoke'])
+            ->setMemo('获取历史上的今天')
+            ->setTimezone('Asia/Shanghai'),
+    ],
+];

+ 22 - 0
config/autoload/logger.php

@@ -32,4 +32,26 @@ return [
             ],
         ],
     ],
+    'crontab' => [
+        'handler' => [
+            'class' => Monolog\Handler\RotatingFileHandler::class,
+            'constructor' => [
+                'filename' => BASE_PATH . '/runtime/logs/crontab.log',
+                'maxFiles' => 30,
+                'level' => Monolog\Logger::DEBUG,
+                'bubble' => true,
+                'filePermission' => null,
+                'useLocking' => false,
+                'dateFormat' => 'Y-m-d',
+            ],
+        ],
+        'formatter' => [
+            'class' => Monolog\Formatter\LineFormatter::class,
+            'constructor' => [
+                'format' => "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n",
+                'dateFormat' => 'Y-m-d H:i:s',
+                'allowInlineLineBreaks' => true,
+            ],
+        ],
+    ],
 ];

+ 2 - 0
config/autoload/processes.php

@@ -1,6 +1,7 @@
 <?php
 
 declare(strict_types=1);
+use Hyperf\Crontab\Process\CrontabDispatcherProcess;
 /**
  * This file is part of Hyperf.
  *
@@ -11,4 +12,5 @@ declare(strict_types=1);
  */
 return [
     Hyperf\AsyncQueue\Process\ConsumerProcess::class, //异步消费进程
+    CrontabDispatcherProcess::class, // 定时任务进程
 ];