Skip to content

Commit

Permalink
Merge pull request #328 from LeChatErrant/prisma-engine-cleaning
Browse files Browse the repository at this point in the history
Closing external services connection
  • Loading branch information
LeChatErrant committed Apr 8, 2021
2 parents b8c9bf5 + 8f8e03d commit 87ac65c
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 38 deletions.
1 change: 0 additions & 1 deletion .husky/.gitignore

This file was deleted.

4 changes: 0 additions & 4 deletions .husky/pre-commit

This file was deleted.

20 changes: 0 additions & 20 deletions package-lock.json

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

8 changes: 3 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
"build": "tsc",
"start": "npm run db:deploy && node dist",
"test": "MODE=test && exit 0",
"integration": "MODE=test && npm run db:deploy && jest --passWithNoTests --coverage --runInBand --forceExit --silent",
"integration:watch": "MODE=test && npm run db:deploy && jest --passWithNoTests --coverage --runInBand --forceExit --watch --silent",
"integration": "MODE=test && npm run db:deploy && jest --passWithNoTests --coverage --runInBand --silent --detectOpenHandles",
"integration:watch": "MODE=test && npm run db:deploy && jest --passWithNoTests --coverage --runInBand --watch --silent --detectOpenHandles",
"dev:db": "docker run --name db -e POSTGRES_PASSWORD=${DB_PASS} -e POSTGRES_USER=${DB_USER} -p ${DB_PORT}:${DB_PORT} -d postgres:alpine",
"dev:redis": "docker run --name redis -e REDIS_PASSWORD=${REDIS_PASS} -p ${REDIS_PORT}:${REDIS_PORT} -d bitnami/redis:latest",
"dev": "npm run db:deploy && concurrently --kill-others 'prisma generate --watch' 'ts-node-dev --respawn --cls --exit-child src'",
Expand All @@ -24,8 +24,7 @@
"db:push": "prisma db push --preview-feature",
"db:studio": "prisma studio --port ${STUDIO_PORT}",
"lint": "eslint './src/**/*.{ts,tsx}' --fix",
"lint:ci": "eslint './src/**/*.{ts,tsx}'",
"postinstall": "husky install"
"lint:ci": "eslint './src/**/*.{ts,tsx}'"
},
"keywords": [],
"author": "",
Expand Down Expand Up @@ -66,7 +65,6 @@
"eslint": "^7.23.0",
"eslint-config-airbnb-typescript": "^12.3.1",
"eslint-plugin-import": "^2.22.1",
"husky": "^6.0.0",
"jest": "^26.6.3",
"supertest": "^6.1.3",
"templated-project-cli": "^1.2.0",
Expand Down
5 changes: 3 additions & 2 deletions src/components/post/post.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Requester from '../../appRequester';
import db from '../../appDatabase';
import logger from '../../appLogger';
import waitApp from '../../utils/waitApp';
import closeApp from '../../utils/closeApp';

const app = new Requester();

Expand Down Expand Up @@ -33,9 +34,9 @@ beforeAll(async () => {
await waitApp();
});

// Gracefully terminate prisma query engine
// Gracefully terminate external services connections
afterAll(async () => {
await db.$disconnect();
await closeApp();
});

// Reset session before each test, create two user and log in
Expand Down
5 changes: 3 additions & 2 deletions src/components/user/user.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Requester from '../../appRequester';
import db from '../../appDatabase';
import logger from '../../appLogger';
import waitApp from '../../utils/waitApp';
import closeApp from '../../utils/closeApp';
import { config } from '../../appConfig';
import seedAdminUser from '../../utils/seedAdminUser';

Expand All @@ -28,9 +29,9 @@ beforeAll(async () => {
await waitApp();
});

// Gracefully terminate prisma query engine
// Gracefully terminate external services connections
afterAll(async () => {
await db.$disconnect();
await closeApp();
});

// Reset session before each test
Expand Down
4 changes: 0 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import app from './app';
import db from './appDatabase';
import logger from './appLogger';
import { config } from './appConfig';
import waitApp from './utils/waitApp';
Expand All @@ -17,7 +16,4 @@ main()
.catch((error) => {
logger.error(error);
throw error;
})
.finally(async () => {
await db.$disconnect();
});
32 changes: 32 additions & 0 deletions src/utils/closeApp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import db from '../appDatabase';
import { redisClient } from '../appStore';
import logger from '../appLogger';

export default async function closeApp() {
/* Prisma */
logger.info('Disconnecting from database...');
await db
.$disconnect()
.then(() => {
logger.info('Disconnected from database !');
})
.catch((error) => {
logger.error(error);
throw error;
});

/* Redis */
if (redisClient) {
logger.info('Disconnecting from redis...');
await new Promise<void>((resolve, reject) => {
redisClient!.quit((error) => {
if (error) {
reject(error);
} else {
resolve();
}
});
});
logger.info('Disconnected from redis !');
}
}

0 comments on commit 87ac65c

Please sign in to comment.