2020-07-21 03:54:25 +00:00
|
|
|
import {Service} from '../../../di/src/decorator/Service.ts'
|
|
|
|
import {Logging} from '../service/logging/Logging.ts'
|
|
|
|
import LifecycleUnit from './Unit.ts'
|
|
|
|
import {container, make} from '../../../di/src/global.ts'
|
|
|
|
import {DependencyKey} from '../../../di/src/type/DependencyKey.ts'
|
2020-07-06 14:53:03 +00:00
|
|
|
import RunLevelErrorHandler from '../error/RunLevelErrorHandler.ts'
|
2020-07-21 03:54:25 +00:00
|
|
|
import {Status} from '../const/status.ts'
|
|
|
|
import Instantiable from "../../../di/src/type/Instantiable.ts";
|
|
|
|
import {Collection} from "../collection/Collection.ts";
|
2020-07-06 14:53:03 +00:00
|
|
|
|
|
|
|
@Service()
|
|
|
|
export default class Application {
|
2020-07-21 03:54:25 +00:00
|
|
|
protected instantiated_units: Collection<LifecycleUnit> = new Collection<LifecycleUnit>()
|
|
|
|
|
2020-07-06 14:53:03 +00:00
|
|
|
constructor(
|
|
|
|
protected logger: Logging,
|
|
|
|
protected rleh: RunLevelErrorHandler,
|
2020-07-21 03:54:25 +00:00
|
|
|
protected units: (Instantiable<LifecycleUnit>)[],
|
2020-07-06 14:53:03 +00:00
|
|
|
) {}
|
|
|
|
|
|
|
|
make(token: DependencyKey) {
|
|
|
|
return make(token)
|
|
|
|
}
|
|
|
|
|
|
|
|
container() {
|
|
|
|
return container
|
|
|
|
}
|
|
|
|
|
|
|
|
async up() {
|
2020-07-21 03:54:25 +00:00
|
|
|
this.logger.info('Starting Daton...', true)
|
|
|
|
for ( const unit_class of this.units ) {
|
|
|
|
const unit = this.make(unit_class)
|
|
|
|
this.instantiated_units.push(unit)
|
|
|
|
await this.start_unit(unit)
|
|
|
|
}
|
2020-07-06 14:53:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async down() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async run() {
|
|
|
|
try {
|
2020-07-21 03:54:25 +00:00
|
|
|
await this.up()
|
|
|
|
await this.down()
|
2020-07-06 14:53:03 +00:00
|
|
|
} catch (e) {
|
|
|
|
await this.app_error(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async app_error(e: Error) {
|
|
|
|
this.rleh.handle(e)
|
|
|
|
}
|
2020-07-21 03:54:25 +00:00
|
|
|
|
|
|
|
protected async start_unit(unit: LifecycleUnit) {
|
|
|
|
try {
|
|
|
|
unit.status = Status.Starting
|
|
|
|
this.logger.info(`Starting ${unit.constructor.name}...`)
|
|
|
|
await unit.up()
|
|
|
|
this.logger.verbose(`Successfully started ${unit.constructor.name}`)
|
|
|
|
unit.status = Status.Running
|
|
|
|
} catch (e) {
|
|
|
|
unit.status = Status.Error
|
|
|
|
this.logger.error(`Error encountered while starting ${unit.constructor.name}. Will attempt to proceed.`)
|
|
|
|
this.logger.debug(e.message)
|
|
|
|
this.logger.verbose(e)
|
|
|
|
}
|
|
|
|
}
|
2020-07-06 14:53:03 +00:00
|
|
|
}
|