From 205759e3cf030a025b9157649c90d6688e165028 Mon Sep 17 00:00:00 2001 From: Maurice Date: Wed, 7 Oct 2026 16:21:16 +0200 Subject: [PATCH] fix(scheduling): do not start a cron tick while the last one still runs CronJob ran a tick on schedule even when the previous one had not finished, so an hourly auto-backup of a large library could overlap itself, and a rejected tick only reached cron's own console line. Jobs now wait for completion and log failures through the app logger. The backup also stores photos, videos and PDFs instead of deflating already-compressed files at level 9. --- server/src/nest/backup/backup.impl.ts | 7 ++++++- .../nest/scheduling/cron-registrar.service.ts | 13 +++++++++++- server/tests/unit/nest/backup.impl.test.ts | 6 +++--- .../unit/nest/cron-registrar.service.test.ts | 20 ++++++++++++++++++- 4 files changed, 40 insertions(+), 6 deletions(-) diff --git a/server/src/nest/backup/backup.impl.ts b/server/src/nest/backup/backup.impl.ts index 967eca67c..cdd32ce92 100644 --- a/server/src/nest/backup/backup.impl.ts +++ b/server/src/nest/backup/backup.impl.ts @@ -22,6 +22,7 @@ import { StorageInvalidKeyError } from '../storage/storage.types'; // --------------------------------------------------------------------------- const dataDir = path.join(__dirname, '../../../data'); +const PRECOMPRESSED = /\.(jpe?g|png|webp|gif|heic|heif|avif|mp4|mov|m4v|webm|pdf|zip|gz)$/i; // Compressed upload cap for restore archives. Defaults to 500 MB, raisable via // BACKUP_UPLOAD_LIMIT_MB for instances whose backups (uploads/ included) grow @@ -297,7 +298,11 @@ export async function createBackup(storage: StorageService, prefix: 'backup' | ' archive.file(encKeyPath, { name: '.encryption_key' }); } - for (const entry of uploadEntries) archive.file(entry.absPath, { name: entry.name }); + // Photos, videos and PDFs are compressed already; deflating them at level + // 9 costs most of a backup's CPU time for a few bytes. They are stored. + for (const entry of uploadEntries) { + archive.file(entry.absPath, { name: entry.name, store: PRECOMPRESSED.test(entry.name) }); + } // Plugin data — each plugin's own SQLite file and any blobs. This is the ONLY // copy of the user data a plugin holds, so it belongs in the backup. Checkpoint diff --git a/server/src/nest/scheduling/cron-registrar.service.ts b/server/src/nest/scheduling/cron-registrar.service.ts index 1e23ab618..9ae17b096 100644 --- a/server/src/nest/scheduling/cron-registrar.service.ts +++ b/server/src/nest/scheduling/cron-registrar.service.ts @@ -90,7 +90,18 @@ export class CronRegistrarService implements OnApplicationShutdown { } return withRequestContext(orm, () => onTick()); }; - const job = CronJob.from({ cronTime: expression, onTick: wrappedTick, start: true, timeZone }); + // waitForCompletion: a tick still running when the next one is due is not + // started twice (an hourly backup of a large library can outlast its hour). + // errorHandler: a rejected tick reaches the app log instead of cron's own + // console line. + const job = CronJob.from({ + cronTime: expression, + onTick: wrappedTick, + start: true, + timeZone, + waitForCompletion: true, + errorHandler: (error: unknown) => logError(`Cron job "${name}" failed: ${error instanceof Error ? error.message : String(error)}`), + }); this.registry.addCronJob(name, job); this.names.add(name); return true; diff --git a/server/tests/unit/nest/backup.impl.test.ts b/server/tests/unit/nest/backup.impl.test.ts index 5a49f1db1..96133bb41 100644 --- a/server/tests/unit/nest/backup.impl.test.ts +++ b/server/tests/unit/nest/backup.impl.test.ts @@ -412,8 +412,8 @@ describe('BACKUP-036 createBackup', () => { expect(storage.getLocalPathOrNull).toHaveBeenCalledWith('files', 'a.pdf'); expect(storage.getLocalPathOrNull).toHaveBeenCalledWith('journey', 'thumbs/x.jpg'); - expect(archiverInstanceMock.file).toHaveBeenCalledWith('/stub/local/path', { name: 'uploads/files/a.pdf' }); - expect(archiverInstanceMock.file).toHaveBeenCalledWith('/stub/local/path', { name: 'uploads/journey/thumbs/x.jpg' }); + expect(archiverInstanceMock.file).toHaveBeenCalledWith('/stub/local/path', { name: 'uploads/files/a.pdf', store: true }); + expect(archiverInstanceMock.file).toHaveBeenCalledWith('/stub/local/path', { name: 'uploads/journey/thumbs/x.jpg', store: true }); // The local fast path never touches getStream — this is the zero-copy default-install branch. expect(storage.getStream).not.toHaveBeenCalled(); }); @@ -609,7 +609,7 @@ describe('BACKUP-036 createBackup', () => { await createBackup(storage); - expect(archiverInstanceMock.file).toHaveBeenCalledWith('/stub/local/path', { name: 'uploads/photos/flat.jpg' }); + expect(archiverInstanceMock.file).toHaveBeenCalledWith('/stub/local/path', { name: 'uploads/photos/flat.jpg', store: true }); const names = archiverInstanceMock.file.mock.calls.map(c => c[1]?.name as string); expect(names.some(n => n?.includes('google/'))).toBe(false); expect(names.some(n => n?.includes('trek/'))).toBe(false); diff --git a/server/tests/unit/nest/cron-registrar.service.test.ts b/server/tests/unit/nest/cron-registrar.service.test.ts index 6b3381ba2..45ebd1aec 100644 --- a/server/tests/unit/nest/cron-registrar.service.test.ts +++ b/server/tests/unit/nest/cron-registrar.service.test.ts @@ -13,13 +13,22 @@ const h = vi.hoisted(() => ({ timeZone?: string; start?: boolean; onTick: () => unknown; + waitForCompletion?: boolean; + errorHandler?: (error: unknown) => void; stopped: boolean; stop(): void; }>, })); vi.mock('cron', () => ({ CronJob: { - from: (opts: { cronTime: string; timeZone?: string; start?: boolean; onTick: () => unknown }) => { + from: (opts: { + cronTime: string; + timeZone?: string; + start?: boolean; + onTick: () => unknown; + waitForCompletion?: boolean; + errorHandler?: (error: unknown) => void; + }) => { const job = { ...opts, stopped: false, @@ -76,6 +85,15 @@ describe('CronRegistrarService', () => { expect(registrar.jobCount).toBe(0); }); + it('CRONREG-014 — a tick still running is not started again, and a failed tick reaches the app log', () => { + const { registrar } = makeRegistrar(false); + registrar.register('slow-job', '0 * * * *', () => {}); + const job = h.jobs[0]!; + expect(job.waitForCompletion).toBe(true); + job.errorHandler!(new Error('disk full')); + expect(logErrorMock).toHaveBeenCalledWith('Cron job "slow-job" failed: disk full'); + }); + it('CRONREG-002 — schedules a started job with tz UTC when TZ is unset', () => { const { registrar, registry } = makeRegistrar(false); expect(registrar.register('job', '0 3 * * *', () => {})).toBe(true);