Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
SyncG3WClientsForRegion
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
4 / 4
5
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%
17 / 17
100.00% covered (success)
100.00%
1 / 1
2
 failed
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace App\Jobs;
4
5use App\Services\GestionaClientSyncService;
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 client sync.
17 */
18class SyncG3WClientsForRegion implements ShouldQueue
19{
20    use Dispatchable;
21    use InteractsWithQueue;
22    use Queueable;
23    use SerializesModels;
24
25    public int $tries = 3;
26
27    public array $backoff = [10, 30, 60];
28
29    public int $timeout = 600;
30
31    public function __construct(
32        public readonly string $date,
33        public readonly string $region,
34    ) {}
35
36    public function middleware(): array
37    {
38        return [
39            (new WithoutOverlapping("g3w:clients:{$this->region}"))
40                ->expireAfter(900)
41                ->dontRelease(),
42            new RateLimited('g3w'),
43        ];
44    }
45
46    public function handle(GestionaClientSyncService $service): void
47    {
48        $start = microtime(true);
49        try {
50            $stats = $service->syncRegion($this->date, $this->region);
51            Log::channel('g3w')->info('SyncG3WClientsForRegion finished', [
52                'region' => $this->region,
53                'date' => $this->date,
54                'wall_ms' => (int) round((microtime(true) - $start) * 1000),
55                'stats' => $stats,
56                'attempt' => $this->attempts(),
57            ]);
58        } catch (\Throwable $e) {
59            Log::channel('g3w')->error('SyncG3WClientsForRegion failed', [
60                'region' => $this->region,
61                'date' => $this->date,
62                'wall_ms' => (int) round((microtime(true) - $start) * 1000),
63                'error' => $e->getMessage(),
64            ]);
65            throw $e;
66        }
67    }
68
69    public function failed(\Throwable $e): void
70    {
71        Log::channel('g3w')->error('SyncG3WClientsForRegion exhausted retries', [
72            'region' => $this->region,
73            'date' => $this->date,
74            'error' => $e->getMessage(),
75        ]);
76    }
77}