seven 23 ساعت پیش
والد
کامیت
2735590131
3فایلهای تغییر یافته به همراه84 افزوده شده و 0 حذف شده
  1. 7 0
      README.md
  2. 41 0
      app/Console/Commands/StartFiveSecondTask.php
  3. 36 0
      database/migrations/2025_10_10_133417_create_jobs_table.php

+ 7 - 0
README.md

@@ -151,6 +151,13 @@ MAIL_EXP=600  # 邮件验证码有效期(秒)
     - 余额归集:https://域名/admin/sync/collect
     - 结算通知:https://域名/admin/sync/settle
 
+## 七、定时任务每5秒执行
+- 先创建队列表
+    - php artisan queue:table
+    - php artisan migrate
+- 修改.env配置 使用数据库做队列
+    QUEUE_CONNECTION=database
+
     
 
 

+ 41 - 0
app/Console/Commands/StartFiveSecondTask.php

@@ -0,0 +1,41 @@
+<?php
+
+
+namespace App\Console\Commands;
+
+use Illuminate\Console\Command;
+use App\Jobs\FiveSecondTaskJob;
+
+class StartFiveSecondTask extends Command
+{
+    /**
+     * The name and signature of the console command.
+     *
+     * @var string
+     */
+    protected $signature = 'command:name';
+
+    /**
+     * The console command description.
+     *
+     * @var string
+     */
+    protected $description = 'Command description';
+
+    /**
+     * Execute the console command.
+     *
+     * @return int
+     */
+    public function handle()
+    {
+        $this->info('启动5秒循环任务...');
+        
+        // 首次启动任务
+        FiveSecondTaskJob::dispatch()->delay(now()->addSeconds(5));
+        
+        $this->info('任务已启动,将在5秒后首次执行');
+        
+        return Command::SUCCESS;
+    }
+}

+ 36 - 0
database/migrations/2025_10_10_133417_create_jobs_table.php

@@ -0,0 +1,36 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+use Illuminate\Support\Facades\Schema;
+
+return new class extends Migration
+{
+    /**
+     * Run the migrations.
+     *
+     * @return void
+     */
+    public function up()
+    {
+        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.
+     *
+     * @return void
+     */
+    public function down()
+    {
+        Schema::dropIfExists('jobs');
+    }
+};