moleculer icon indicating copy to clipboard operation
moleculer copied to clipboard

moleculer exits without error message

Open elixiao opened this issue 5 years ago • 9 comments

I use moleculer with node-config. App crashes immediately after running without any message. After hours of research, I found maybe the reason is config object is sealed and immutable but moleculer wants to modify it. If moleculer failed to start, why not print some error messages?

const config = require('config')
const {ServiceBroker} = require('moleculer')
const broker = new ServiceBroker(config.get('moleculer'))
broker.start()
  .then(() => {
    console.log('moleculer broker success!')
  })
  .catch((e: any) => {
    console.log('moleculer broker failed!')
  })

elixiao avatar Apr 02 '20 04:04 elixiao

From FAQ: If there is no continuously running process (e.g., transporter connection, API gateway, DB connection) that keeps event loop running then the process will exit. It’s normal behavior and not a bug. If you want to keep your service broker running then you should keep the event loop “busy”. Try to enable the transporter in moleculer.config.js.

AndreMaz avatar Apr 02 '20 05:04 AndreMaz

@AndreMaz There is transporter connection in moleculer.config.js. The problem is if transporter.options is sealed, Moleculer exits without error message. You can try the following BrokerOptions:

{
  namespace: 'test',
  logLevel: 'warn',
  transporter: {
    type: 'NATS',
    options: Object.seal({
      url: 'nats://nats:4222',
      maxReconnectAttempts: 10
    })
  }
}

elixiao avatar Apr 02 '20 07:04 elixiao

Hey @elixiao

Tested over here with your snipped and I got a Unable to create ServiceBroker error that is present in ServiceBroker constructor.

image

AndreMaz avatar Apr 02 '20 10:04 AndreMaz

As a workaround you can use lodash cloneDeep function to "unseal" the config. object right before passing it to the constructor. This way the ServiceBroker can modify it and inject default configs.

https://github.com/moleculerjs/moleculer/blob/78b66743e18f8eb706491b60e156bfb9572035a1/src/service-broker.js#L149

AndreMaz avatar Apr 02 '20 10:04 AndreMaz

@AndreMaz Thanks. Add logger:true to BrokerOptions could show error messages now. Because config.get('xxx') results a deep sealed object. If I don't add logger: true, the app just exits without any output.

const {ServiceBroker} = require('moleculer')

const options = Object.seal({
  namespace: 'test',
  logLevel: 'warn',
  logger: true, // <---- here is the key point, if remove this line, there are no error messages and app just exits
  transporter: Object.seal({
    type: 'NATS',
    options: Object.seal({
      url: 'nats://localhost:4222',
      maxReconnectAttempts: 10
    })
  })
})

const broker = new ServiceBroker(options)

broker.start()
  .then(() => {
    console.log('moleculer broker success!')
  })
  .catch((e) => {
    console.log('moleculer broker failed!')
  })

elixiao avatar Apr 03 '20 08:04 elixiao

@elixiao what is your use-case why you are using sealed objects?

icebob avatar Apr 04 '20 08:04 icebob

@icebob Actually I don't want to use sealed objects. However, node-config returns me a sealed object. I put moleculer BrokerOptions into default.json/production.json in order to connect to different transporter. node-config loads different json file according to different NODE_ENV.

image

const config = require('config')
const {ServiceBroker} = require('moleculer')
const brokerOptions = config.get('moleculer') // returns a deep sealed object
const broker = new ServiceBroker(brokerOptions)

elixiao avatar Apr 05 '20 05:04 elixiao

Please create a repro repo and I will check what we can do to solve it.

icebob avatar Apr 05 '20 08:04 icebob

@icebob https://github.com/elixiao/moleculer-demo use node src/ or npm run dev to run demo.

elixiao avatar Apr 05 '20 13:04 elixiao

I'm closing this issue, because we don't know, how we can solve it inside Moleculer. If you have idea or solution please reopen or open a PR.

icebob avatar Jan 14 '23 13:01 icebob