Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
BulkSyncCofraiRun
0.00% covered (danger)
0.00%
0 / 39
0.00% covered (danger)
0.00%
0 / 4
30
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 / 27
0.00% covered (danger)
0.00%
0 / 1
6
 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\CofraiBulkSyncService;
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 * Cofrai mirror of BulkSyncG3WRun (FIRE-1170): per-region worker for the
17 * Cofrai "Sincronización masiva" admin tool. One job per region per run —
18 * the controller fans out N jobs (one per selected region) and they share
19 * the `cofrai` rate-limiter with the regular Cofrai cron sync. The
20 * `WithoutOverlapping` lock keys on (region, runId) so two different runs
21 * can target the same region concurrently without colliding but the same
22 * run can't double-process a region if it gets re-dispatched.
23 *
24 * Mirrors the SyncCofraiRegion pattern (FIRE-1077).
25 */
26class BulkSyncCofraiRun implements ShouldQueue
27{
28    use Dispatchable;
29    use InteractsWithQueue;
30    use Queueable;
31    use SerializesModels;
32
33    public int $tries = 3;
34
35    /** @var array<int,int> seconds between retries */
36    public array $backoff = [30, 90, 300];
37
38    /** Hard kill at 1 hour — bulk runs can be longer than the daily cron. */
39    public int $timeout = 3600;
40
41    /**
42     * @param  array<int,string>  $fields  UI checkbox names (see CofraiQuotationsService::BULK_SYNC_FIELD_MAP)
43     */
44    public function __construct(
45        public readonly int $runId,
46        public readonly string $region,
47        public readonly string $dateFrom,
48        public readonly string $dateTo,
49        public readonly array $fields,
50        public readonly bool $suppressOps,
51        public readonly bool $suppressFin,
52    ) {}
53
54    public function middleware(): array
55    {
56        return [
57            (new WithoutOverlapping("cofrai:bulk:{$this->region}:{$this->runId}"))
58                ->expireAfter(7200)
59                ->dontRelease(),
60            new RateLimited('cofrai'),
61        ];
62    }
63
64    public function handle(CofraiBulkSyncService $service): void
65    {
66        // The job can take 20+ minutes for large date ranges. Disable the
67        // PHP execution timeout — this matters when the job runs via
68        // `dispatchAfterResponse` on a web worker (which inherits the
69        // request's max_execution_time). A real queue worker doesn't have
70        // this limit.
71        @set_time_limit(0);
72
73        $start = microtime(true);
74
75        try {
76            $service->bulkSyncByFilters(
77                $this->runId,
78                $this->region,
79                $this->dateFrom,
80                $this->dateTo,
81                $this->fields,
82                $this->suppressOps,
83                $this->suppressFin,
84            );
85            $status = 'ok';
86        } catch (\Throwable $e) {
87            $status = 'failed';
88            Log::channel('cofrai')->error('BulkSyncCofraiRun failed', [
89                'run_id' => $this->runId,
90                'region' => $this->region,
91                'error' => $e->getMessage(),
92            ]);
93            throw $e;
94        } finally {
95            Log::channel('cofrai')->info('BulkSyncCofraiRun finished', [
96                'run_id' => $this->runId,
97                'region' => $this->region,
98                'wall_ms' => (int) round((microtime(true) - $start) * 1000),
99                'status' => $status ?? 'unknown',
100                'attempt' => $this->attempts(),
101            ]);
102        }
103    }
104
105    public function failed(\Throwable $e): void
106    {
107        Log::channel('cofrai')->error('BulkSyncCofraiRun exhausted retries', [
108            'run_id' => $this->runId,
109            'region' => $this->region,
110            'error' => $e->getMessage(),
111        ]);
112    }
113}