feat: in-process nightly scheduler + pause at window end
Hook registers a 15-minute schedule that starts/resumes the backfill only inside the maintenance window (no external cron or token). The backfill pauses when the window closes and resumes next night. UI shows paused state and last-run errors.
This commit is contained in:
parent
18db52a47c
commit
ee39633ad7
6 changed files with 60 additions and 8 deletions
|
|
@ -1,8 +1,11 @@
|
|||
import { defineHook } from '@directus/extensions-sdk';
|
||||
import { runBackfill } from '../lib/backfill';
|
||||
import { recordOp } from '../lib/diagnostics';
|
||||
import { buildRegistry, isImageType, loadCustomPresets } from '../lib/presets';
|
||||
import { readSettings } from '../lib/settings';
|
||||
import { readRun } from '../lib/state';
|
||||
import { warmFile } from '../lib/warmer';
|
||||
import { isInWindow } from '../lib/window';
|
||||
|
||||
interface UploadMeta {
|
||||
key?: string;
|
||||
|
|
@ -10,13 +13,19 @@ interface UploadMeta {
|
|||
payload?: { type?: string | null; collection?: string };
|
||||
}
|
||||
|
||||
export default defineHook(({ action }, { services, getSchema, env, logger }) => {
|
||||
export default defineHook(({ action, schedule }, { services, database, getSchema, env, logger }) => {
|
||||
action('files.upload', (meta) => {
|
||||
void warmUpload(meta as UploadMeta).catch((error) =>
|
||||
logger.error(error, 'image-previews: upload warm failed'),
|
||||
);
|
||||
});
|
||||
|
||||
schedule('*/15 * * * *', () => {
|
||||
void scheduledBackfill().catch((error) =>
|
||||
logger.error(error, 'image-previews: scheduled run failed'),
|
||||
);
|
||||
});
|
||||
|
||||
async function warmUpload(meta: UploadMeta): Promise<void> {
|
||||
const settings = await readSettings(env);
|
||||
|
||||
|
|
@ -53,4 +62,27 @@ export default defineHook(({ action }, { services, getSchema, env, logger }) =>
|
|||
|
||||
if (settings.debug) logger.info({ fileId, summary }, 'image-previews: warmed upload');
|
||||
}
|
||||
|
||||
async function scheduledBackfill(): Promise<void> {
|
||||
const settings = await readSettings(env);
|
||||
|
||||
if (!settings.enabled) return;
|
||||
if (!isInWindow(settings.windowStart, settings.windowEnd)) return;
|
||||
|
||||
const run = await readRun(env);
|
||||
|
||||
if (run?.running) return;
|
||||
|
||||
const finishedToday = run?.finishedAt
|
||||
? new Date(run.finishedAt).toDateString() === new Date().toDateString()
|
||||
: false;
|
||||
|
||||
if (finishedToday && run && run.done >= run.total) return;
|
||||
|
||||
logger.info('image-previews: scheduled backfill starting');
|
||||
|
||||
void runBackfill({ services, database, getSchema, env, logger }, {}).catch((error) =>
|
||||
logger.error(error, 'image-previews: scheduled backfill failed'),
|
||||
);
|
||||
}
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue