Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions app/Jobs/Menu/FetchMenusJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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);
}
}
}
16 changes: 15 additions & 1 deletion app/Jobs/Recipe/FetchRecipeJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*/
Expand Down
2 changes: 1 addition & 1 deletion routes/console.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
]);

Schedule::job(new SyncMenusJob())
->twiceDaily();
->twiceDailyAt(6, 18);

Schedule::job(new CountryResourcesOrchestrationJob())
->twiceDaily();
Expand Down
Loading