Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SyncG3WInvoicesForRegion
0.00% covered (danger)
0.00%
0 / 18
0.00% covered (danger)
0.00%
0 / 4
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 middleware
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 handle
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
 failed
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
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    public array $backoff = [10, 30, 60];
28    public int $timeout = 600;
29
30    public function __construct(public readonly string $region) {}
31
32    public function middleware(): array
33    {
34        return [
35            (new WithoutOverlapping("g3w:invoices:{$this->region}"))
36                ->expireAfter(900)
37                ->dontRelease(),
38            new RateLimited('g3w'),
39        ];
40    }
41
42    public function handle(FacturasService $service): void
43    {
44        $start = microtime(true);
45        try {
46            $service->getInvoices($this->region);
47        } finally {
48            Log::channel('g3w_invoices')->info('SyncG3WInvoicesForRegion finished', [
49                'region' => $this->region,
50                'wall_ms' => (int) round((microtime(true) - $start) * 1000),
51                'attempt' => $this->attempts(),
52            ]);
53        }
54    }
55
56    public function failed(\Throwable $e): void
57    {
58        Log::channel('g3w_invoices')->error('SyncG3WInvoicesForRegion exhausted retries', [
59            'region' => $this->region,
60            'error' => $e->getMessage(),
61        ]);
62    }
63}