Node/Adonis CPU on VPS


#1

I’ve read some stuff about Node not using more then one tread/core on a CPU, is that correct and does this mean that it is no use choosing a VPS with more then one core?


#2

Yes it runs on one core by default, you can run a cluster of server ( only if you need it )


#3

so i don’t need more than 1 CPU core on my VPS?

How about memory?

I will run about 5 Adonis sites with low traffic.


#4

Every node/adonis instance will have an own process. 5 adonis sites could run with 5 processes on a singe core or spread over multiple cores if available.

For 5 Adonis sites with low traffic 1 CPU core should be more than enough, but this highly depends on CPU performance, traffic, other processes like databases, …


#5

PM2 (process-manager for Node) can utilize more than one core for your apps.


#6

@virk how would one properly cluster an adonis instance across multiple cores without using something like PM2? I’m using the legacy adonis version as an HTTP REST API.


#7

Using the Nodejs inbuilt cluster module. Just read the node.js official docs for cluster and code will go in server.js file


#8

@hashbang, if you solve this please share the server.js code.

Thank you.


#9
'use strict'

/*
|--------------------------------------------------------------------------
| Http Server
|--------------------------------------------------------------------------
|
| Here we boot the HTTP Server by calling the exported method. A callback
| function is optionally passed which is executed, once the HTTP server
| is running.
|
*/
const cluster = require('cluster')
const numCPUs = require('os').cpus().length

const http = require('./bootstrap/http')

if (cluster.isMaster) {
  console.log(`Master ${process.pid} is running`)

  // Fork workers.
  for (let i = 0; i < numCPUs; i++) {
    cluster.fork()
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`worker ${worker.process.pid} died`)

    console.log('Starting a new worker')
    cluster.fork()
  })

} else {

  // In this case it is an HTTP server
  http(function () {
    use('Event').fire('Http.start')
  })

  console.log(`Worker ${process.pid} started`)
}

#10

Thank you so much. I use Adonuxt, any chance someone knows how to apply the code above in this server.js?

'use strict'

/*
|--------------------------------------------------------------------------
| Http server
|--------------------------------------------------------------------------
|
| This file bootstrap Adonisjs to start the HTTP server. You are free to
| customize the process of booting the http server.
|
| """ Loading ace commands """
|     At times you may want to load ace commands when starting the HTTP server.
|     Same can be done by chaining `loadCommands()` method after
|
| """ Preloading files """
|     Also you can preload files by calling `preLoad('path/to/file')` method.
|     Make sure to pass relative path from the project root.
*/

const { Ignitor } = require('@adonisjs/ignitor')

new Ignitor(require('@adonisjs/fold'))
  .appRoot(__dirname)
  .fireHttpServer()
  .then(() => {
    return use('App/Services/Nuxt').build()
  })
  .then(() => {
    use('Logger').info('Nuxt is ready to handle requests')
  })
  .catch(console.error)

#11

Hey Peter, add .preLoad(‘start/your_file.js’) juste after .fireHttpServer()