Files
productalert/app/Console/Kernel.php
2023-10-02 09:52:13 +08:00

79 lines
2.4 KiB
PHP

<?php
namespace App\Console;
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;
class Kernel extends ConsoleKernel
{
/**
* Define the application's command schedule.
*/
protected function schedule(Schedule $schedule)
{
// Schedule MY TECH Category
$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()->format('H:i:00');
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))->format('H:i:00');
$nextRun->save();
}
}
})->everyMinute();
}
/**
* Register the commands for the application.
*/
protected function commands(): void
{
$this->load(__DIR__.'/Commands');
require base_path('routes/console.php');
}
}