Refactor units to be generic; start bundles; start app index.ts

This commit is contained in:
garrettmills
2020-07-20 22:54:25 -05:00
parent c0777f77b5
commit 60bb9afa29
22 changed files with 163 additions and 110 deletions

View File

@@ -1,16 +1,21 @@
import { Service } from '../../../di/src/decorator/Service.ts'
import { Logging } from '../service/logging/Logging.ts'
import Unit from './Unit.ts'
import { container, make } from '../../../di/src/global.ts'
import { DependencyKey } from '../../../di/src/type/DependencyKey.ts'
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'
import RunLevelErrorHandler from '../error/RunLevelErrorHandler.ts'
import {Status} from '../const/status.ts'
import Instantiable from "../../../di/src/type/Instantiable.ts";
import {Collection} from "../collection/Collection.ts";
@Service()
export default class Application {
protected instantiated_units: Collection<LifecycleUnit> = new Collection<LifecycleUnit>()
constructor(
protected logger: Logging,
protected rleh: RunLevelErrorHandler,
protected units: Unit[],
protected units: (Instantiable<LifecycleUnit>)[],
) {}
make(token: DependencyKey) {
@@ -22,7 +27,12 @@ export default class Application {
}
async up() {
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)
}
}
async down() {
@@ -31,7 +41,8 @@ export default class Application {
async run() {
try {
this.logger.info('Starting Daton...')
await this.up()
await this.down()
} catch (e) {
await this.app_error(e)
}
@@ -40,4 +51,19 @@ export default class Application {
async app_error(e: Error) {
this.rleh.handle(e)
}
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)
}
}
}

View File

@@ -1,9 +1,8 @@
import { Status, isStatus } from '../const/status.ts'
import { Unit } from './decorators.ts'
import { Collection } from '../collection/Collection.ts'
import {container, make} from '../../../di/src/global.ts'
import {DependencyKey} from "../../../di/src/type/DependencyKey.ts";
import Instantiable, {isInstantiable} from "../../../di/src/type/Instantiable.ts";
import {DependencyKey} from '../../../di/src/type/DependencyKey.ts'
import Instantiable, {isInstantiable} from '../../../di/src/type/Instantiable.ts'
const isLifecycleUnit = (something: any): something is (typeof LifecycleUnit) => {
return isInstantiable(something) && something.prototype instanceof LifecycleUnit