Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SyncG3WInvoicesForRegion
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 middleware
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 handle
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 failed
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Jobs;
4
5use App\Services\FacturasService;
6use Illuminate\Bus\Queueable;
7use Illuminate\Contracts\Queue\ShouldQueue;
8use Illuminate\Foundation\Bus\Dispatchable;
9use Illuminate\Queue\InteractsWithQueue;
10use Illuminate\Queue\Middleware\RateLimited;
11use Illuminate\Queue\Middleware\WithoutOverlapping;
12use Illuminate\Queue\SerializesModels;
13use Illuminate\Support\Facades\Log;
14
15/**
16 * FIRE-1151: per-region invoice sync. Heavier than budgets — ~3 G3W calls
17 * per invoice — so this job runs on the same 'g3w' rate-limiter bucket.
18 */
19class SyncG3WInvoicesForRegion implements ShouldQueue
20{
21    use Dispatchable;
22    use InteractsWithQueue;
23    use Queueable;
24    use SerializesModels;
25
26    public int $tries = 3;
27
28    public array $backoff = [10, 30, 60];
29
30    public int $timeout = 600;
31
32    public function __construct(public readonly string $region) {}
33
34    public function middleware(): array
35    {
36        return [
37            (new WithoutOverlapping("g3w:invoices:{$this->region}"))
38                ->expireAfter(900)
39                ->dontRelease(),
40            new RateLimited('g3w'),
41        ];
42    }
43
44    public function handle(FacturasService $service): void
45    {
46        $start = microtime(true);
47        try {
48            $service->getInvoices($this->region);
49        } finally {
50            Log::channel('g3w_invoices')->info('SyncG3WInvoicesForRegion finished', [
51                'region' => $this->region,
52                'wall_ms' => (int) round((microtime(true) - $start) * 1000),
53                'attempt' => $this->attempts(),
54            ]);
55        }
56    }
57
58    public function failed(\Throwable $e): void
59    {
60        Log::channel('g3w_invoices')->error('SyncG3WInvoicesForRegion exhausted retries', [
61            'region' => $this->region,
62            'error' => $e->getMessage(),
63        ]);
64    }
65}