Issue
I am trying to create a Schedule service using nestJs' Cron decorator
I have a Cron-decorated method below:
@Cron(CronExpression.EVERY_5_SECONDS)
triggerDataloaderCron() {
this.logger.debug('called every 10 seconds');
return this.healthService.getPCFHealth();
}
And this cron job calls a method in another service, which is shown below
getHealth() {
//code to form up an endpoint, saved as the variable fullUrl
//Does not come into this block
return this.httpService.get(fullUrl).pipe(
map((axiosResponse: AxiosResponse) => {
return axiosResponse.data;
}),
catchError((err) => {
console.log("in error", err);
throw new CustomException(ExceptionConstants.EurekaConnectionException);
})
);
}
When the cron job runs, i am able to enter the getHealth() method, but the this.httpService etc... block of code does not run.
Any suggestions on how this can be achieved? Or if I am going about this the wrong way?
Thanks!
Solution
The getHealth method returns an Observable. An observable does not execute unless there is at least one subscriber.
In your cron method, add a subscription as follows:
this.healthService.getPCFHealth()
.subscribe(response => console.log(response))
Since the cron method executes at regular intervals, I don't think you need to return a value from it.
Answered By - Ravi Mashru Answer Checked By - Dawn Plyler (WPSolving Volunteer)