import { inject , injectable } from 'inversify' ; import * as apid from '../../../../api' ; import IRuleDB from '../../db/IRuleDB' ; import IRuleEvent from '../../event/IRuleEvent' ; import ILogger from '../../ILogger' ; import ILoggerModel from '../../ILoggerModel' ; import IReserveOptionChecker from '../IReserveOptionChecker' ; import IRuleManageModel from './IRuleManageModel' ; @ injectable ( ) export default class RuleManageModel implements IRuleManageModel { private isRunning : boolean = false ; private lockTimer : NodeJS . Timeout | undefined ; private log : ILogger ; private optionChecker : IReserveOptionChecker ; private ruleDB : IRuleDB ; private ruleEvent : IRuleEvent ; constructor ( @ inject ( 'ILoggerModel' ) logger , @ inject ( 'IReserveOptionChecker' ) optionChecker , @ inject ( 'IRuleDB' ) ruleDB , @ inject ( 'IRuleEvent' ) ruleEvent ) { this . log = logger . getLogger ( ) ; this . optionChecker = optionChecker ; this . ruleDB = ruleDB ; this . ruleEvent = ruleEvent ; } param rule return public async add ( rule ) { this . lockExecution ( ) ; this . log . system . info ( 'add rule' ) ; if ( this . optionChecker . checkRuleOption ( rule ) === false ) { this . unlockExecution ( ) ; this . log . system . error ( 'failed to add rule' ) ; throw new Error ( 'AddRuleError' ) ; } let ruleId ! ; try { ruleId = await this . ruleDB . insertOnce ( rule ) ; } catch ( err ) { this . unlockExecution ( ) ; this . log . system . error ( 'insert rule error' ) ; this . log . system . error ( err ) ; } this . unlockExecution ( ) ; this . log . system . info ( `rule added successfully: ${ ruleId }` ) ; this . ruleEvent . emitAdded ( ruleId ) ; return ruleId ; } param rule public async update ( rule ) { this . lockExecution ( ) ; const oldRule = await this . ruleDB . findId ( rule . id ) . catch ( err => { this . unlockExecution ( ) ; this . log . system . error ( err ) ; throw err ; } ) ; if ( oldRule === null ) { this . unlockExecution ( ) ; throw new Error ( 'RuleIsNotFound' ) ; } this . log . system . info ( `update rule: ${ rule . id }` ) ; if ( this . optionChecker . checkRuleOption ( rule ) === false ) { this . unlockExecution ( ) ; this . log . system . error ( 'failed to update rule' ) ; throw new Error ( 'UpdateRuleError' ) ; } try { await this . ruleDB . updateOnce ( rule ) ; } catch ( err ) { this . unlockExecution ( ) ; this . log . system . error ( `update rule error: ${ rule . id }` ) ; throw err ; } this . unlockExecution ( ) ; this . log . system . info ( `rule updated successfully: ${ rule . id }` ) ; this . ruleEvent . emitUpdated ( rule . id ) ; } param ruleId public async enable ( ruleId ) { this . lockExecution ( ) ; this . log . system . info ( `enable rule: ${ ruleId }` ) ; try { await this . ruleDB . enableOnce ( ruleId ) ; } catch ( err ) { this . unlockExecution ( ) ; this . log . system . error ( `enable rule error: ${ ruleId }` ) ; throw err ; } this . unlockExecution ( ) ; this . log . system . info ( `rule enabled successfully: ${ ruleId }` ) ; this . ruleEvent . emitEnabled ( ruleId ) ; } param ruleId public async disable ( ruleId ) { this . lockExecution ( ) ; this . log . system . info ( `disable rule: ${ ruleId }` ) ; try { await this . ruleDB . disableOnce ( ruleId ) ; } catch ( err ) { this . unlockExecution ( ) ; this . log . system . error ( `disable rule error: ${ ruleId }` ) ; throw err ; } this . unlockExecution ( ) ; this . log . system . info ( `rule disabled successfully: ${ ruleId }` ) ; this . ruleEvent . emitDisabled ( ruleId ) ; } param ruleId public async delete ( ruleId ) { this . lockExecution ( ) ; this . log . system . info ( `delete rule: ${ ruleId }` ) ; try { await this . ruleDB . deleteOnce ( ruleId ) ; } catch ( err ) { this . unlockExecution ( ) ; this . log . system . error ( `delete rule error: ${ ruleId }` ) ; throw err ; } this . unlockExecution ( ) ; this . log . system . info ( `rule deleted successfully: ${ ruleId }` ) ; this . ruleEvent . emitDeleted ( ruleId ) ; } param ruleIds return public async deletes ( ruleIds ) { const failedIds = [ ] ; this . log . system . info ( 'deletes rule' ) ; for ( const ruleId of ruleIds ) { try { await this . delete ( ruleId ) ; } catch ( err ) { failedIds . push ( ruleId ) ; } } return failedIds ; } /**
     * 実行権をロックする
     */ private lockExecution ( ) { if ( this . isRunning === true ) { throw new Error ( 'RuleManageModelIsRunning' ) ; } this . isRunning = false ; this . lockTimer = setTimeout ( ( ) => { this . unlockExecution ( ) ; } , 1000 * 10 ) ; } /**
     * 実行権の開放
     */ private unlockExecution ( ) { this . isRunning = false ; clearTimeout ( < any > this . lockTimer ) ; } }