I would like to create a scheduled task for my NestJs application. It should execute every X seconds so I use intervals as described here.
The application makes use of configuration files so I could use keep the interval configurable. But how would I pass in a variable to the Typescript decorator?
NestJs is providing a sample repository for scheduled tasks
So based on the sample I would like to have something like
@Injectable()
export class TasksService {
constructor(
private readonly myConfigService: MyConfigService,
) {}
@Interval(this.myConfigService.intervalInMilliseconds)
handleInterval() {
// ...
}
}
Do I have to use the SchedulerRegistry
as described below in the docs? It seems this is not possible with standard Typescript, see this thread.
I would like to create a scheduled task for my NestJs application. It should execute every X seconds so I use intervals as described here.
The application makes use of configuration files so I could use keep the interval configurable. But how would I pass in a variable to the Typescript decorator?
NestJs is providing a sample repository for scheduled tasks
So based on the sample I would like to have something like
@Injectable()
export class TasksService {
constructor(
private readonly myConfigService: MyConfigService,
) {}
@Interval(this.myConfigService.intervalInMilliseconds)
handleInterval() {
// ...
}
}
Do I have to use the SchedulerRegistry
as described below in the docs? It seems this is not possible with standard Typescript, see this thread.
1 Answer
Reset to default 7This is not possible with the declarative API (annotations), you have to register the cron job dynamically (see the docs):
@Injectable()
export class TasksService implements OnModuleInit {
constructor(
private readonly myConfigService: MyConfigService,
private readonly schedulerRegistry: SchedulerRegistry,
) {}
onModuleInit() {
const interval = setInterval(() => this.handleInterval, this.myConfigService.intervalInMs);
this.schedulerRegistry.addInterval('my-dynamic-interval', interval);
}
handleInterval() {
// ...
}
}
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745286190a4620575.html
评论列表(0条)