Start basic CLI framework

This commit is contained in:
2020-09-16 10:28:38 -05:00
parent 4dc1f6d7c8
commit d57053703a
10 changed files with 179 additions and 4 deletions

View File

@@ -0,0 +1,4 @@
export default class ErrorWithContext extends Error {
}

View File

@@ -68,7 +68,16 @@ export default class Application {
* @return Promise<void>
*/
async down() {
this.logger.info('Stopping Daton...', true)
for ( const unit of this.instantiated_units ) {
if ( !unit ) continue
await this.stop_unit(unit)
}
setTimeout(() => {
this.logger.warn(`Force exiting...`)
Deno.exit()
}, 2000)
}
/**
@@ -106,8 +115,25 @@ export default class Application {
} 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)
this.logger.debug(e)
}
}
/**
* Shut down the given lifecycle unit.
* @param {LifecycleUnit} unit
*/
protected async stop_unit(unit: LifecycleUnit) {
try {
unit.status = Status.Stopping
this.logger.info(`Stopping ${unit.constructor.name}...`)
await unit.down()
this.logger.verbose(`Successfully stopped ${unit.constructor.name}`)
unit.status = Status.Stopped
} catch (e) {
unit.status = Status.Error
this.logger.error(`Error encountered while stopping ${unit.constructor.name}. Will attempt to proceed.`)
this.logger.debug(e)
}
}

View File

@@ -31,6 +31,11 @@ export default class HttpServer extends LifecycleUnit {
const request_timeout: number = this.config.get('server.request_timeout', 15000)
Deno.signal(Deno.Signal.SIGINT).then(() => {
this.logger.info('Closing server...', true)
this._server.close()
})
for await ( const native_request of this._server ) {
let req: Request = this.make(Request, native_request)