import { CoreStart , Logger } from 'kibana/server' ; import { RunContext , TaskManagerSetupContract , TaskManagerStartContract , } from '../../../task_manager/server' ; import { AlertsConfig } from '../config' ; import { AlertingPluginsStart } from '../plugin' ; import { HealthStatus } from '../types' ; import { getAlertingHealthStatus } from './get_health' ; export const HEALTH_TASK_TYPE = 'alerting_health_check' ; export const HEALTH_TASK_ID = `Alerting-${ HEALTH_TASK_TYPE }` ; export function initializeAlertingHealth ( logger , taskManager , coreStartServices ) { registerAlertingHealthCheckTask ( logger , taskManager , coreStartServices ) ; } export async function scheduleAlertingHealthCheck ( logger , config , taskManager ) { try { const interval = ( await config ) . healthCheck . interval ; await taskManager . ensureScheduled ( { id : HEALTH_TASK_ID , taskType : HEALTH_TASK_TYPE , schedule : { interval , } , state : { } , params : { } , } ) ; } catch ( e ) { logger . debug ( `Error scheduling task, received ${ e . message }` ) ; } } function registerAlertingHealthCheckTask ( logger , taskManager , coreStartServices ) { taskManager . registerTaskDefinitions ( { [ HEALTH_TASK_TYPE ] : { title : 'Alerting framework health check task' , createTaskRunner : healthCheckTaskRunner ( logger , coreStartServices ) , } , } ) ; } export function healthCheckTaskRunner ( logger , coreStartServices ) { return ( { taskInstance } ) => { const { state } = taskInstance ; return { async run ( ) { try { return await getAlertingHealthStatus ( ( await coreStartServices ) [ 0 ] . savedObjects , state . runs ) ; } catch ( errMsg ) { logger . warn ( `Error executing alerting health check task: ${ errMsg }` ) ; return { state : { runs : ( state . runs || 0 ) + 1 , health_status : HealthStatus . Error , } , } ; } } , } ; } ; }