diff --git a/app/Jobs/Menu/FetchMenusJob.php b/app/Jobs/Menu/FetchMenusJob.php index 1bab4bf..ee599ec 100644 --- a/app/Jobs/Menu/FetchMenusJob.php +++ b/app/Jobs/Menu/FetchMenusJob.php @@ -5,7 +5,6 @@ use App\Enums\QueueEnum; use App\Http\Clients\HelloFresh\HelloFreshClient; use App\Jobs\Concerns\HandlesApiFailuresTrait; -use App\Jobs\Recipe\FetchRecipeJob; use App\Models\Country; use App\Models\Menu; use App\Models\Recipe; @@ -14,7 +13,6 @@ use Illuminate\Foundation\Queue\Queueable; use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\RequestException; -use Illuminate\Support\Facades\Bus; use Illuminate\Support\Facades\Context; use Throwable; @@ -131,25 +129,36 @@ protected function importMenu(array $menuData): void return; } + // TODO: Disabled to reduce API calls. Monitor NZ data quality. // Dispatch batch to fetch missing recipes, then sync menu - $jobs = []; - foreach ($missingHellofreshIds as $missingHellofreshId) { - $jobs[] = new FetchRecipeJob( - country: $this->country, - locale: $this->locale, - hellofreshId: $missingHellofreshId, - ); - } - - // Extract primitive values for serializable closure - $menuId = $menu->id; - $countryId = $this->country->id; - $recipeIds = $menuData['recipe_ids']; + // $jobs = []; + // foreach ($missingHellofreshIds as $missingHellofreshId) { + // $jobs[] = new FetchRecipeJob( + // country: $this->country, + // locale: $this->locale, + // hellofreshId: $missingHellofreshId, + // ); + // } + // + // // Extract primitive values for serializable closure + // $menuId = $menu->id; + // $countryId = $this->country->id; + // $recipeIds = $menuData['recipe_ids']; + // + // Bus::batch($jobs) + // ->name(sprintf('Fetch missing recipes for menu %d', $menu->year_week)) + // ->onQueue(QueueEnum::HelloFresh->value) + // ->then(fn () => SyncMenuRecipesJob::dispatch($menuId, $countryId, $recipeIds)) + // ->dispatch(); + + // Sync menu with existing recipes only + $recipeIds = Recipe::where('country_id', $this->country->id) + ->whereIn('hellofresh_id', $menuData['recipe_ids']) + ->pluck('id') + ->toArray(); - Bus::batch($jobs) - ->name(sprintf('Fetch missing recipes for menu %d', $menu->year_week)) - ->onQueue(QueueEnum::HelloFresh->value) - ->then(fn () => SyncMenuRecipesJob::dispatch($menuId, $countryId, $recipeIds)) - ->dispatch(); + if ($recipeIds !== []) { + $menu->recipes()->sync($recipeIds); + } } } diff --git a/app/Jobs/Recipe/FetchRecipeJob.php b/app/Jobs/Recipe/FetchRecipeJob.php index 3599c21..3fdd7db 100644 --- a/app/Jobs/Recipe/FetchRecipeJob.php +++ b/app/Jobs/Recipe/FetchRecipeJob.php @@ -7,12 +7,13 @@ use App\Jobs\Concerns\HandlesApiFailuresTrait; use App\Models\Country; use Illuminate\Bus\Batchable; +use Illuminate\Contracts\Queue\ShouldBeUnique; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Queue\Queueable; use Illuminate\Http\Client\ConnectionException; use Illuminate\Http\Client\RequestException; -class FetchRecipeJob implements ShouldQueue +class FetchRecipeJob implements ShouldBeUnique, ShouldQueue { use Batchable; use HandlesApiFailuresTrait; @@ -23,6 +24,19 @@ class FetchRecipeJob implements ShouldQueue */ public int $tries = 3; + /** + * The number of seconds after which the job's unique lock will be released. + */ + public int $uniqueFor = 300; + + /** + * The unique ID of the job. + */ + public function uniqueId(): string + { + return $this->country->id . '-' . $this->hellofreshId; + } + /** * Create a new job instance. */ diff --git a/routes/console.php b/routes/console.php index 8948a24..d8e6061 100644 --- a/routes/console.php +++ b/routes/console.php @@ -29,7 +29,7 @@ ]); Schedule::job(new SyncMenusJob()) - ->twiceDaily(); + ->twiceDailyAt(6, 18); Schedule::job(new CountryResourcesOrchestrationJob()) ->twiceDaily();