moleculer-decorators
moleculer-decorators copied to clipboard
Services is called twice when started
Current Behavior
I was doing some tests on my services and noticed that the service is started 2 times and it seems to me the second instance replaces the first one, I put a console.log in the class constructor and it was fired 2 times.
Expected Behavior
The expected behavior was that the class's constructor was called only once, which is more strange is that if you have to add other parameters to the constructor (DI) the first time the constructor is called the additional parameters are undefined and only exist on the second call .
Failure Information
Steps to Reproduce
Please provide detailed steps for reproducing the issue.
Create a service using TS and classes; Put a console.log inside the constructor; Export the class with a function decorator;
Reproduce code snippet
import { Context, Service as MoleculerService, ServiceBroker } from 'moleculer'
import { Action, Service } from 'moleculer-decorators'
import { verify, sign } from 'jsonwebtoken'
import { IAuth } from '@Interfaces'
import { TimeInSeconds } from '../../src/helpers/enums/time'
@Service({ name: 'Auth' })
class AuthService extends MoleculerService {
public constructor(broker: ServiceBroker) {
super(broker)
console.log('Called At: ', new Date())
}
@Action({
cache: {
enabled: true,
ttl: TimeInSeconds.FiveMinutes,
},
params: {
token: 'string'
}
})
public async DecodeJWT(ctx: Context<{token: string}>): Promise<IAuth.JwtPayload | string | null> {
try {
return verify(ctx.params.token, process.env.JWT_SECRET)
} catch (error) {
return null
}
}
@Action({
params: {
id: 'string',
email: { type: 'string', optional: true },
name: 'string'
}
})
public async GenerateJWT(ctx: Context<IAuth.JwtDto>): Promise<string | null> {
try {
const { id, email, name } = ctx.params
return sign({id, email, name}, process.env.JWT_SECRET)
} catch (error) {
return null
}
}
}
export default (broker: ServiceBroker): MoleculerService => new AuthService(broker)
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 Decorator version:
1.3.0
- Moleculer version:
0.14.16
- NodeJS version:
v14.16.1
- Operating System:
MacOS BigSur 11.4
Failure Logs
$ ts-node ./node_modules/moleculer/bin/moleculer-runner.js --env --repl --hot services/**/*.ts services/**/*.js
[2021-08-19T13:41:43.652Z] INFO nmac-1381.local-46462/BROKER: Moleculer v0.14.16 is starting...
[2021-08-19T13:41:43.654Z] INFO nmac-1381.local-46462/BROKER: Namespace: <not defined>
[2021-08-19T13:41:43.654Z] INFO nmac-1381.local-46462/BROKER: Node ID: nmac-1381.local-46462
[2021-08-19T13:41:43.655Z] INFO nmac-1381.local-46462/REGISTRY: Strategy: RoundRobinStrategy
[2021-08-19T13:41:43.655Z] INFO nmac-1381.local-46462/REGISTRY: Discoverer: LocalDiscoverer
[2021-08-19T13:41:43.656Z] INFO nmac-1381.local-46462/BROKER: Serializer: JSONSerializer
[2021-08-19T13:41:43.669Z] INFO nmac-1381.local-46462/BROKER: Validator: FastestValidator
[2021-08-19T13:41:43.671Z] INFO nmac-1381.local-46462/BROKER: Registered 14 internal middleware(s).
Called At: 2021-08-19T13:41:49.022Z
Called At: 2021-08-19T13:41:49.023Z