From cd5837c3bf9f8b073ad2d74391525777e563e0ca Mon Sep 17 00:00:00 2001 From: Charles T Date: Mon, 2 Oct 2023 23:34:19 +0800 Subject: [PATCH] Update (kernel): add more schedulers --- app/Console/Kernel.php | 76 ++++++++++++++++++++---------------------- 1 file changed, 36 insertions(+), 40 deletions(-) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index c5467e5..457fd52 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -16,23 +16,31 @@ class Kernel extends ConsoleKernel */ protected function schedule(Schedule $schedule) { - // Schedule MY TECH Category + $this->scheduleCategory($schedule, 'my', 'Technology', 'gen:my-technology'); + $this->scheduleCategory($schedule, 'my', 'Fitness', 'gen:my-fitness'); + $this->scheduleCategory($schedule, 'my', 'Home & Living', 'gen:my-home-living'); + } - $schedule->call(function () { - - $category = Category::where('country_locale_slug','my')->where('name','Technology')->first(); + /** + * Schedule a category. + */ + private function scheduleCategory(Schedule $schedule, $locale, $categoryName, $taskName) + { + $schedule->call(function () use ($locale, $categoryName) { + + $category = Category::where('country_locale_slug', $locale)->where('name', $categoryName)->first(); - if (!is_null($category)) - { - $task = $category->country_locale_slug . "-" . $category->slug; + if (!is_null($category)) + { + $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(); + if (!is_null($nextRun)) + { + $currentTime = now(); - if ($currentTime->gte($nextRun->next_run_time)) { + if ($currentTime->gte($nextRun->next_run_time)) { $shopee_seller_category = ShopeeSellerCategory::where('category_id', $category->id) ->orderByRaw('ISNULL(last_ai_written_at) DESC') @@ -40,32 +48,24 @@ protected function schedule(Schedule $schedule) ->orderBy('id', 'asc') ->first(); - // $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'); - $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)); - $nextRun->save(); - } - } - - - })->everyMinute(); + $nextRun->next_run_time = now()->addMinutes(rand(1200, 1440)); + $nextRun->save(); + } + } + else + { + $nextRun = new DailyTaskSchedule; + $nextRun->task = $category->country_locale_slug . "-" . $category->slug; + $nextRun->next_run_time = now()->addMinutes(rand(0, 1440)); + $nextRun->save(); + } + } + })->everyMinute()->name($taskName); } /** @@ -77,8 +77,4 @@ protected function commands(): void require base_path('routes/console.php'); } - - } - -