import type { Client } from '@elastic/elasticsearch' ; import { SerializedConcreteTaskInstance } from '../../../../plugins/task_manager/server/task' ; export interface TaskManagerDoc { type : string ; task : SerializedConcreteTaskInstance ; } export class TaskManagerUtils { private readonly es : Client ; private readonly retry : any ; constructor ( es , retry ) { this . es = es ; this . retry = retry ; } async waitForEmpty ( taskRunAtFilter ) { return await this . retry . try ( async ( ) => { const searchResult = await this . es . search ( { index : '.kibana_task_manager' , body : { query : { bool : { must : [ { terms : { 'task.scope' : [ 'actions' , 'alerting' ] , } , } , { range : { 'task.scheduledAt' : { gte : taskRunAtFilter . getTime ( ) . toString ( ) , } , } , } , ] , } , } , } , } ) ; if ( searchResult . hits . total . value ) { throw new Error ( `Expected 0 tasks but received ${ searchResult . hits . total . value }` ) ; } } ) ; } async waitForAllTasksIdle ( taskRunAtFilter ) { return await this . retry . try ( async ( ) => { const searchResult = await this . es . search ( { index : '.kibana_task_manager' , body : { query : { bool : { must : [ { terms : { 'task.scope' : [ 'actions' , 'alerting' ] , } , } , { range : { 'task.scheduledAt' : { gte : taskRunAtFilter . getTime ( ) . toString ( ) , } , } , } , ] , must_not : [ { term : { 'task.status' : 'idle' , } , } , ] , } , } , } , } ) ; if ( searchResult . hits . total . value ) { throw new Error ( `Expected 0 non-idle tasks but received ${ searchResult . hits . total . value }` ) ; } } ) ; } async waitForActionTaskParamsToBeCleanedUp ( createdAtFilter ) { return await this . retry . try ( async ( ) => { const searchResult = await this . es . search ( { index : '.kibana' , body : { query : { bool : { must : [ { term : { type : 'action_task_params' , } , } , { range : { updated_at : { gte : createdAtFilter . getTime ( ) . toString ( ) , } , } , } , ] , } , } , } , } ) ; if ( searchResult . hits . total . value ) { throw new Error ( `Expected 0 action_task_params objects but received ${ searchResult . hits . total . value }` ) ; } } ) ; } }