mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
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.
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user