This commit is contained in:
2026-06-25 14:43:13 -05:00
parent 6f77fac58e
commit aaa6a9ee26
25 changed files with 1542 additions and 67 deletions

View File

@@ -0,0 +1,41 @@
import { getStringSetting } from '../models/settingsModel.js';
import { syncDynamicHostGroups } from '../models/hostGroupModel.js';
import { logger } from './logger.js';
let timer = null;
let running = false;
export function hostGroupSyncIntervalMinutes() {
const minutes = Number(getStringSetting('host_group_sync_interval_minutes', process.env.HOST_GROUP_SYNC_INTERVAL_MINUTES || '60'));
return Number.isFinite(minutes) ? minutes : 60;
}
export function startHostGroupWorker() {
const minutes = hostGroupSyncIntervalMinutes();
if (minutes <= 0) return null;
running = true;
scheduleNext(minutes);
logger.info(`host group sync scheduled every ${minutes} minute(s)`);
return timer;
}
export function stopHostGroupWorker() {
running = false;
if (timer) clearTimeout(timer);
timer = null;
}
function scheduleNext(minutes) {
if (!running || minutes <= 0) return;
timer = setTimeout(() => {
try {
const summary = syncDynamicHostGroups();
logger.info('host group sync complete', summary);
} catch (error) {
logger.error('host group sync failed', { error: error.message });
} finally {
scheduleNext(hostGroupSyncIntervalMinutes());
}
}, minutes * 60 * 1000);
timer.unref?.();
}