In an Node.js application I need to run a background task every 10s (task in the general sense). Most of the time, this task is a simple polling another webservice, and every now and then something changes in that web service and the background task updates internal state of my application. FYI, this application runs in a Docker container in an Openshift deployment.
Today I realized that the background task had stopped executing. The log files did not show any exception or other suspicious messages. The CPU and memory consumption of the Openshift pod looked normal. Unfortunately I did not have the verbose log level enabled, only info, and my skills with Linux are not good enough to take a process dump and analyze it.
I am wondering if there is something that I am doing wrong with how I implemented this background task. Also, this is the first time I encountered this (AFAIK) and the application has been under development for over a year now, with application instances running sometimes 2 months or longer without issue.
let pollingActive = true;
let pollingInterval = 10000;
const pollingMethod = () => {
if (!this.pollingActive) {
return;
}
logger.verbose('Start of synchronization cycle.');
performSync() // this is the polling and internal state update work
.then(() => {
this.pollingHandle = setTimeout(() => { pollingMethod(); },
this.pollingInterval);
})
.catch(err => {
logger.info('Error while synchronizing.', err);
this.pollingHandle = setTimeout(() => { pollingMethod(); },
this.pollingInterval);
});
};
pollingMethod();
I double-checked my code and the only time pollingActive becomes false is when the Node.js application shuts down, which I know it didn't because it was still responding to regular REST API requests.
Any idea why this background task could have stopped working?
Side note: I am sure there are better ways to do background tasks in Node.js or a Docker container for that matter, including having the external web service send change notifications via a queue (which may still need to be polled ;-) However, because this project is on the "way out" I am in no position to make significant changes.