diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index ec3d881..35fc388 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -26,33 +26,33 @@ protected function schedule(Schedule $schedule) { $task = $category->country_locale_slug . "-" . $category->slug; - $nextRun = DailyTaskSchedule::where('task', $task)->first(); + $nextRun = DailyTaskSchedule::where('task', $task)->first(); if (!is_null($nextRun)) { - $currentTime = now()->format('H:i:00'); - - if ($currentTime == $nextRun->next_run_time) { + $currentTime = now(); - $shopee_seller_category = ShopeeSellerCategory::where('category_id', $category->id)->where(function($query) { - $query->whereNull('last_ai_written_at') - ->orWhere('last_ai_written_at', '=', ShopeeSellerCategory::whereNotNull('last_ai_written_at')->orderBy('last_ai_written_at', 'asc')->value('last_ai_written_at')); - })->first(); + if ($currentTime->equalTo($nextRun->next_run_time)) { - $task = ShopeeSellerTopProductScraperJob::dispatch($shopee_seller_category->seller, $category->country_locale->country_iso, $category) - ->onQueue('default') - ->onConnection('default'); + $shopee_seller_category = ShopeeSellerCategory::where('category_id', $category->id)->where(function($query) { + $query->whereNull('last_ai_written_at') + ->orWhere('last_ai_written_at', '=', ShopeeSellerCategory::whereNotNull('last_ai_written_at')->orderBy('last_ai_written_at', 'asc')->value('last_ai_written_at')); + })->first(); - // Update the next random run time for the following day using Eloquent - $nextRun->next_run_time = now()->addMinutes(rand(1200, 1440))->format('H:i:00'); - $nextRun->save(); - } + $task = ShopeeSellerTopProductScraperJob::dispatch($shopee_seller_category->seller, $category->country_locale->country_iso, $category) + ->onQueue('default') + ->onConnection('default'); + + // Update the next random run time for the following day using Eloquent + $nextRun->next_run_time = now()->addMinutes(rand(1200, 1440)); + $nextRun->save(); + } } else { $nextRun = new DailyTaskSchedule; $nextRun->task = 'my-technology'; - $nextRun->next_run_time = now()->addMinutes(rand(0, 1440))->format('H:i:00'); + $nextRun->next_run_time = now()->addMinutes(rand(0, 1440)); $nextRun->save(); } } diff --git a/database/migrations/2023_10_01_144735_create_daily_task_schedules_table.php b/database/migrations/2023_10_01_144735_create_daily_task_schedules_table.php index 9454a7e..e8a2e6e 100644 --- a/database/migrations/2023_10_01_144735_create_daily_task_schedules_table.php +++ b/database/migrations/2023_10_01_144735_create_daily_task_schedules_table.php @@ -14,7 +14,7 @@ public function up(): void Schema::create('daily_task_schedules', function (Blueprint $table) { $table->id(); $table->string('task'); - $table->time('next_run_time'); + $table->timestamp('next_run_time'); $table->timestamps(); }); }