mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-04 18:04:18 +00:00
feat(server): give the scheduler a contract with the container (#1827)
The scheduler runs outside the Nest container but needs four things inside it: backupService, trekPhotoCache, placePhotoCache and airtrailSync. It reaches all four with a require() or await import() at call time. This settles how that should work before four separate folds each invent their own answer, and wires one consumer as proof. It takes the dependencies from the container rather than growing four more bridges, for a reason that showed up while writing the previous PRs: adding one constructor parameter to a service meant hand-editing five bridge files that rebuild their services with `new`. app.get() has no such cost, and hands back the container singleton instead of a second instance. The scheduler declares what it needs structurally — a `backups` with a createBackup — and never names BackupService. That is not fastidiousness: an import of the class would pull in src/nest/backup → src/services/backupService → src/scheduler, which is the exact cycle the lazy `await import()` in runBackup existed to dodge. With the dependency handed in, the scheduler stops importing backupService entirely and the cycle is gone; backupService still imports the scheduler for VALID_INTERVALS, but now only in one direction. index.ts fills the contract from the app buildApp() already initialised, right before the first cron starts, the way bootstrap.ts hands the /mcp handler its registry. Two smaller things fall out. The Nest BackupService was swallowing createBackup's prefix parameter, so it could only ever produce 'backup' names; it forwards it now. And the scheduler can be started without a container at all — its unit tests do exactly that — so runBackup says so in the log instead of throwing, with a case covering it. The other three consumers stay on their require() calls; they move with their own folds.
This commit is contained in:
@@ -62,7 +62,8 @@ vi.mock('../../src/config', () => ({
|
||||
}));
|
||||
|
||||
import path from 'node:path';
|
||||
import { start } from '../../src/scheduler';
|
||||
import { start, setSchedulerDeps } from '../../src/scheduler';
|
||||
import { createBackup } from '../../src/services/backupService';
|
||||
|
||||
const liveDb = path.join(__dirname, '../../data', 'travel.db');
|
||||
|
||||
@@ -82,6 +83,9 @@ function stubArchiver(): Record<string, (arg?: unknown) => void> {
|
||||
/** Starts the scheduler with auto-backup enabled and hands back the cron callback. */
|
||||
function scheduledRun(): () => Promise<void> {
|
||||
fsMock.readFileSync.mockReturnValue(JSON.stringify({ enabled: true, interval: 'daily', keep_days: 7 }));
|
||||
// The same wiring index.ts does from the container, with the real service
|
||||
// function behind it — the run below is still the production code path.
|
||||
setSchedulerDeps({ backups: { createBackup } });
|
||||
start();
|
||||
return cronMock.schedule.mock.calls.at(-1)?.[1] as () => Promise<void>;
|
||||
}
|
||||
@@ -154,3 +158,22 @@ describe('auto-backup run', () => {
|
||||
expect(fsMock.readdirSync).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('auto-backup without container dependencies', () => {
|
||||
it('logs and skips instead of throwing when setSchedulerDeps was never called', async () => {
|
||||
// A fresh module registry, so the deps this file's other cases install are
|
||||
// not still sitting in the scheduler's module state.
|
||||
vi.resetModules();
|
||||
const { start: freshStart } = await import('../../src/scheduler');
|
||||
|
||||
vi.clearAllMocks();
|
||||
fsMock.readFileSync.mockReturnValue(JSON.stringify({ enabled: true, interval: 'daily', keep_days: 7 }));
|
||||
freshStart();
|
||||
await (cronMock.schedule.mock.calls.at(-1)?.[1] as () => Promise<void>)();
|
||||
|
||||
expect(logMock.logError).toHaveBeenCalledWith(
|
||||
'Auto-Backup: skipped, the scheduler was started without its container dependencies',
|
||||
);
|
||||
expect(archiverMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user