mirror of
https://github.com/wassname/talk.git
synced 2026-06-29 04:11:26 +08:00
f488b0e02d
- Adjusted docs - Fixed issues with process terminating wrong
126 lines
2.6 KiB
JavaScript
Executable File
126 lines
2.6 KiB
JavaScript
Executable File
#!/usr/bin/env node
|
|
|
|
const app = require('../app');
|
|
const program = require('./commander');
|
|
const http = require('http');
|
|
const scraper = require('../services/scraper');
|
|
const mailer = require('../services/mailer');
|
|
const kue = require('../services/kue');
|
|
const mongoose = require('../services/mongoose');
|
|
const util = require('./util');
|
|
|
|
/**
|
|
* Get port from environment and store in Express.
|
|
*/
|
|
|
|
const port = normalizePort(process.env.TALK_PORT || '3000');
|
|
|
|
app.set('port', port);
|
|
|
|
/**
|
|
* Create HTTP server.
|
|
*/
|
|
const server = http.createServer(app);
|
|
|
|
/**
|
|
* Event listener for HTTP server "error" event.
|
|
*/
|
|
function onError(error) {
|
|
if (error.syscall !== 'listen') {
|
|
throw error;
|
|
}
|
|
|
|
let bind = typeof port === 'string'
|
|
? `Pipe ${port}`
|
|
: `Port ${port}`;
|
|
|
|
// handle specific listen errors with friendly messages
|
|
switch (error.code) {
|
|
case 'EACCES':
|
|
console.error(`${bind} requires elevated privileges`);
|
|
break;
|
|
case 'EADDRINUSE':
|
|
console.error(`${bind} is already in use`);
|
|
break;
|
|
}
|
|
|
|
throw error;
|
|
}
|
|
|
|
/**
|
|
* Normalize a port into a number, string, or false.
|
|
*/
|
|
|
|
function normalizePort(val) {
|
|
let port = parseInt(val, 10);
|
|
|
|
if (isNaN(port)) {
|
|
|
|
// named pipe
|
|
return val;
|
|
}
|
|
|
|
if (port >= 0) {
|
|
|
|
// port number
|
|
return port;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Event listener for HTTP server "listening" event.
|
|
*/
|
|
|
|
function onListening() {
|
|
let addr = server.address();
|
|
let bind = typeof addr === 'string'
|
|
? `pipe ${ addr}`
|
|
: `port ${ addr.port}`;
|
|
console.log(`Listening on ${ bind}`);
|
|
}
|
|
|
|
/**
|
|
* Start the app.
|
|
*/
|
|
function startApp() {
|
|
|
|
/**
|
|
* Listen on provided port, on all network interfaces.
|
|
*/
|
|
server.listen(port);
|
|
server.on('error', onError);
|
|
server.on('listening', onListening);
|
|
}
|
|
|
|
//==============================================================================
|
|
// Setting up the program command line arguments.
|
|
//==============================================================================
|
|
|
|
program
|
|
.option('-j, --jobs', 'enable job processing on this thread')
|
|
.parse(process.argv);
|
|
|
|
// Start the application serving.
|
|
startApp();
|
|
|
|
// Enable job processing on the thread if enabled.
|
|
if (program.jobs) {
|
|
|
|
// Start the scraper processor.
|
|
scraper.process();
|
|
|
|
// Start the mail processor.
|
|
mailer.process();
|
|
}
|
|
|
|
// Define a safe shutdown function to call in the event we need to shutdown
|
|
// because the node hooks are below which will interrupt the shutdown process.
|
|
// Shutdown the mongoose connection, the app server, and the scraper.
|
|
util.onshutdown([
|
|
() => program.jobs ? kue.Task.shutdown() : null,
|
|
() => mongoose.disconnect(),
|
|
() => server.close()
|
|
]);
|