Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
6.25% |
2 / 32 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CofraiQuotationsRetryFailed | |
6.25% |
2 / 32 |
|
50.00% |
1 / 2 |
75.74 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| handle | |
0.00% |
0 / 30 |
|
0.00% |
0 / 1 |
72 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace App\Console\Commands; |
| 4 | |
| 5 | use App\Services\CofraiQuotationsService; |
| 6 | use Illuminate\Console\Command; |
| 7 | use Illuminate\Support\Facades\Log; |
| 8 | |
| 9 | class CofraiQuotationsRetryFailed extends Command |
| 10 | { |
| 11 | /** |
| 12 | * The name and signature of the console command. |
| 13 | * |
| 14 | * FIRE-982 (2026-04-29): mirrored G3W's `quotations:retry-failed` |
| 15 | * signature so the Cofrai retry has the same FIRE-1025 protection |
| 16 | * (per-run cap + time-bounded scan + per-region filter). Without |
| 17 | * this, a single fire would iterate every failed ID ever logged |
| 18 | * for the company — same runaway shape that pinned MySQL at 270% |
| 19 | * CPU on G3W on 2026-04-27. |
| 20 | * |
| 21 | * @var string |
| 22 | */ |
| 23 | protected $signature = 'cofrai-quotations:retry-failed |
| 24 | {name?} |
| 25 | {--region=} |
| 26 | {--verbose-errors} |
| 27 | {--limit=200 : Max IDs to process per run (across all regions)} |
| 28 | {--days=7 : Only consider failed-sync log rows newer than this many days}'; |
| 29 | |
| 30 | /** |
| 31 | * The console command description. |
| 32 | * |
| 33 | * @var string |
| 34 | */ |
| 35 | protected $description = 'Re-syncs Cofrai budgets that previously failed (FIRE-982)'; |
| 36 | |
| 37 | /** |
| 38 | * All Cofrai-active regions. Mirrors `QuotationsRetryFailed::REGIONS` |
| 39 | * so the operator-facing UX is identical between the two commands. |
| 40 | */ |
| 41 | private const REGIONS = [ |
| 42 | 'Cataluña', |
| 43 | 'Madrid', |
| 44 | 'Comunidad Valenciana', |
| 45 | 'Andalucía', |
| 46 | 'Baleares', |
| 47 | ]; |
| 48 | |
| 49 | private CofraiQuotationsService $cofraiQuotationsService; |
| 50 | |
| 51 | public function __construct(CofraiQuotationsService $cofraiQuotationsService) |
| 52 | { |
| 53 | parent::__construct(); |
| 54 | $this->cofraiQuotationsService = $cofraiQuotationsService; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Execute the console command. |
| 59 | */ |
| 60 | public function handle(): int |
| 61 | { |
| 62 | $name = $this->argument('name') ?? 'System'; |
| 63 | $regionOption = $this->option('region'); |
| 64 | $regions = $regionOption ? [$regionOption] : self::REGIONS; |
| 65 | |
| 66 | // FIRE-1025 / FIRE-977 follow-up: cap the work each invocation |
| 67 | // does so a single run can't pile up on top of the next one. |
| 68 | // The limit is shared across regions — once we've burned through |
| 69 | // it, remaining regions are deferred to the next fire. |
| 70 | $remainingBudget = max(1, (int) $this->option('limit')); |
| 71 | $daysWindow = max(1, (int) $this->option('days')); |
| 72 | |
| 73 | $hasFailure = false; |
| 74 | |
| 75 | foreach ($regions as $region) { |
| 76 | if ($remainingBudget <= 0) { |
| 77 | Log::channel('cofrai')->info('cofrai-quotations:retry-failed - Hit per-run budget; deferring remaining regions to next run.'); |
| 78 | $this->info('Hit per-run budget; deferring remaining regions to the next run.'); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | try { |
| 83 | $result = $this->cofraiQuotationsService->syncErrorBudgets( |
| 84 | $name, |
| 85 | $region, |
| 86 | $remainingBudget, |
| 87 | $daysWindow, |
| 88 | ); |
| 89 | |
| 90 | $processed = (int) ($result['processed'] ?? 0); |
| 91 | $remainingBudget -= $processed; |
| 92 | |
| 93 | if (! empty($result['skipped'])) { |
| 94 | // Quiet skip — region simply isn't configured for Cofrai. |
| 95 | // Don't count this as a failure. |
| 96 | $this->line("Region {$region}: skipped — ".($result['reason'] ?? 'not applicable')); |
| 97 | } elseif (! empty($result['success'])) { |
| 98 | $this->info("Region {$region}: processed {$processed}."); |
| 99 | } else { |
| 100 | $this->warn("Region {$region}: ".($result['error'] ?? 'unknown error')); |
| 101 | $hasFailure = true; |
| 102 | } |
| 103 | } catch (\Exception $e) { |
| 104 | Log::channel('cofrai')->error("cofrai-quotations:retry-failed - Error processing region {$region}: ".$e->getMessage()); |
| 105 | $this->error("Error processing region {$region}: ".$e->getMessage()); |
| 106 | $hasFailure = true; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | return $hasFailure ? Command::FAILURE : Command::SUCCESS; |
| 111 | } |
| 112 | } |