Skip to content

Commit

Permalink
feat(provider): implement graceful shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
jlenon7 committed Oct 27, 2022
1 parent 9c00687 commit b218e8a
Show file tree
Hide file tree
Showing 11 changed files with 124 additions and 127 deletions.
157 changes: 55 additions & 102 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "1.2.4",
"version": "1.2.5",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <lenon@athenna.io>",
Expand Down Expand Up @@ -40,11 +40,11 @@
"#tests/*": "./tests/*.js"
},
"dependencies": {
"@athenna/artisan": "1.5.6",
"@athenna/artisan": "1.5.8",
"@athenna/common": "1.0.0",
"@athenna/config": "1.1.8",
"@athenna/ioc": "1.2.7",
"@athenna/logger": "1.3.4",
"@athenna/config": "1.1.9",
"@athenna/ioc": "1.2.8",
"@athenna/logger": "1.3.6",
"@faker-js/faker": "7.4.0",
"knex": "2.3.0"
},
Expand Down Expand Up @@ -135,6 +135,10 @@
"es2021": true,
"node": true
},
"globals": {
"Env": true,
"ioc": true
},
"plugins": [
"prettier"
],
Expand Down
18 changes: 17 additions & 1 deletion src/Factories/DriverFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export class DriverFactory {

return client
} catch (error) {
console.log(error)
throw new ConnectionFailedException(conName, driverName, error)
}
}
Expand Down Expand Up @@ -161,6 +162,21 @@ export class DriverFactory {
}
}

/**
* Close all opened connections of DriverFactory.
*
* @return {Promise<void>}
*/
static async closeAllConnections() {
const availableDrivers = this.availableDrivers(true)

const promises = availableDrivers.map(
this.closeConnectionByDriver.bind(this),
)

await Promise.all(promises)
}

/**
* Create the connection with database by connection name.
*
Expand Down Expand Up @@ -236,7 +252,7 @@ export class DriverFactory {
static getLogger() {
const logger = new Logger()

return process.env.NODE_ENV === 'test' || process.env.BOOT_LOGS === 'false'
return Env('NODE_ENV') === 'test' || !Env('BOOT_LOGS')
? logger.channel('discard')
: logger.channel('application')
}
Expand Down
33 changes: 19 additions & 14 deletions src/Providers/DatabaseProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import { ServiceProvider } from '@athenna/ioc'

import { Database, DatabaseImpl } from '#src/index'
import { DatabaseImpl } from '#src/index'

export class DatabaseProvider extends ServiceProvider {
/**
Expand All @@ -18,22 +18,27 @@ export class DatabaseProvider extends ServiceProvider {
* @return {Promise<void>}
*/
async boot() {
this.container.bind('Athenna/Core/Database', DatabaseImpl)
const Database = this.container
.bind('Athenna/Core/Database', DatabaseImpl)
.use('Athenna/Core/Database')

const isToAutoConnect =
process.env.DB_AUTO_CONNECT &&
(process.env.DB_AUTO_CONNECT === true ||
process.env.DB_AUTO_CONNECT === 'true' ||
process.env.DB_AUTO_CONNECT === '(true)')
if (Env('DB_AUTO_CONNECT', true) && !Env('IS_ARTISAN', true)) {
await Database.connect()
}
}

const isArtisanApp =
process.env.IS_ARTISAN &&
(process.env.IS_ARTISAN === true ||
process.env.IS_ARTISAN === 'true' ||
process.env.IS_ARTISAN === '(true)')
/**
* Shutdown any application services.
*
* @return {void|Promise<void>}
*/
async shutdown() {
const Database = this.container.use('Athenna/Core/Database')

if (isToAutoConnect && !isArtisanApp) {
await Database.connect()
if (!Database) {
return
}

await Database.closeAll()
}
}
Loading

0 comments on commit b218e8a

Please sign in to comment.