feat(server): replica_failure admin notifications — events bridge, hourly debounce, channel registration

This commit is contained in:
jubnl
2026-08-20 22:51:49 +02:00
parent 4671720982
commit 6db135da4b
55 changed files with 356 additions and 3 deletions
@@ -0,0 +1,34 @@
import { describe, it, expect } from 'vitest';
import { ReplicaFailureDebouncer } from '../../../../src/nest/notifications/replica-failure-debouncer';
const HOUR = 60 * 60 * 1000;
describe('ReplicaFailureDebouncer', () => {
it('DEB-001 first failure per backend is admitted with suppressed=0', () => {
let now = 1_000_000;
const debouncer = new ReplicaFailureDebouncer(HOUR, () => now);
expect(debouncer.admit('s3-bkp')).toBe(0);
});
it('DEB-002 failures inside the window are suppressed; the next one after it carries the count', () => {
let now = 1_000_000;
const debouncer = new ReplicaFailureDebouncer(HOUR, () => now);
expect(debouncer.admit('s3-bkp')).toBe(0);
now += 1000;
expect(debouncer.admit('s3-bkp')).toBeNull();
expect(debouncer.admit('s3-bkp')).toBeNull();
now += HOUR;
expect(debouncer.admit('s3-bkp')).toBe(2); // the two suppressed ones ride the summary
now += 1000;
expect(debouncer.admit('s3-bkp')).toBeNull(); // window reset
});
it('DEB-003 backends debounce independently', () => {
let now = 1_000_000;
const debouncer = new ReplicaFailureDebouncer(HOUR, () => now);
expect(debouncer.admit('a')).toBe(0);
expect(debouncer.admit('b')).toBe(0);
expect(debouncer.admit('a')).toBeNull();
expect(debouncer.admit('b')).toBeNull();
});
});
@@ -0,0 +1,34 @@
import { describe, it, expect, vi } from 'vitest';
import { StorageEventsService } from '../../../../src/nest/storage/storage-events.service';
import { StorageHealthNotifierService } from '../../../../src/nest/notifications/storage-health-notifier.service';
import type { NotificationsService } from '../../../../src/nest/notifications/notifications.service';
describe('StorageHealthNotifierService', () => {
it('NOTIF-001 subscribes on bootstrap and sends an admin-scoped replica_failure with params', () => {
const events = new StorageEventsService();
const send = vi.fn().mockResolvedValue(undefined);
const notifier = new StorageHealthNotifierService(events, { send } as unknown as NotificationsService);
notifier.onApplicationBootstrap();
events.emitReplicaFailure({ backend: 's3-bkp', key: 'backups/db.zip', op: 'put', error: 'timeout', at: 1 });
expect(send).toHaveBeenCalledWith({
event: 'replica_failure',
actorId: null,
scope: 'admin',
targetId: 0,
params: { backend: 's3-bkp', key: 'backups/db.zip', op: 'put', error: 'timeout', suppressed: '0' },
});
// Second failure inside the window: suppressed, no second send.
events.emitReplicaFailure({ backend: 's3-bkp', key: 'backups/x.zip', op: 'put', error: 'timeout', at: 2 });
expect(send).toHaveBeenCalledTimes(1);
});
it('NOTIF-002 a rejected send never throws into the emitter (write path stays safe)', () => {
const events = new StorageEventsService();
const send = vi.fn().mockRejectedValue(new Error('smtp down'));
const notifier = new StorageHealthNotifierService(events, { send } as unknown as NotificationsService);
notifier.onApplicationBootstrap();
expect(() =>
events.emitReplicaFailure({ backend: 'b', key: 'k', op: 'delete', error: 'e', at: 3 }),
).not.toThrow();
});
});