Moleculer: requestID is null on first call, but has a value next

Created on 18 Dec 2019  路  6Comments  路  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 Behaviour

The requestID of a call is null on the first call to a service. On next calls it has a value.

Expected Behaviour

requestID should have the same value.

Steps to Reproduce

Run, the snippet below, check the logs.
A null value is logged on the first call, but it has a different value on the second call.

Reproduce code snippet

See the code at https://codesandbox.io/s/moleculer-sample-2mz7d

Context

I have a service which is reusing data for every request. I need the requestID to be valued every time I call the service within the same request, especially for tests.

Workaround

I found that by triggering the request.id getter in the first parent service, the requestID is always valued in nested calls. To see the workaround, just replace the line 18 of the code above with:

ctx.id && (await ctx.call("called.action"));

Alternatively, I could use the parentID as a placeholder if requestID is null, correct?

Failure Logs

null 'f19bf0e5-be19-4029-965b-55749bc78774'
f19bf0e5-be19-4029-965b-55749bc78774 f19bf0e5-be19-4029-965b-55749bc78774

the first line shows the issue.

Core Wait for next release Bug

All 6 comments

Something weird is happening over here :confused:
Tried to debug to see what's happening and just hovering twice over ctx sets the requestID.
Hover the first time and it's null, hover the second time and it's set...

Y9jiglViZy

ctx.id is a getter, so it's value will be generated only if need. Generating UUID is an expensive operation, so it will generate only if it's read.

https://github.com/moleculerjs/moleculer/blob/a0b5d9989cd0e4553cc160635f0895c86d1d2ddb/src/context.js#L139-L146

Thanks @icebob that clears things

I think the only solution if we convert requestID to getter/setter (like id) and if not set manually, we should set from ctx.id

@icebob I think the issue is here: https://github.com/moleculerjs/moleculer/blob/a0b5d9989cd0e4553cc160635f0895c86d1d2ddb/src/context.js#L122

On the first call the getter is triggered by the called service after the requestID computation. So probably it could be fixed by moving the parentID setup before the requestID.
By the way, this code would work only when the parent and the child are referencing the same object, which means that it works only if the services are local, correct?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ngraef picture ngraef  路  3Comments

kesslerdev picture kesslerdev  路  4Comments

ngraef picture ngraef  路  4Comments

rozhddmi picture rozhddmi  路  4Comments

HighSoftWare96 picture HighSoftWare96  路  4Comments