Update (kernel): add more schedulers

This commit is contained in:
2023-10-02 23:34:19 +08:00
parent fff91760c4
commit cd5837c3bf

View File

@@ -16,23 +16,31 @@ class Kernel extends ConsoleKernel
*/ */
protected function schedule(Schedule $schedule) 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 () { /**
* Schedule a category.
$category = Category::where('country_locale_slug','my')->where('name','Technology')->first(); */
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)) if (!is_null($category))
{ {
$task = $category->country_locale_slug . "-" . $category->slug; $task = $category->country_locale_slug . "-" . $category->slug;
$nextRun = DailyTaskSchedule::where('task', $task)->first(); $nextRun = DailyTaskSchedule::where('task', $task)->first();
if (!is_null($nextRun)) if (!is_null($nextRun))
{ {
$currentTime = now(); $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) $shopee_seller_category = ShopeeSellerCategory::where('category_id', $category->id)
->orderByRaw('ISNULL(last_ai_written_at) DESC') ->orderByRaw('ISNULL(last_ai_written_at) DESC')
@@ -40,32 +48,24 @@ protected function schedule(Schedule $schedule)
->orderBy('id', 'asc') ->orderBy('id', 'asc')
->first(); ->first();
// $shopee_seller_category = ShopeeSellerCategory::where('category_id', $category->id)->where(function($query) { $task = ShopeeSellerTopProductScraperJob::dispatch($shopee_seller_category->seller, $category->country_locale->country_iso, $category)
// $query->whereNull('last_ai_written_at') ->onQueue('default')
// ->orWhere('last_ai_written_at', '=', ShopeeSellerCategory::whereNotNull('last_ai_written_at')->orderBy('last_ai_written_at', 'asc')->value('last_ai_written_at')); ->onConnection('default');
// })->first();
$task = ShopeeSellerTopProductScraperJob::dispatch($shopee_seller_category->seller, $category->country_locale->country_iso, $category) $nextRun->next_run_time = now()->addMinutes(rand(1200, 1440));
->onQueue('default') $nextRun->save();
->onConnection('default'); }
}
// Update the next random run time for the following day using Eloquent else
$nextRun->next_run_time = now()->addMinutes(rand(1200, 1440)); {
$nextRun->save(); $nextRun = new DailyTaskSchedule;
} $nextRun->task = $category->country_locale_slug . "-" . $category->slug;
} $nextRun->next_run_time = now()->addMinutes(rand(0, 1440));
else $nextRun->save();
{ }
$nextRun = new DailyTaskSchedule; }
$nextRun->task = 'my-technology';
$nextRun->next_run_time = now()->addMinutes(rand(0, 1440));
$nextRun->save();
}
}
})->everyMinute();
})->everyMinute()->name($taskName);
} }
/** /**
@@ -77,8 +77,4 @@ protected function commands(): void
require base_path('routes/console.php'); require base_path('routes/console.php');
} }
} }