refactor: manage monitor via gateway hooks
This commit is contained in:
33
plugin/hooks/gateway-start.ts
Normal file
33
plugin/hooks/gateway-start.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
import { hostname } from 'os';
|
||||
import { getPluginConfig } from '../core/config';
|
||||
import { startManagedMonitor } from '../core/managed-monitor';
|
||||
|
||||
export function registerGatewayStartHook(api: any, deps: {
|
||||
logger: any;
|
||||
pushMetaToMonitor: () => Promise<void>;
|
||||
startCalendarScheduler: () => void;
|
||||
setMetaPushInterval: (handle: ReturnType<typeof setInterval>) => void;
|
||||
}) {
|
||||
const { logger, pushMetaToMonitor, startCalendarScheduler, setMetaPushInterval } = deps;
|
||||
|
||||
api.on('gateway_start', () => {
|
||||
logger.info('HarborForge plugin active');
|
||||
const live = getPluginConfig(api);
|
||||
|
||||
startManagedMonitor(logger, {
|
||||
managedMonitor: live.managedMonitor,
|
||||
monitor_port: live.monitor_port,
|
||||
logLevel: live.logLevel,
|
||||
});
|
||||
|
||||
const intervalSec = live.reportIntervalSec || 30;
|
||||
setTimeout(() => void pushMetaToMonitor(), 2000);
|
||||
setMetaPushInterval(setInterval(() => void pushMetaToMonitor(), intervalSec * 1000));
|
||||
|
||||
if (live.enabled !== false && live.calendarEnabled !== false) {
|
||||
setTimeout(() => startCalendarScheduler(), 5000);
|
||||
}
|
||||
|
||||
logger.info(`HarborForge startup config identifier=${live.identifier || hostname()} backend=${live.backendUrl}`);
|
||||
});
|
||||
}
|
||||
23
plugin/hooks/gateway-stop.ts
Normal file
23
plugin/hooks/gateway-stop.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { stopManagedMonitor } from '../core/managed-monitor';
|
||||
|
||||
export function registerGatewayStopHook(api: any, deps: {
|
||||
logger: any;
|
||||
getMetaPushInterval: () => ReturnType<typeof setInterval> | null;
|
||||
clearMetaPushInterval: () => void;
|
||||
stopCalendarScheduler: () => void;
|
||||
}) {
|
||||
const { logger, getMetaPushInterval, clearMetaPushInterval, stopCalendarScheduler } = deps;
|
||||
|
||||
api.on('gateway_stop', () => {
|
||||
logger.info('HarborForge plugin stopping');
|
||||
|
||||
const handle = getMetaPushInterval();
|
||||
if (handle) {
|
||||
clearInterval(handle);
|
||||
clearMetaPushInterval();
|
||||
}
|
||||
|
||||
stopCalendarScheduler();
|
||||
stopManagedMonitor(logger);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user