Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
55.17% covered (warning)
55.17%
16 / 29
50.00% covered (danger)
50.00%
2 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
SyncG3WClientsForRegion
55.17% covered (warning)
55.17%
16 / 29
50.00% covered (danger)
50.00%
2 / 4
7.25
0.00% covered (danger)
0.00%
0 / 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
52.94% covered (warning)
52.94%
9 / 17
0.00% covered (danger)
0.00%
0 / 1
2.42
 failed
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
2
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    public array $backoff = [10, 30, 60];
27    public int $timeout = 600;
28
29    public function __construct(
30        public readonly string $date,
31        public readonly string $region,
32    ) {}
33
34    public function middleware(): array
35    {
36        return [
37            (new WithoutOverlapping("g3w:clients:{$this->region}"))
38                ->expireAfter(900)
39                ->dontRelease(),
40            new RateLimited('g3w'),
41        ];
42    }
43
44    public function handle(GestionaClientSyncService $service): void
45    {
46        $start = microtime(true);
47        try {
48            $stats = $service->syncRegion($this->date, $this->region);
49            Log::channel('g3w')->info('SyncG3WClientsForRegion finished', [
50                'region' => $this->region,
51                'date' => $this->date,
52                'wall_ms' => (int) round((microtime(true) - $start) * 1000),
53                'stats' => $stats,
54                'attempt' => $this->attempts(),
55            ]);
56        } catch (\Throwable $e) {
57            Log::channel('g3w')->error('SyncG3WClientsForRegion failed', [
58                'region' => $this->region,
59                'date' => $this->date,
60                'wall_ms' => (int) round((microtime(true) - $start) * 1000),
61                'error' => $e->getMessage(),
62            ]);
63            throw $e;
64        }
65    }
66
67    public function failed(\Throwable $e): void
68    {
69        Log::channel('g3w')->error('SyncG3WClientsForRegion exhausted retries', [
70            'region' => $this->region,
71            'date' => $this->date,
72            'error' => $e->getMessage(),
73        ]);
74    }
75}