import * as child_process from 'child_process' ; import * as path from 'path' ; import 'reflect-metadata' ; import { install } from 'source-map-support' ; import IEPGUpdateExecutorManageModel from './model/epgUpdater/IEPGUpdateExecutorManageModel' ; import IEventSetter from './model/event/IEventSetter' ; import IConfiguration from './model/IConfiguration' ; import IConnectionCheckModel from './model/IConnectionCheckModel' ; import ILoggerModel from './model/ILoggerModel' ; import IMirakurunClientModel from './model/IMirakurunClientModel' ; import IIPCServer from './model/ipc/IIPCServer' ; import container from './model/ModelContainer' ; import * as containerSetter from './model/ModelContainerSetter' ; import IRecordingManageModel from './model/operator/recording/IRecordingManageModel' ; import IReservationManageModel from './model/operator/reservation/IReservationManageModel' ; import IStorageManageModel from './model/operator/storage/IStorageManageModel' ; install ( ) ; containerSetter . set ( container ) ; /**
 * 初期処理
 */ const init = async ( ) => { const logger = container . get < ILoggerModel > ( 'ILoggerModel' ) ; logger . initialize ( ) ; const log = logger . getLogger ( ) ; process . on ( 'uncaughtException' , err => { log . system . fatal ( `uncaughtException: ${ err . message }` ) ; log . system . fatal ( err ) ; } ) ; process . on ( 'unhandledRejection' , err => { log . system . fatal ( 'unhandledRejection' ) ; log . system . fatal ( err ) ; } ) ; const config = container . get < IConfiguration > ( 'IConfiguration' ) . getConfig ( ) ; if ( process . platform !== 'win32' && process . getuid ( ) === 0 ) { if ( typeof config . gid === 'string' || typeof config . gid === 'number' ) { process . setgid ( config . gid ) ; } else { process . setgid ( 'video' ) ; } if ( typeof config . uid === 'string' || typeof config . uid === 'number' ) { process . setuid ( config . uid ) ; } } logger . initialize ( path . join ( __dirname , '..' , 'config' , 'operatorLogConfig.yml' ) ) ; const connectionChecker = container . get < IConnectionCheckModel > ( 'IConnectionCheckModel' ) ; await connectionChecker . checkMirakurun ( ) ; await connectionChecker . checkDB ( ) ; } ; /**
 * Operator 機能起動処理
 */ const runOperator = async ( ) => { const client = container . get < IMirakurunClientModel > ( 'IMirakurunClientModel' ) . getClient ( ) ; const eventSetter = container . get < IEventSetter > ( 'IEventSetter' ) ; eventSetter . set ( ) ; const reservationManageModel = container . get < IReservationManageModel > ( 'IReservationManageModel' ) ; const recordingManager = container . get < IRecordingManageModel > ( 'IRecordingManageModel' ) ; const tuners = await client . getTuners ( ) ; reservationManageModel . setTuners ( tuners ) ; recordingManager . setTuner ( tuners ) ; const storageManageModel = container . get < IStorageManageModel > ( 'IStorageManageModel' ) ; storageManageModel . start ( ) ; } ; /**
 * Service 起動処理
 */ const runService = async ( ) => { const child = child_process . spawn ( process . argv [ 0 ] , [ path . join ( __dirname , 'model' , 'service' , 'ServiceExecutor.js' ) ] , { stdio : [ 'ignore' , 'ignore' , 'ignore' , 'ipc' ] , } ) ; const log = container . get < ILoggerModel > ( 'ILoggerModel' ) . getLogger ( ) ; child . once ( 'exit' , ( ) => { log . system . fatal ( 'service process is down' ) ; log . system . fatal ( 'restart service' ) ; runService ( ) ; } ) ; child . once ( 'error' , ( ) => { runService ( ) ; } ) ; if ( child . stdout !== null ) { child . stdout . on ( 'data' , ( ) => { } ) ; } if ( child . stderr !== null ) { child . stderr . on ( 'data' , ( ) => { } ) ; } const ipcServer = container . get < IIPCServer > ( 'IIPCServer' ) ; ipcServer . register ( child ) ; log . system . info ( `start service pid: ${ child . pid }` ) ; } ; /**
 * クリーンアップ処理
 */ const cleanup = async ( ) => { const reservationManageModel = container . get < IReservationManageModel > ( 'IReservationManageModel' ) ; const recordingManager = container . get < IRecordingManageModel > ( 'IRecordingManageModel' ) ; await recordingManager . cleanup ( ) ; await reservationManageModel . cleanup ( ) ; } ; /**
 * EPGUpdater 起動処理
 */ const runEPGUpdater = async ( ) => { const epgUpdateExecutorManageModel = container . get < IEPGUpdateExecutorManageModel > ( 'IEPGUpdateExecutorManageModel' ) ; epgUpdateExecutorManageModel . execute ( ) ; } ; ( async ( ) => { try { await init ( ) ; } catch ( err ) { console . error ( 'initialize error' ) ; console . error ( err ) ; process . exit ( 1 ) ; } await runOperator ( ) ; await runService ( ) ; await cleanup ( ) ; await runEPGUpdater ( ) ; } ) ( ) ;