瀏覽代碼

任务调度

seven 1 月之前
父節點
當前提交
421b57f505

+ 7 - 3
app/Http/Controllers/Admin/TestController.php

@@ -22,11 +22,15 @@ public function index()
         // $result = app('api-football')->leagues($params);
         // file_put_contents(storage_path('logs/leagues.log'),json_encode($result) . PHP_EOL, FILE_APPEND);
 
-        // $result = json_decode(file_get_contents(storage_path('logs/leagues.log')), true);
+        $result = json_decode(file_get_contents(storage_path('logs/leagues.log')), true);
         // echo '<pre>';
         // var_dump($result);
         // return $this->success($result);
-
-        SyncFootballDataService::syncLeagues($params);
+        $aa = [];
+        foreach ($result['response'] as $item) {
+            $aa[] = $item['league'];
+        }
+        var_dump($aa);
+        // SyncFootballDataService::syncLeagues($params);
     }
 }

+ 39 - 0
app/Jobs/DailyReportJob.php

@@ -0,0 +1,39 @@
+<?php
+
+
+namespace App\Jobs;
+
+use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Foundation\Queue\Queueable;
+
+use Illuminate\Support\Facades\Log;
+
+use App\Services\FixtureService;
+
+// 每日执行任务
+class DailyReportJob implements ShouldQueue
+{
+    use Queueable;
+
+    /**
+     * Create a new job instance.
+     */
+    public function __construct()
+    {
+        //
+    }
+
+    /**
+     * Execute the job.
+     */
+    public function handle(): void
+    {
+        //
+        Log::info('DailyReportJob executed at ' . now());
+
+        
+        FixtureService::index();
+
+        Log::info('Generating daily report...');
+    }
+}

+ 1 - 1
app/Services/ApiFootball/Sdk.php

@@ -30,7 +30,7 @@ public function countries($params = [])
 
     /**
      * @description: 获取联赛数据
-     * @param {*} $params
+     * @param {integer} $params.id
      * @return {array}
      */    
     public function leagues($params = [])

+ 32 - 0
database/migrations/2026_03_05_173523_create_jobs_table.php

@@ -0,0 +1,32 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     */
+    public function up(): void
+    {
+        Schema::create('jobs', function (Blueprint $table) {
+            $table->bigIncrements('id');
+            $table->string('queue')->index();
+            $table->longText('payload');
+            $table->unsignedTinyInteger('attempts');
+            $table->unsignedInteger('reserved_at')->nullable();
+            $table->unsignedInteger('available_at');
+            $table->unsignedInteger('created_at');
+        });
+    }
+
+    /**
+     * Reverse the migrations.
+     */
+    public function down(): void
+    {
+        Schema::dropIfExists('jobs');
+    }
+};

+ 33 - 0
routes/console.php

@@ -3,6 +3,39 @@
 use Illuminate\Foundation\Inspiring;
 use Illuminate\Support\Facades\Artisan;
 
+use Illuminate\Support\Facades\Schedule;
+
+use App\Jobs\DailyReportJob;
+
 Artisan::command('inspire', function () {
     $this->comment(Inspiring::quote());
 })->purpose('Display an inspiring quote');
+
+
+// // 这里定义你的定时任务(和以前 Kernel.php 里的 schedule 方法内容一样)
+// Schedule::command('inspire')->hourly();
+
+// // 每天运行一次的例子(凌晨1点)
+// Schedule::command('your:command-name')
+//     ->dailyAt('01:00')
+//     ->timezone('Asia/Shanghai')
+//     ->onOneServer()
+//     ->runInBackground();
+
+// // 或者调用闭包
+// Schedule::call(function () {
+//     // 你的业务代码...
+//     \Log::info('每日任务执行了 ' . now());
+// })->daily()->name('每日清理任务')->withoutOverlapping();
+
+// // 或者调度一个 Job 类
+// Schedule::job(new \App\Jobs\ProcessReports)->dailyAt('03:00');
+
+// 每天凌晨 2:00 自动 dispatch 这个 Job
+Schedule::job(new DailyReportJob)
+        ->dailyAt('02:00')
+        ->timezone('Asia/Shanghai')           // 根据你的服务器时区调整
+        ->onOneServer()                       // 多服务器部署时防止重复执行(推荐)
+        ->withoutOverlapping()                // 防止重叠执行(如果上一次还没完,这次不跑)
+        ->name('daily-report-job')            // 给任务起个名字,便于日志追踪
+        ->description('每天生成日报表');