From 789e697ce3e9195c130c4e048caaf187eb4b4b87 Mon Sep 17 00:00:00 2001 From: Todd Williams Date: Tue, 24 May 2016 16:38:55 -0700 Subject: [PATCH 1/3] Improve production mode 1. Perform a clean up before pm2 starts that will make sure there were no phatom processes running before kicking off new phones. (This one helps both production and development) 2. Run the gluestick build as part of creating the image. This way the server will be ready as soon as it starts. Added "skip build" option to arguments so that the docker image will stop kicking off a build when it starts. Kicking off a build is still useful when running locally and you haven't run `gluestick build` before running with NODE_ENV as production. --- docker/Dockerfile | 3 +- src/cli.js | 11 ++- src/commands/start-server.js | 68 +++++++++++-------- src/config/webpack-isomorphic-tools-config.js | 4 +- 4 files changed, 51 insertions(+), 35 deletions(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index 092f9942b..e991be5ab 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -66,4 +66,5 @@ RUN apt-get update && \ EXPOSE 8888 -CMD ["gluestick", "start", "-T"] +RUN gluestick build +CMD ["gluestick", "start", "-P"] diff --git a/src/cli.js b/src/cli.js index 954160b97..5fd332bac 100755 --- a/src/cli.js +++ b/src/cli.js @@ -36,6 +36,7 @@ const karmaTestOption = ["-k, --karma", "run tests in Karma"]; const mochaReporterOption = ["-r, --reporter [type]", "run tests in Node.js"]; const firefoxOption = ["-F, --firefox", "Use Firefox with test runner"]; const singleRunOption = ["-S, --single", "Run test suite only once"]; +const skipBuildOption = ["-P, --skip-build", "skip build when running in production mode"]; commander .version(currentGluestickVersion); @@ -82,6 +83,7 @@ commander .option(...debugTestOption) .option(...mochaReporterOption) .option(...karmaTestOption) + .option(...skipBuildOption) .action(checkGluestickProject) .action(() => notifyUpdates()) .action(startAll) @@ -210,7 +212,14 @@ async function startAll(options) { process.exit(); } - spawnProcess("client"); + // in production spawning the client really just creates a build. Our docker + // images pre-build and therefor they start with the skip build option as + // true. We only want to start the client in development mode or if + // skipBuild is not specified + if (!(isProduction && options.skipBuild)) { + spawnProcess("client"); + } + spawnProcess("server", (options.debugServer ? ["--debug-server"] : [])); // Start tests unless they asked us not to or we are in production mode diff --git a/src/commands/start-server.js b/src/commands/start-server.js index 18223b455..ee2a8a76e 100644 --- a/src/commands/start-server.js +++ b/src/commands/start-server.js @@ -44,39 +44,47 @@ module.exports = function startServer (debug=false) { process.exit(2); } - pm2.start({ - script: scriptPath, - name: name, - cwd: CWD, - exec_mode: "cluster", - instances: MAX_INSTANCES, // 0 = auto detect based on CPUs - max_memory_restart: process.env.MAX_MEMORY_RESTART || "200M", - environment_name: process.env.NODE_ENV, - no_autorestart: false, - merge_logs: true, - watch: process.env.NODE_ENV !== "production" ? ["assets", "src", "Index.js"] : false - }, (error) => { - if (error) { - logger.error(error); - pm2.disconnect(); - } - - // start showing the logs - spawn(path.join(__dirname, "..", "..", "node_modules", ".bin", "pm2"), ["logs", name, "--raw", "--lines", 0], {stdio: "inherit"}); + // Stop any previous processes with the same name before starting new + // instances + pm2.stop(name, () => { + startPM2(scriptPath, name); }); + }); +}; - /** - * When the app is quit, we go through all of the processes that were - * started up because of PM2 and we terminate them. - */ - process.on("SIGINT", () => { - logger.info(`Stopping pm2 instance: ${highlight(name)}…`); - pm2.delete(name, () => { - pm2.disconnect(() => { - process.exit(); - }); +function startPM2 (scriptPath, name) { + pm2.start({ + script: scriptPath, + name: name, + cwd: CWD, + exec_mode: "cluster", + instances: MAX_INSTANCES, // 0 = auto detect based on CPUs + max_memory_restart: process.env.MAX_MEMORY_RESTART || "200M", + environment_name: process.env.NODE_ENV, + no_autorestart: false, + merge_logs: true, + watch: process.env.NODE_ENV !== "production" ? ["assets", "src", "Index.js"] : false + }, (error) => { + if (error) { + logger.error(error); + pm2.disconnect(); + } + + // start showing the logs + spawn(path.join(__dirname, "..", "..", "node_modules", ".bin", "pm2"), ["logs", name, "--raw", "--lines", 0], {stdio: "inherit"}); + }); + + /** + * When the app is quit, we go through all of the processes that were + * started up because of PM2 and we terminate them. + */ + process.on("SIGINT", () => { + logger.info(`Stopping pm2 instance: ${highlight(name)}…`); + pm2.delete(name, () => { + pm2.disconnect(() => { + process.exit(); }); }); }); -}; +} diff --git a/src/config/webpack-isomorphic-tools-config.js b/src/config/webpack-isomorphic-tools-config.js index d03079411..5f84b67a0 100644 --- a/src/config/webpack-isomorphic-tools-config.js +++ b/src/config/webpack-isomorphic-tools-config.js @@ -42,9 +42,7 @@ module.exports = { styles: { extensions: ["css", "scss", "sass"], filter: function(module, regex, options, log) { - if (options.development) { - return WebpackIsomorphicToolsPlugin.style_loader_filter(module, regex, options, log); - } + return WebpackIsomorphicToolsPlugin.style_loader_filter(module, regex, options, log); }, path: WebpackIsomorphicToolsPlugin.style_loader_path_extractor, parser: WebpackIsomorphicToolsPlugin.css_loader_parser From d9507a5ff62a7a716eef25c5acc04d74155ecb00 Mon Sep 17 00:00:00 2001 From: Todd Williams Date: Tue, 24 May 2016 16:59:17 -0700 Subject: [PATCH 2/3] pre-build --- docker/Dockerfile | 1 - templates/new/src/config/.Dockerfile | 1 + 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/docker/Dockerfile b/docker/Dockerfile index e991be5ab..b99696ae2 100644 --- a/docker/Dockerfile +++ b/docker/Dockerfile @@ -66,5 +66,4 @@ RUN apt-get update && \ EXPOSE 8888 -RUN gluestick build CMD ["gluestick", "start", "-P"] diff --git a/templates/new/src/config/.Dockerfile b/templates/new/src/config/.Dockerfile index 5918b45bf..49dbff3bb 100644 --- a/templates/new/src/config/.Dockerfile +++ b/templates/new/src/config/.Dockerfile @@ -7,3 +7,4 @@ FROM truecar/gluestick:0.7.0 ADD . /app RUN npm install +RUN gluestick build From a3a02f8d832b6c72f9bd1ac2627e3b2bc303aa3b Mon Sep 17 00:00:00 2001 From: Todd Williams Date: Tue, 24 May 2016 17:02:36 -0700 Subject: [PATCH 3/3] bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 2bba7440c..d53c945b9 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "gluestick", - "version": "0.7.0", + "version": "0.7.1", "description": "GlueStick is a command line interface for quickly developing universal web applications using React", "main": "src/cli-harmony.js", "bin": "src/cli-harmony.js",