Files
app-agent/app/Jobs/SyncCafeBazaarApp.php
T
2026-08-24 19:44:24 +03:30

68 lines
1.6 KiB
PHP

<?php
namespace App\Jobs;
use App\Models\MobileApp;
use App\Services\CafeBazaar\SyncCafeBazaarAppAction;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\Middleware\RateLimited;
use Throwable;
class SyncCafeBazaarApp implements ShouldBeUnique, ShouldQueue
{
use Queueable;
public int $tries = 8;
public int $timeout = 300;
public int $uniqueFor = 600;
public function __construct(
public readonly int $mobileAppId,
public readonly bool $force = false,
) {}
public function uniqueId(): string
{
return (string) $this->mobileAppId;
}
/** @return array<int, int> */
public function backoff(): array
{
return [60, 300, 900];
}
/** @return array<int, object> */
public function middleware(): array
{
return [(new RateLimited('cafe-bazaar-sync'))->releaseAfter(60)];
}
/**
* Execute the job.
*/
public function handle(SyncCafeBazaarAppAction $sync): void
{
$app = MobileApp::query()->find($this->mobileAppId);
if ($app === null || (! $app->bazaar_sync_enabled && ! $this->force)) {
return;
}
$sync->execute($app, $this->force);
}
public function failed(?Throwable $exception): void
{
MobileApp::query()->whereKey($this->mobileAppId)->update([
'bazaar_sync_status' => 'failed',
'bazaar_last_synced_at' => now(),
'bazaar_sync_error' => $exception?->getMessage() ?? 'The Bazaar synchronization job failed.',
]);
}
}