From 343f009150f67e8e2754602fc0459d2b92247295 Mon Sep 17 00:00:00 2001 From: Chi Vinh Le Date: Tue, 17 Oct 2017 15:57:34 +0700 Subject: [PATCH] Refactor serve --- bin/cli-serve | 178 +------------------------------ scripts/e2e-serve.js => serve.js | 60 ++++++----- test/e2e/globals.js | 2 +- 3 files changed, 35 insertions(+), 205 deletions(-) rename scripts/e2e-serve.js => serve.js (71%) diff --git a/bin/cli-serve b/bin/cli-serve index b04714690..4324cac92 100755 --- a/bin/cli-serve +++ b/bin/cli-serve @@ -1,161 +1,7 @@ #!/usr/bin/env node const program = require('./commander'); -const app = require('../app'); -const debug = require('debug')('talk:cli:serve'); -const errors = require('../errors'); -const {createServer} = require('http'); -const scraper = require('../services/scraper'); -const mailer = require('../services/mailer'); -const MigrationService = require('../services/migration'); -const SetupService = require('../services/setup'); -const kue = require('../services/kue'); -const mongoose = require('../services/mongoose'); -const util = require('./util'); -const cache = require('../services/cache'); -const {createSubscriptionManager} = require('../graph/subscriptions'); -const { - PORT -} = require('../config'); - -/** -* Get port from environment and store in Express. -*/ - -const port = normalizePort(PORT); -app.set('port', port); - -/** -* Create HTTP server. -*/ -const server = 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. - */ - -async function onListening() { - - // Start the cache instance. - await cache.init(); - - let addr = server.address(); - let bind = typeof addr === 'string' - ? `pipe ${addr}` - : `port ${addr.port}`; - debug(`API Server Listening on ${bind}`); -} - -/** - * Start the app. - */ -async function startApp(program) { - - try { - - // Check to see if the application is installed. If the application - // has been installed, then it will throw errors.ErrSettingsNotInit, this - // just means we don't have to check that the migrations have run. - await SetupService.isAvailable(); - - debug('setup is currently available, migrations not being checked'); - - } catch (e) { - - // Check the error. - switch (e) { - case errors.ErrInstallLock, errors.ErrSettingsInit: - - debug('setup is not currently available, migrations now being checked'); - - // The error was expected, just continue. - break; - default: - - // The error was not expected, throw the error! - throw e; - } - - // Now try and check the migration status. - try { - - // Verify that the minimum migration version is met. - await MigrationService.verify(); - - } catch (e) { - console.error(e); - process.exit(1); - } - - debug('migrations do not have to be run'); - } - - /** - * Listen on provided port, on all network interfaces. - */ - server.on('error', onError); - server.on('listening', onListening); - server.on('listening', () => { - - }); - server.listen(port, () => { - - // Mount the websocket server if requested. - if (program.websockets) { - debug(`Websocket Server Listening on ${port}`); - - // Mount the subscriptions server on the application server. - createSubscriptionManager(server); - } - }); -} +const serve = require('../serve'); //============================================================================== // Setting up the program command line arguments. @@ -166,24 +12,6 @@ program .option('-w, --websockets', 'enable the websocket (subscriptions) handler on this thread') .parse(process.argv); -// Start the application serving. -startApp(program); +// Start serving. +serve({jobs: program.jobs, websockets: program.websockets}); -// 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() -]); diff --git a/scripts/e2e-serve.js b/serve.js similarity index 71% rename from scripts/e2e-serve.js rename to serve.js index 2b94da9f7..e97095d52 100644 --- a/scripts/e2e-serve.js +++ b/serve.js @@ -1,28 +1,21 @@ -#!/usr/bin/env node - -const debug = require('debug')('talk:e2e:serve'); -const app = require('../app'); -const errors = require('../errors'); +const app = require('./app'); +const debug = require('debug')('talk:cli:serve'); +const errors = require('./errors'); const {createServer} = require('http'); -const scraper = require('../services/scraper'); -const mailer = require('../services/mailer'); -const SetupService = require('../services/setup'); -const kue = require('../services/kue'); -const mongoose = require('../services/mongoose'); -const util = require('../bin/util'); -const cache = require('../services/cache'); -const MigrationService = require('../services/migration'); -const {createSubscriptionManager} = require('../graph/subscriptions'); +const scraper = require('./services/scraper'); +const mailer = require('./services/mailer'); +const MigrationService = require('./services/migration'); +const SetupService = require('./services/setup'); +const kue = require('./services/kue'); +const mongoose = require('./services/mongoose'); +const cache = require('./services/cache'); +const util = require('./bin/util'); +const {createSubscriptionManager} = require('./graph/subscriptions'); const { PORT -} = require('../config'); - -/** -* Get port from environment and store in Express. -*/ +} = require('./config'); const port = normalizePort(PORT); -app.set('port', port); /** * Create HTTP server. @@ -95,7 +88,8 @@ async function onListening() { /** * Start the app. */ -async function serve() { +async function serve({jobs = true, websockets = true} = {}) { + try { // Check to see if the application is installed. If the application @@ -131,6 +125,8 @@ async function serve() { console.error(e); process.exit(1); } + + debug('migrations do not have to be run'); } /** @@ -144,23 +140,29 @@ async function serve() { server.listen(port, () => { // Mount the websocket server if requested. - debug(`Websocket Server Listening on ${port}`); + if (websockets) { + debug(`Websocket Server Listening on ${port}`); - // Mount the subscriptions server on the application server. - createSubscriptionManager(server); + // Mount the subscriptions server on the application server. + createSubscriptionManager(server); + } }); - // Start the scraper processor. - scraper.process(); + // Enable job processing on the thread if enabled. + if (jobs) { - // Start the mail processor. - mailer.process(); + // 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([ - () => kue.Task.shutdown(), + () => jobs ? kue.Task.shutdown() : null, () => mongoose.disconnect(), () => server.close() ]); diff --git a/test/e2e/globals.js b/test/e2e/globals.js index a894ef66a..78ab7f136 100644 --- a/test/e2e/globals.js +++ b/test/e2e/globals.js @@ -1,4 +1,4 @@ -const serve = require('../../scripts/e2e-serve'); +const serve = require('../../serve'); const mongoose = require('../../services/mongoose'); const {shutdown} = require('../../bin/util');