Skip to content
This repository has been archived by the owner on May 6, 2019. It is now read-only.

Commit

Permalink
added real close sequence and a bit more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
natnat-mc committed Jan 12, 2019
1 parent 0ad4f7a commit 0114e8f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
15 changes: 9 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,22 @@ shared.config=function(namespace, key) {
return config[namespace][key];
};

// create global event emitter
const EE=require('events');
const events=new EE();
shared.events=events;

// load database
require('./src/db');

// load everything
require('./src/express');

// setup signal handlers to exit
((a => {
[].forEach.call(['int', 'term', 'hup'], a);
})(s => {
['int', 'term', 'hup'].forEach(s => {
s='SIG'+s.toUpperCase();
process.on(s, () => {
log.notice("Exiting on "+s);
process.exit(0);
log.notice("Exiting due to "+s);
events.emit('die', s);
});
}));
});
10 changes: 5 additions & 5 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ try {
}

// add close handlers
process.on('exit', () => db.close());
process.on('SIGINT', () => db.close());
process.on('SIGHUP', () => db.close());
process.on('SIGTERM', () => db.close());
shared.events.on('die', () => {
db.close();
log.notice("Closed database");
});

// execute db migrations automatically
(() => {
Expand Down Expand Up @@ -57,7 +57,7 @@ process.on('SIGTERM', () => db.close());
}
});

log.info("Database is ready");
log.notice("Database is ready");

shared.db=db;
module.exports=db;
14 changes: 13 additions & 1 deletion src/express.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// load libs
const shared=require('../shared');
const db=require('./db');
const log=require('log').get('express');

// create and setup an app
const express=require('express');
const app=express();
Expand All @@ -9,5 +14,12 @@ app.set('views', process.cwd()+'/views'); // allow EJS views to be loaded
app.use(express.static(process.cwd()+'/static')); // allow static resources to be served

// listen on either the port given in env (or loaded from config) or 3000
app.listen(process.env.PORT || 3000, process.env.ADDRESS || '0.0.0.0');
const server=app.listen(process.env.PORT || 3000, process.env.ADDRESS || '0.0.0.0', () => {
log.notice("Webserver listening on "+(process.env.ADDRESS || '0.0.0.0')+":"+(process.env.PORT || 3000));
});

// setup exit handler
shared.events.on('die', () => {
log.notice("Closing webserver");
server.close(() => log.notice("Closed webserver"));
});

0 comments on commit 0114e8f

Please sign in to comment.