6d6fa10d91
Manual recovery после падения SnapshotProjectRoutingJob cron'а. В отличие от snapshot:backfill (ON CONFLICT DO NOTHING), snapshot:rebuild сначала DELETE'ит существующий snapshot за дату, затем INSERT'ит свежий из live state. Fail-loud strategy (Spec §4.2.6): 1. Heartbeat alarm via SchedulerHeartbeatTracker (Task 2.4 — already wired). 2. LeadRouter Log::error on missing snapshot (Task 2.5 — already wired). 3. Manual recovery: php artisan snapshot:rebuild --date=YYYY-MM-DD. NO fallback to live projects — explicit downtime + alert is safer than silent regression. NB: ->transaction() wrapper НЕ используется — конфликтует с SharesSupplierPdo shared-PDO в тестах. half-done state допустим: retry восстанавливает; на проде admin контроль и редкость вызова. Plan: docs/superpowers/plans/2026-05-26-slepok-routing-protection.md §Task 2.10 Tests added: - tests/Feature/Console/SnapshotRebuildCommandTest.php — 2 tests. Status: RED locally (Windows-native PG Project factory signal_type quirk — same as Task 2.2/2.3, memory project_slepok_protection.md). Command itself registered (php artisan list | grep snapshot). GREEN expected on CI Linux.
73 lines
2.3 KiB
PHP
73 lines
2.3 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
use App\Models\Project;
|
|
use App\Models\Tenant;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Foundation\Testing\DatabaseTransactions;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Tests\Concerns\SharesSupplierPdo;
|
|
|
|
uses(DatabaseTransactions::class);
|
|
uses(SharesSupplierPdo::class);
|
|
|
|
it('rebuilds snapshot for given date from current live (recovery after cron failure)', function (): void {
|
|
Carbon::setTestNow('2026-05-29 09:00:00', 'Europe/Moscow');
|
|
|
|
$tenant = Tenant::factory()->create(['frozen_by_balance_at' => null]);
|
|
Project::factory()->for($tenant)->asCallSignal('79161234567')->create([
|
|
'is_active' => true,
|
|
'delivery_days_mask' => 127,
|
|
'daily_limit_target' => 10,
|
|
]);
|
|
|
|
$this->artisan('snapshot:rebuild', ['--date' => '2026-05-29'])->assertSuccessful();
|
|
|
|
expect(
|
|
DB::table('project_routing_snapshots')
|
|
->where('snapshot_date', '2026-05-29')
|
|
->where('tenant_id', $tenant->id)
|
|
->count()
|
|
)->toBe(1);
|
|
|
|
Carbon::setTestNow();
|
|
});
|
|
|
|
it('replaces existing snapshot (NOT idempotent skip — full rebuild)', function (): void {
|
|
Carbon::setTestNow('2026-05-29 09:00:00', 'Europe/Moscow');
|
|
|
|
$tenant = Tenant::factory()->create(['frozen_by_balance_at' => null]);
|
|
$project = Project::factory()->for($tenant)->asCallSignal('79161234567')->create([
|
|
'is_active' => true,
|
|
'delivery_days_mask' => 127,
|
|
'daily_limit_target' => 10,
|
|
]);
|
|
|
|
// Уже есть snapshot за 2026-05-29 со stale daily_limit=3.
|
|
DB::table('project_routing_snapshots')->insert([
|
|
'snapshot_date' => '2026-05-29',
|
|
'project_id' => $project->id,
|
|
'tenant_id' => $tenant->id,
|
|
'daily_limit' => 3, // stale
|
|
'delivery_days_mask' => 127,
|
|
'regions' => '{}',
|
|
'signal_type' => 'call',
|
|
'signal_identifier' => '79161234567',
|
|
'expected_volume' => 3,
|
|
'delivered_count' => 0,
|
|
'created_at' => now(),
|
|
]);
|
|
|
|
$this->artisan('snapshot:rebuild', ['--date' => '2026-05-29'])->assertSuccessful();
|
|
|
|
// После rebuild — daily_limit обновлён до live=10.
|
|
$row = DB::table('project_routing_snapshots')
|
|
->where('snapshot_date', '2026-05-29')
|
|
->where('tenant_id', $tenant->id)
|
|
->first();
|
|
expect((int) $row->daily_limit)->toBe(10);
|
|
|
|
Carbon::setTestNow();
|
|
});
|