fix: isolate tick errors per event, bound overlap lock

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01BCrwHHnGCB5XH968Nxokqw
This commit is contained in:
nikita.hohlov
2026-09-04 07:46:35 -03:00
parent affac4ce1a
commit 6138ac7bb0
3 changed files with 37 additions and 2 deletions

View File

@@ -17,7 +17,9 @@ use App\Scheduling\WindowResolver;
use Illuminate\Contracts\Cache\Repository as Cache; use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str; use Illuminate\Support\Str;
use Throwable;
/** /**
* Один тик планировщика (раз в минуту): переходы состояний по датам и решение, * Один тик планировщика (раз в минуту): переходы состояний по датам и решение,
@@ -39,7 +41,15 @@ final class Tick
Event::query() Event::query()
->whereIn('state', EventState::nonTerminalValues()) ->whereIn('state', EventState::nonTerminalValues())
->with('user') ->with('user')
->chunkById(200, fn (Collection $events) => $events->each(fn (Event $e) => $this->process($e))); ->chunkById(200, function (Collection $events) {
foreach ($events as $event) {
try {
$this->process($event);
} catch (Throwable $e) {
Log::error('hado.tick: событие пропущено', ['event_id' => $event->id, 'error' => $e->getMessage()]);
}
}
});
} }
private function process(Event $event): void private function process(Event $event): void

View File

@@ -2,5 +2,5 @@
use Illuminate\Support\Facades\Schedule; use Illuminate\Support\Facades\Schedule;
Schedule::command('hado:tick')->everyMinute()->withoutOverlapping()->runInBackground(); Schedule::command('hado:tick')->everyMinute()->withoutOverlapping(5)->runInBackground();
Schedule::command('hado:purge')->dailyAt('04:00'); Schedule::command('hado:purge')->dailyAt('04:00');

View File

@@ -18,6 +18,8 @@ use App\Models\User;
use Carbon\CarbonImmutable; use Carbon\CarbonImmutable;
use Illuminate\Foundation\Testing\RefreshDatabase; use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Tests\Support\FakeChannel; use Tests\Support\FakeChannel;
use Tests\Support\FakeRegistry; use Tests\Support\FakeRegistry;
use Tests\TestCase; use Tests\TestCase;
@@ -187,4 +189,27 @@ class TickTest extends TestCase
$this->webhook->presence = Presence::Absent; $this->webhook->presence = Presence::Absent;
$this->assertSame(Presence::Present, Cache::get('presence:'.Channel::where('type', 'webhook')->sole()->id)); $this->assertSame(Presence::Present, Cache::get('presence:'.Channel::where('type', 'webhook')->sole()->id));
} }
public function test_poison_event_does_not_stop_the_pass(): void
{
Log::spy();
$poison = User::create(['login' => 'poison', 'tz' => 'UTC', 'quiet_start' => '22:00:00', 'quiet_end' => '09:00:00']);
DB::table('users')->where('id', $poison->id)->update(['tz' => 'Not/AZone']);
Channel::create(['user_id' => $poison->id, 'type' => ChannelType::Webhook, 'config' => ['deliver_url' => 'u', 'presence_url' => 'p'], 'enabled' => true]);
Event::create([
'source_id' => $this->source->id, 'user_id' => $poison->id, 'source_ref' => 'r', 'topic' => uniqid(),
'due_mode' => DueMode::Local, 'due_date' => '2026-09-30', 'due_time' => '24:00:00', 'fire_on' => '2026-08-31',
'after_due' => AfterDue::Keep, 'payload' => ['title' => 'T'], 'state' => EventState::Scheduled,
'quiet_until' => CarbonImmutable::parse('2000-01-01Z'),
]);
$this->event('2026-09-30', '2026-08-31');
$this->webhook->presence = Presence::Present;
$this->tickAt('2026-09-05T10:00:00Z');
$this->assertSame(1, Delivery::count(), 'здоровое событие доставлено, несмотря на отравленное');
Log::shouldHaveReceived('error')->once();
}
} }