feat: fix exit hook and cleanup docker scripts (#758)

* feat: cleanup docker scripts

* feat: make logging directory if non-existent to fix stop behavior
This commit is contained in:
Eli Bosley
2024-01-03 12:12:37 -05:00
committed by GitHub
parent 3e95bb259f
commit 33c69bf76f
8 changed files with 38 additions and 21 deletions

View File

@@ -15,8 +15,6 @@ RUN apt-get update -y && apt-get install -y \
git \
build-essential
RUN mkdir /var/log/unraid-api/
WORKDIR /app
# Set app env

View File

@@ -1,5 +1,5 @@
[api]
version="3.2.3+f36c72f5"
version="3.2.3+dcb6def1"
extraOrigins=""
[local]
[notifier]

View File

@@ -1,5 +1,5 @@
[api]
version="3.2.3+f36c72f5"
version="3.2.3+dcb6def1"
extraOrigins=""
[local]
[notifier]

View File

@@ -45,12 +45,12 @@
"start:plugin-verbose": "LOG_CONTEXT=true LOG_MOTHERSHIP_MESSAGES=true LOG_TYPE=pretty LOG_LEVEL=trace unraid-api start --debug",
"start:dev": "LOG_MOTHERSHIP_MESSAGES=true LOG_TYPE=pretty NODE_ENV=development LOG_LEVEL=trace NODE_ENV=development tsup --config ./tsup.config.ts --watch --onSuccess 'DOTENV_CONFIG_PATH=./.env.development node -r dotenv/config dist/unraid-api.cjs start --debug'",
"restart:dev": "LOG_MOTHERSHIP_MESSAGES=true LOG_TYPE=pretty NODE_ENV=development LOG_LEVEL=trace NODE_ENV=development tsup --config ./tsup.config.ts --watch --onSuccess 'DOTENV_CONFIG_PATH=./.env.development node -r dotenv/config dist/unraid-api.cjs restart --debug'",
"stop:dev": "LOG_MOTHERSHIP_MESSAGES=true LOG_TYPE=pretty NODE_ENV=development LOG_LEVEL=trace NODE_ENV=development tsup --config ./tsup.config.ts --watch --onSuccess 'DOTENV_CONFIG_PATH=./.env.development node -r dotenv/config dist/unraid-api.cjs stop --debug'",
"stop:dev": "LOG_MOTHERSHIP_MESSAGES=true LOG_TYPE=pretty NODE_ENV=development LOG_LEVEL=trace NODE_ENV=development tsup --config ./tsup.config.ts --onSuccess 'DOTENV_CONFIG_PATH=./.env.development node -r dotenv/config dist/unraid-api.cjs stop --debug'",
"start:report": "LOG_MOTHERSHIP_MESSAGES=true LOG_TYPE=pretty NODE_ENV=development LOG_LEVEL=trace NODE_ENV=development LOG_CONTEXT=true tsup --config ./tsup.config.ts --watch --onSuccess 'DOTENV_CONFIG_PATH=./.env.development node -r dotenv/config dist/unraid-api.cjs report --debug'",
"start:docker": "docker compose run --rm builder-interactive",
"build:dev": "GIT_SHA=$(git rev-parse --short HEAD) IS_TAGGED=$(git describe --tags --abbrev=0 --exact-match || echo '') docker-compose build dev",
"docker:dev": "GIT_SHA=$(git rev-parse --short HEAD) IS_TAGGED=$(git describe --tags --abbrev=0 --exact-match || echo '') docker-compose run --rm --service-ports dev",
"docker:test": "GIT_SHA=$(git rev-parse --short HEAD) IS_TAGGED=$(git describe --tags --abbrev=0 --exact-match || echo '') docker-compose run --rm builder npm run test"
"build:dev": "./scripts/dc.sh build dev",
"docker:dev": "./scripts/dc.sh run --rm --service-ports dev",
"docker:test": "./scripts/dc.sh run --rm builder npm run test"
},
"files": [
".env.staging",

4
api/scripts/dc.sh Executable file
View File

@@ -0,0 +1,4 @@
#!/bin/sh
# Pass all entered params after the docker-compose call
GIT_SHA=$(git rev-parse --short HEAD) IS_TAGGED=$(git describe --tags --abbrev=0 --exact-match || echo '') docker-compose -f docker-compose.yml "$@"

View File

@@ -2,6 +2,20 @@ import { pino } from 'pino';
import { LOG_TRANSPORT, LOG_TYPE } from '@app/environment';
import pretty from 'pino-pretty';
import { existsSync, mkdirSync } from 'node:fs';
import { getters } from '@app/store/index';
import { join } from 'node:path';
const makeLoggingDirectoryIfNotExists = () => {
if (!existsSync(getters.paths()['log-base'])) {
console.log('Creating logging directory');
mkdirSync(getters.paths()['log-base']);
}
};
if (LOG_TRANSPORT === 'file') {
makeLoggingDirectoryIfNotExists();
}
export const levels = [
'trace',
@@ -20,9 +34,9 @@ const level =
] ?? 'info';
export const logDestination = pino.destination({
dest: LOG_TRANSPORT === 'file' ? '/var/log/unraid-api/stdout.log' : 1,
dest: LOG_TRANSPORT === 'file' ? join(getters.paths()['log-base'], 'stdout.log') : 1,
minLength: 1_024,
sync: false
sync: false,
});
const stream =

View File

@@ -38,6 +38,15 @@ const unlinkUnixPort = () => {
// Boot app
void am(
async () => {
exitHook(() => {
server?.close?.();
// If port is unix socket, delete socket before exiting
unlinkUnixPort();
shutdownApiEvent();
process.exit(0);
});
environment.IS_MAIN_PROCESS = true;
logger.debug('ENV %o', env);
@@ -94,15 +103,7 @@ void am(
await validateApiKeyIfPresent();
// On process exit stop HTTP server - this says it supports async but it doesnt seem to
exitHook(() => {
server?.close?.();
// If port is unix socket, delete socket before exiting
unlinkUnixPort();
shutdownApiEvent();
process.exit(0);
});
},
async (error: NodeJS.ErrnoException) => {
logger.error('API-GLOBAL-ERROR %s %s', error.message, error.stack);

View File

@@ -18,7 +18,7 @@ export const shutdownApiEvent = () => {
logger.debug('Writing final configs');
writeConfigSync('flash');
writeConfigSync('memory');
logger.debug('Shutting down log destination');
logDestination.flushSync();
logDestination.destroy();
logDestination.destroy();
};