Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions src/heartbeatClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Logger } from '@map-colonies/js-logger';
import { httpClientConfig } from './models/utils';

export class HeartbeatClient extends HttpClient {
public intervalKey: NodeJS.Timer | null = null;
private readonly intervalDictionary: Record<string, NodeJS.Timer | undefined>;
public constructor(
protected readonly logger: Logger,
protected intervalMs: number,
Expand All @@ -13,6 +13,7 @@ export class HeartbeatClient extends HttpClient {
disableDebugLogs: boolean | undefined = true
) {
super(logger, heartbeatBaseUrl, targetService, httpRetryConfig, disableDebugLogs);
this.intervalDictionary = {};
}

public start(taskId: string): void {
Expand All @@ -22,11 +23,15 @@ export class HeartbeatClient extends HttpClient {
targetService: this.targetService,
msg: `start heartbeat for taskId=${taskId}`,
});
if (this.intervalKey !== null) {
clearInterval(this.intervalKey);
this.intervalKey = null;
const interval = this.intervalDictionary[taskId];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

change to intervalId

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is not intervalID. It is the interval. You can see that the returned type is Timer

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right, my mistake

if (interval) {
this.logger.warn({
taskId,
msg: `interval for taskId: ${taskId} is not null but it should be null! Maybe there is a bug in the service`,
});
clearInterval(interval);
}
this.intervalKey = setInterval(
this.intervalDictionary[taskId] = setInterval(
// eslint-disable-next-line @typescript-eslint/no-misused-promises
async () => {
await this.send(taskId);
Expand All @@ -42,9 +47,15 @@ export class HeartbeatClient extends HttpClient {
targetService: this.targetService,
msg: `stop heartbeat for taskId=${taskId}`,
});
if (this.intervalKey !== null) {
clearInterval(this.intervalKey);
this.intervalKey = null;
const interval = this.intervalDictionary[taskId];

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also here

if (interval) {
clearInterval(interval);
delete this.intervalDictionary[taskId];
} else {
this.logger.error({
taskId,
msg: `interval for taskId: ${taskId} is not exists but it should exists!`,
});
}
await this.remove(taskId);
}
Expand Down