Log failed visit maintenance attempts #57

Merged
eleith merged 1 commits from fix/log-visit-maintenance-failures into main 2026-11-25 14:09:42 +00:00
3 changed files with 27 additions and 2 deletions

No files matched your search

@@ -0,0 +1,5 @@
---
'@goto/worker': patch
---
Log failed visit maintenance attempts without exposing error details or changing retry behavior.
@@ -1,6 +1,7 @@
import { loadConfig } from '@goto/config';
import { afterEach, expect, test, vi } from 'vitest';
import type { WorkerContext } from '../services/context.js';
import { logger } from '../services/logger.js';
import { maintainVisits } from '../services/maintain-visits.js';
import { runVisitMaintenance, type VisitMaintenanceStep } from './maintain-visits.js';
@@ -64,3 +65,16 @@ test('zero retention keeps scheduled hit accounting with the same durable jitter
expect(sleep).toHaveBeenCalledOnce();
expect(maintainVisits).toHaveBeenCalledExactlyOnceWith(ctx);
});
test('logs a failed maintenance attempt without exposing error details or swallowing the failure', async () => {
const failure = new Error('Private storage details');
vi.mocked(maintainVisits).mockRejectedValueOnce(failure);
const log = vi.spyOn(logger, 'error').mockImplementation(() => undefined);
try {
const { step } = steps();
await expect(runVisitMaintenance(context(), { scheduled: true }, step)).rejects.toBe(failure);
expect(log).toHaveBeenCalledExactlyOnceWith('Visit maintenance failed');
} finally {
log.mockRestore();
}
});
+8 -2
View File
@@ -1,6 +1,7 @@
import { randomInt } from 'node:crypto';
import type { OpenWorkflow } from 'openworkflow';
import type { WorkerContext } from '../services/context.js';
import { logger } from '../services/logger.js';
import { maintainVisits } from '../services/maintain-visits.js';
import { visitMaintenance, type VisitMaintenanceInput } from './specs.js';
@@ -21,8 +22,13 @@ export async function runVisitMaintenance(
const duration: `${number}s` = `${seconds}s`;
await step.sleep('retention-jitter', duration);
}
function maintain(): Promise<void> {
return maintainVisits(context);
async function maintain(): Promise<void> {
try {
await maintainVisits(context);
} catch (error) {
logger.error('Visit maintenance failed');
throw error;
}
}
// Preserve the durable step identity used by already-queued maintenance runs.
await step.run({ name: 'retain-visits' }, maintain);