Moleculer: Unable to call broker on the same service in started lifecycle event

Created on 9 Jan 2020  路  4Comments  路  Source: moleculerjs/moleculer

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • [x] I am running the latest version
  • [X] I checked the documentation and found no answer
  • [X] I checked to make sure that this issue has not already been filed
  • [X] I'm reporting the issue to the correct repository

Current Behavior

I've created a service user that on the started lifecycle callback calls a few action of the same service through the broker. When I try to do that a SERVICE_NOT_FOUND exception is thrown.

Expected Behavior

I'm expecting that calling an action in the started callback of the service itself wouldn't be a problem.

Failure Information

Exception: ServiceNotFoundError: Service 'user.register' is not found.

Reproduce code snippet

module.exports = {
  name: 'user',
  actions: {
    myAction(ctx) {
      return Promise.resolve();
    }
  },
  methods: {
     _myMethod() {
         // throwing error here! user.myAction not registered
         return this.broker.call('user.myAction'); 
    }
  },
  started() {
      return this._myMethod();
  }
};

Workaround

Calling the action with a delay through setTimeout.

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • Moleculer version: ^0.13.12
  • NodeJS version: v12.13.0
  • Operating System: Mac OS 10.14.6 (Mojave)

All 4 comments

It's not available because while the service is not started properly, it doesn't appear in the Service Registry, so you can't call. You can't call a not started service. If you want to call an own service from started, use the this.actions.myAction() form.

Ok that's fine... Actually I had too much dependencies for the action I wanted to call so I made a bootstrap script inside the started of the broker. In that case I should be sure that all services are correctly initialized and register, shouldn't I?

I'll close this, but first I was wondering there is no lifecycle method in a service that achives my purpose?

You can subscribe to the $broker.started event:

module.exports = {
    name: "posts",

    events: {
        "$broker.started"() {
            // All local services started.
        }
    }
};

That's great, thank you!

Was this page helpful?
0 / 5 - 0 ratings