Skip to content

Commit

Permalink
Proper handling of SIGINT & SIGTERM, optimized package.json scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
janik6n committed Dec 25, 2024
1 parent fff5b41 commit d748ca9
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 4 deletions.
22 changes: 22 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,28 @@ All notable changes to this template will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [1.1.1] - 2024-12-25

### Added

- Handle `SIGINT` and `SIGTERM` signals properly.

### Changed

### Deprecated

### Fixed

- Docker container command-line arguments handling.

### Removed

### Security

### Internal

- Use variable references in ´package.json´ instead of hard-coded values.

## [1.1.0] - 2024-12-23

### Added
Expand Down
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ COPY --chown=node:node --from=builder /app/dist .
# Set environment variable as an example
ENV CONTAINERIZED=true
# Run the app
CMD ["sh", "-c", "dumb-init node index.mjs"]
ENTRYPOINT ["dumb-init", "node", "index.mjs"]
CMD []
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "typescript-starter",
"description": "TypeScript Starter",
"version": "1.1.0",
"version": "1.1.1",
"homepage": "https://github.com/janik6n/typescript-starter#readme",
"repository": {
"type": "git",
Expand All @@ -22,7 +22,7 @@
"scripts": {
"build": "rimraf ./dist && npx tsc --noEmit && node build.js",
"build:tsc": "rimraf ./dist && tsc",
"build:container": "docker build -t typescript-starter:1.1.0 .",
"build:container": "docker build -t $npm_package_name:$npm_package_version .",
"dev": "tsx src/index.ts",
"dev:watch": "tsx watch src/index.ts",
"format": "prettier . --write",
Expand All @@ -35,7 +35,7 @@
"test:watch": "vitest --coverage.enabled=false --project='unit-integration'",
"test:ci": "vitest run --coverage.enabled=true --project='unit-integration'",
"start": "node ./dist/index.mjs",
"start:container": "docker run -it --rm typescript-starter:1.1.0"
"start:container": "docker run -it --rm $npm_package_name:$npm_package_version"
},
"engines": {
"node": ">=22.0.0"
Expand Down
30 changes: 30 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import process from 'node:process';

import type { Config } from './models/config-model.js';
import { validateConfig } from './utils/utils.js';

// Handle SIGINT & SIGTERM signals
const cleanup = () => {
console.log('\nPerforming cleanup...');
// Add cleanup logic here
process.exit(0);
};

// Handle signals
process.on('SIGINT', () => {
console.log('\nReceived SIGINT (Ctrl+C)');
cleanup();
});

process.on('SIGTERM', () => {
console.log('\nReceived SIGTERM');
cleanup();
});

const serverConfig: Config = {
id: `srv-1`,
description: `Config for Server-1`,
Expand All @@ -10,12 +30,22 @@ const serverConfig: Config = {
(async function () {
console.log(`Begin processing.`);

// As an example (see Dockerfile) when the app is running in containerized environment, the CONTAINERIZED env variable is set to true
console.log(`ENV CONTAINERIZED: ${process.env.CONTAINERIZED}`);

const validationResult = await validateConfig(serverConfig);
if (validationResult) {
console.log(`Server config is valid!`);
} else {
console.log(`Server config is not valid!`);
}

// Artificial delay so you have time to test signals.
await new Promise<void>((resolve) => {
setTimeout(() => {
resolve();
}, 5000);
});

console.log(`Processing ready.`);
})();

0 comments on commit d748ca9

Please sign in to comment.