Skip to content

Instantly share code, notes, and snippets.

@devicezero
Last active June 7, 2024 11:14
Show Gist options
  • Save devicezero/5074169c36020c31019389cc5d2720ed to your computer and use it in GitHub Desktop.
Save devicezero/5074169c36020c31019389cc5d2720ed to your computer and use it in GitHub Desktop.
pm2 nuxt js example
// production usage at your own risk
const fs = require("fs");
let numberOfProcesses = 1;
let maxMemory = 128;
let maxMemoryPerInstance = 128;
if (fs.existsSync("/run/config.json")) {
const pshConfig = require("/run/config.json");
// on grid environments, we don't want to round up to not run too many processes
if (pshConfig.info.limits.cpu > 1) {
numberOfProcesses = Math.floor(pshConfig.info.limits.cpu);
}
// on DG3 environments we can safely round up
if (process.env.PLATFORM_MODE) {
numberOfProcesses = Math.ceil(pshConfig.info.limits.cpu);
}
maxMemory = pshConfig.info.limits.memory;
// Distribute the available memory across all instances
maxMemoryPerInstance = Math.ceil(maxMemory / numberOfProcesses);
}
module.exports = {
apps: [
{
name: "index",
script: "./index.js",
// args: `start -c ${process.cwd()}/nuxt.config.js`,
cwd: '/app',
instances: numberOfProcesses,
exec_mode: "cluster",
max_memory_restart: `${maxMemoryPerInstance}M`,
node_args: `--max_old_space_size=${maxMemoryPerInstance}`,
},
],
};
@devicezero
Copy link
Author

.platform.app.yaml modifications

dependencies:
  nodejs:
    pm2: ^5.3.0

pm2 needs a mount

mounts:
  "run":
    source: local
    source_path: run
  ".cache":
    source: local
    source_path: cache
  ".pm2":
    source: local
    source_path: pm2

new start command

web:
  commands:
    start: NUXT_PORT=$PORT PM2_HOME=$PLATFORM_APP_DIR/.pm2 pm2-runtime start ecosystem.config.js

nuxt.config.js modification
we will need this at the root level (telemetry messes with the start command and due to the pm2 setup we need to specify the build + root dir to nuxt)

rootDir: process.cwd(),
buildDir: process.cwd() + '/.nuxt/',
telemetry: false

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment