Skip to content

Commit

Permalink
refactor: cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
thetutlage committed Nov 20, 2019
1 parent 6678a7b commit 0a2e74c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 19 deletions.
40 changes: 26 additions & 14 deletions src/Ignitor/Ace/AppCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ export class AppCommands {
*/
private _wired = false

/**
* Signals listener to listen for exit signals and kill command
*/
private _signalsListener = new SignalsListener()

/**
* Source root always points to the compiled source
* code.
Expand All @@ -34,6 +39,25 @@ export class AppCommands {
) {
}

/**
* Hooks into kernel lifecycle events to conditionally
* load the app.
*/
private _addKernelHooks (kernel: ace.Kernel) {
kernel.before('find', async (command) => {
if (command && command.settings.loadApp) {
await this._wire()
this._bootstrapper.application.isReady = true
}
})

kernel.before('run', async () => {
if (this._wired) {
await this._bootstrapper.executeReadyHooks()
}
})
}

/**
* Boot the application.
*/
Expand Down Expand Up @@ -61,24 +85,12 @@ export class AppCommands {

const manifest = new this._ace.Manifest(this._sourceRoot)
const kernel = new this._ace.Kernel(this._bootstrapper.application)

kernel.before('find', async (command) => {
if (command && command.settings.loadApp) {
await this._wire()
this._bootstrapper.application.isReady = true
}
})

kernel.before('run', async () => {
if (this._wired) {
await this._bootstrapper.executeReadyHooks()
}
})
this._addKernelHooks(kernel)

kernel.useManifest(manifest)
await kernel.handle(argv)

new SignalsListener().listen(async () => {
this._signalsListener.listen(async () => {
if (this._wired) {
await this._bootstrapper.executeShutdownHooks()
}
Expand Down
2 changes: 0 additions & 2 deletions src/Ignitor/Ace/GenerateManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

import ace from '@adonisjs/ace'
import { Bootstrapper } from '../Bootstrapper'
import { exitProcess } from '../../utils'

/**
* Exposes the API to generate the manifest file
Expand Down Expand Up @@ -52,6 +51,5 @@ export class GenerateManifest {
* Success
*/
this._ace.logger.create('.adonisrc.json')
exitProcess(0)
}
}
2 changes: 2 additions & 0 deletions src/Ignitor/Bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ export class Bootstrapper {
* Executes the ready hooks on the providers
*/
public async executeReadyHooks () {
this._logger!.trace('executing ready hooks')
await Promise.all(this._providersWithReadyHook.map((provider) => provider.ready()))
this._providersWithReadyHook = []
}
Expand All @@ -193,6 +194,7 @@ export class Bootstrapper {
* Executes the ready hooks on the providers
*/
public async executeShutdownHooks () {
this._logger!.trace('executing shutdown hooks')
await Promise.all(this._providersWithShutdownHook.map((provider) => provider.shutdown()))
this._providersWithShutdownHook = []
}
Expand Down
6 changes: 3 additions & 3 deletions src/Ignitor/HttpServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class HttpServer {
this._monitorHttpServer()
this._signalsListener.listen(() => this.close())
} catch (error) {
new ErrorHandler(this.application).handleError(error).finally(() => process.exit(1))
new ErrorHandler(this.application).handleError(error)
}
}

Expand All @@ -219,8 +219,6 @@ export class HttpServer {
* we are not accepting any new request during cool off.
*/
await this._closeHttpServer()

this._logger.trace('preparing server shutdown')
await this._bootstrapper.executeShutdownHooks()
}

Expand All @@ -230,6 +228,8 @@ export class HttpServer {
* seconds.
*/
public async kill (waitTimeout: number = 3000) {
this._logger.trace('forcefully killing http server')

try {
await Promise.race([this.close(), new Promise((resolve) => {
setTimeout(resolve, waitTimeout)
Expand Down

0 comments on commit 0a2e74c

Please sign in to comment.