From 2b55e0376ec592335bb246f831c32082a9f55ae5 Mon Sep 17 00:00:00 2001 From: Charles T Date: Mon, 2 Oct 2023 00:22:13 +0800 Subject: [PATCH] Add (ai gen) --- app/Console/Kernel.php | 66 +++++++++++++------ app/Models/DailyTaskSchedule.php | 35 ++++++++++ ...4735_create_daily_task_schedules_table.php | 29 ++++++++ 3 files changed, 109 insertions(+), 21 deletions(-) create mode 100644 app/Models/DailyTaskSchedule.php create mode 100644 database/migrations/2023_10_01_144735_create_daily_task_schedules_table.php diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 9d3b495..8886296 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -4,6 +4,8 @@ use App\Jobs\ShopeeSellerTopProductScraperJob; use App\Models\Category; +use App\Models\DailyTaskSchedule; +use App\Models\ShopeeSellerCategory; use Illuminate\Console\Scheduling\Schedule; use Illuminate\Foundation\Console\Kernel as ConsoleKernel; @@ -14,29 +16,49 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // Schdule MY TECH Category - $category = Category::where('country_locale_slug','my')->where('name','Technology')->first(); + // Schedule MY TECH Category - if (!is_null($category)) - { - $hour = rand(0, 23); - $minute = rand(0, 59); - - $schedule->call(function () use ($category) { - - $shopee_seller_category = \App\Models\ShopeeSellerCategory::where('category_id', $category->id)->where(function($query) { - $query->whereNull('last_ai_written_at') - ->orWhere('last_ai_written_at', '=', \App\Models\ShopeeSellerCategory::whereNotNull('last_ai_written_at')->orderBy('last_ai_written_at', 'asc')->value('last_ai_written_at')); - })->first(); - - $task = ShopeeSellerTopProductScraperJob::dispatch($shopee_seller_category->seller, $category->country_locale->country_iso, $category) - ->onQueue('default') - ->onConnection('default'); - - - })->dailyAt("{$hour}:{$minute}"); - } + $schedule->call(function () { + $category = Category::where('country_locale_slug','my')->where('name','Technology')->first(); + + if (!is_null($category)) + { + $task = $category->country_locale_slug . "-" . $category->slug; + + $nextRun = DailyTaskSchedule::where('task', $task)->first(); + + if (!is_null($nextRun)) + { + $currentTime = now()->toTimeString(); + + if ($currentTime == $nextRun->next_run_time) { + + $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(); + + $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))->toTimeString(); + $nextRun->save(); + } + } + } + else + { + $nextRun = new DailyTaskSchedule; + $nextRun->task = 'my-technology'; + $nextRun->next_run_time = now()->addMinutes(rand(0, 1440))->toTimeString(); + $nextRun->save(); + } + + })->everyMinute(); + } /** @@ -51,3 +73,5 @@ protected function commands(): void } + + diff --git a/app/Models/DailyTaskSchedule.php b/app/Models/DailyTaskSchedule.php new file mode 100644 index 0000000..f7eb898 --- /dev/null +++ b/app/Models/DailyTaskSchedule.php @@ -0,0 +1,35 @@ + 'datetime' + ]; + + protected $fillable = [ + 'task', + 'next_run_time' + ]; +} 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 new file mode 100644 index 0000000..9454a7e --- /dev/null +++ b/database/migrations/2023_10_01_144735_create_daily_task_schedules_table.php @@ -0,0 +1,29 @@ +id(); + $table->string('task'); + $table->time('next_run_time'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('daily_task_schedules'); + } +};