Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example dir, update package.json #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions example/cluster.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import app from './express';
import * as cluster from 'cluster';
import {GracefulShutdownManager} from '../src/index';

const PORT_NUMBER = 8080;

if (cluster.isMaster) {
let aliveForks = 2;
cluster.fork();
cluster.fork();
cluster.on('exit', function (worker, exitCode) {
console.log('Worker %d died :(, exitCode is %d', worker.id, exitCode);
// if all workers died, then cluster is exiting
if (!--aliveForks) {
process.exit()
}
});
// cluster should wait
process.on('SIGTERM', () => console.log('master ignore SIGTERM'));
process.on('SIGINT', () => console.log('master ignore SIGINT'));
} else {
// Configure forks as usual, but exit on terminating
const server = app.listen(PORT_NUMBER, (error: any) => {
if (!error) {
console.log('Started Express server on port: %d', PORT_NUMBER);
}
});

const shutdownManager = new GracefulShutdownManager(server);

process.on('SIGTERM', () => onProcessInterrupt('SIGTERM'));
process.on('SIGINT', () => onProcessInterrupt('SIGINT'));


function onProcessInterrupt (signal: string) {
console.log('Termination signal is received from OS (' + signal + '), the application will terminate');
//noinspection JSIgnoredPromiseFromCall
shutdownManager.terminate(() => {
console.log('Server is terminated');
// This is close the fork
process.exit(0)
});
}
}
33 changes: 33 additions & 0 deletions example/express.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

import * as express from 'express';
import {Request, Response, Application} from 'express';


const app: Application = express();

app.get('/quick', (req: Request, res: Response): any => {
res.send('quick response');
});

app.get('/slow', (req: Request, res: Response): any => {
setTimeout(() => res.send('slow response'), 3000);
});

app.get('/progress', (req: Request, res: Response): any => {
const socket = res.connection;
socket.setNoDelay(true);
res.writeHead(200, {
'Content-Type': 'text/html',
// 'Content-Type': 'application/octet-stream',
'Transfer-Encoding': 'chunked'
});
res.write(`buffer: ${socket.bufferSize} <br>\n`);
const time = 8000;
(new Array(10)).fill(0).forEach((zero: number, i: number) => {
const delay = Math.round(i * time / 10);
setTimeout(() => res.write(`slow response chunk #${i} <small>(${delay} ms)</small><br>\n`), delay);
});
setTimeout(() => res.end(), time);
});

export default app;
24 changes: 24 additions & 0 deletions example/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import {GracefulShutdownManager} from '../src/index';
import app from './express';

const PORT_NUMBER = 8080;

const server = app.listen(PORT_NUMBER, (error: any) => {
if (!error) {
console.log('Started Express server on port: %d', PORT_NUMBER);
}
});

const shutdownManager = new GracefulShutdownManager(server);

process.on('SIGTERM', () => onProcessInterrupt('SIGTERM'));
process.on('SIGINT', () => onProcessInterrupt('SIGINT'));


function onProcessInterrupt(signal: string) {
console.log('Termination signal is received from OS (' + signal + '), the application will terminate');
//noinspection JSIgnoredPromiseFromCall
shutdownManager.terminate(() => {
console.log('Server is terminated');
});
}
14 changes: 12 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,18 @@
"typings": "build/src/index.d.ts",
"author": "Slava Fomin II <[email protected]>",
"license": "MIT",
"repository": "[email protected]:moebius-mlm/http-graceful-shutdown.git",
"directories": {
"example": "build/example"
},
"scripts": {
"build": "tsc",
"prepublish": "npm run build"
},
"devDependencies": {
"@types/express": "^4.0.35",
"express": "^4.15.3"
"express": "^4.15.3",
"typescript": "^2.6.2"
},
"keywords": [
"http",
Expand All @@ -23,5 +32,6 @@
"sigterm",
"sigint",
"typescript"
]
],
"dependencies": {}
}
1 change: 1 addition & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"include": [
"src/**/*",
"example/**/*",
"test/**/*"
],
"exclude": [
Expand Down