Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| SyncCofraiRegion | |
0.00% |
0 / 26 |
|
0.00% |
0 / 4 |
30 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| middleware | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| handle | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
6 | |||
| failed | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Jobs; |
| 4 | |
| 5 | use App\Services\CofraiQuotationsService; |
| 6 | use Illuminate\Bus\Queueable; |
| 7 | use Illuminate\Contracts\Queue\ShouldQueue; |
| 8 | use Illuminate\Foundation\Bus\Dispatchable; |
| 9 | use Illuminate\Queue\InteractsWithQueue; |
| 10 | use Illuminate\Queue\Middleware\RateLimited; |
| 11 | use Illuminate\Queue\Middleware\WithoutOverlapping; |
| 12 | use Illuminate\Queue\SerializesModels; |
| 13 | use Illuminate\Support\Facades\Log; |
| 14 | |
| 15 | /** |
| 16 | * FIRE-1077: Cofrai parallel to SyncG3WRegion. |
| 17 | * Per-region Cofrai budget sync, dispatched in parallel from |
| 18 | * CofraiQuotationsSync::handle(). |
| 19 | * |
| 20 | * Uses the 'cofrai' rate-limiter bucket (not 'g3w') so the two |
| 21 | * integrations don't compete for the same external-API budget. |
| 22 | */ |
| 23 | class SyncCofraiRegion implements ShouldQueue |
| 24 | { |
| 25 | use Dispatchable; |
| 26 | use InteractsWithQueue; |
| 27 | use Queueable; |
| 28 | use SerializesModels; |
| 29 | |
| 30 | public int $tries = 3; |
| 31 | |
| 32 | /** @var array<int,int> backoff between retries in seconds */ |
| 33 | public array $backoff = [10, 30, 60]; |
| 34 | |
| 35 | /** Job is killed if it runs longer than this. */ |
| 36 | public int $timeout = 600; |
| 37 | |
| 38 | public function __construct( |
| 39 | public readonly string $date, |
| 40 | public readonly string $name, |
| 41 | public readonly string $region, |
| 42 | ) {} |
| 43 | |
| 44 | public function middleware(): array |
| 45 | { |
| 46 | return [ |
| 47 | (new WithoutOverlapping("cofrai:region:{$this->region}")) |
| 48 | ->expireAfter(900) |
| 49 | ->dontRelease(), |
| 50 | new RateLimited('cofrai'), |
| 51 | ]; |
| 52 | } |
| 53 | |
| 54 | public function handle(CofraiQuotationsService $service): void |
| 55 | { |
| 56 | $start = microtime(true); |
| 57 | |
| 58 | try { |
| 59 | $service->syncByDate($this->date, $this->name, $this->region); |
| 60 | $status = 'ok'; |
| 61 | } catch (\Throwable $e) { |
| 62 | $status = 'failed'; |
| 63 | Log::channel('cofrai')->error("SyncCofraiRegion failed: {$this->region} — {$e->getMessage()}"); |
| 64 | throw $e; |
| 65 | } finally { |
| 66 | Log::channel('cofrai')->info('SyncCofraiRegion finished', [ |
| 67 | 'region' => $this->region, |
| 68 | 'date' => $this->date, |
| 69 | 'wall_ms' => (int) round((microtime(true) - $start) * 1000), |
| 70 | 'status' => $status ?? 'unknown', |
| 71 | 'attempt' => $this->attempts(), |
| 72 | ]); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | public function failed(\Throwable $e): void |
| 77 | { |
| 78 | Log::channel('cofrai')->error('SyncCofraiRegion exhausted retries', [ |
| 79 | 'region' => $this->region, |
| 80 | 'date' => $this->date, |
| 81 | 'error' => $e->getMessage(), |
| 82 | ]); |
| 83 | } |
| 84 | } |