import { Influx } from '../../../_core/Influx/Influx' ; import { watcherClasses } from '../../../_core/Watcher/exports' ; import { IWatcherConfig , Watcher } from '../../../_core/Watcher/Watcher' ; import { Watchers } from './Watchers' ; import { logger } from '../../../logger' ; export param { Influx } influx returns { Promise < any > } export async function restartWatchers ( influx ) { let instance = null ; try { const watchers = await Watchers . getWatchers ( ) ; for ( const watcher of watchers ) { if ( ! Watchers . runningWatchers . find ( w => w . id === watcher . id ) ) { instance = < Watcher > new ( < any > watcherClasses ) [ watcher . type ] ( { ... watcher } ) ; await instance . init ( ) ; instance . setInflux ( influx ) ; instance . run ( ) . catch ( error => { throw error ; } ) ; Watchers . runningWatchers . push ( instance ) ; } } return '' ; } catch ( error ) { if ( instance && Watchers . runningWatchers . indexOf ( instance ) > - 1 ) { Watchers . runningWatchers . splice ( Watchers . runningWatchers . indexOf ( instance ) , 1 ) ; } logger . error ( error ) ; throw new Error ( 'Error while restarting watchers' ) ; } } export returns { Promise < any > } export async function stopWatchers ( ) { try { Watchers . runningWatchers . forEach ( watcher => watcher . stop ( ) ) ; Watchers . runningWatchers = [ ] ; } catch ( error ) { logger . error ( error ) ; throw new Error ( 'Error while stopping watchers' ) ; } } export param { * } config returns export async function watcherConfigurationExist ( config ) { const watchers = await Watchers . getWatchers ( ) ; const keys = Object . keys ( config ) ; if ( keys . indexOf ( 'extra' ) !== - 1 ) { keys . splice ( keys . indexOf ( 'extra' ) , 1 ) ; } for ( const watcher of watchers ) { let sameKeys = 0 ; keys . forEach ( key => ( watcher [ key ] === config [ key ] ? sameKeys ++ : sameKeys ) ) ; if ( sameKeys === keys . length ) return true ; } return false ; }