Skip to content

Commit a434e98

Browse files
committed
rename HeartbeatService to IntervalService
1 parent b61b360 commit a434e98

File tree

2 files changed

+42
-40
lines changed

2 files changed

+42
-40
lines changed

packages/core/src/v3/runEngineWorker/supervisor/session.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { VERSION } from "../../../version.js";
88
import { io, Socket } from "socket.io-client";
99
import { WorkerClientToServerEvents, WorkerServerToClientEvents } from "../types.js";
1010
import { getDefaultWorkerHeaders } from "./util.js";
11-
import { HeartbeatService } from "../../utils/interval.js";
11+
import { IntervalService } from "../../utils/interval.js";
1212

1313
type SupervisorSessionOptions = SupervisorClientCommonOptions & {
1414
queueConsumerEnabled?: boolean;
@@ -29,7 +29,7 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
2929
private readonly queueConsumerEnabled: boolean;
3030
private readonly queueConsumer: RunQueueConsumer;
3131

32-
private readonly heartbeatService: HeartbeatService;
32+
private readonly heartbeat: IntervalService;
3333
private readonly heartbeatIntervalSeconds: number;
3434

3535
constructor(private opts: SupervisorSessionOptions) {
@@ -50,8 +50,8 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
5050

5151
// TODO: This should be dynamic and set by (or at least overridden by) the platform
5252
this.heartbeatIntervalSeconds = opts.heartbeatIntervalSeconds || 30;
53-
this.heartbeatService = new HeartbeatService({
54-
heartbeat: async () => {
53+
this.heartbeat = new IntervalService({
54+
onInterval: async () => {
5555
console.debug("[SupervisorSession] Sending heartbeat");
5656

5757
const body = this.getHeartbeatBody();
@@ -182,7 +182,7 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
182182
if (this.queueConsumerEnabled) {
183183
console.log("[SupervisorSession] Queue consumer enabled");
184184
this.queueConsumer.start();
185-
this.heartbeatService.start();
185+
this.heartbeat.start();
186186
} else {
187187
console.warn("[SupervisorSession] Queue consumer disabled");
188188
}
@@ -196,7 +196,7 @@ export class SupervisorSession extends EventEmitter<WorkerEvents> {
196196
}
197197

198198
async stop() {
199-
this.heartbeatService.stop();
199+
this.heartbeat.stop();
200200
this.runNotificationsSocket?.disconnect();
201201
}
202202

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,95 @@
1-
type HeartbeatServiceOptions = {
2-
heartbeat: () => Promise<void>;
1+
type IntervalServiceOptions = {
2+
onInterval: () => Promise<void>;
3+
onError?: (error: unknown) => Promise<void>;
34
intervalMs?: number;
45
leadingEdge?: boolean;
5-
onError?: (error: unknown) => Promise<void>;
66
};
77

8-
export class HeartbeatService {
9-
private _heartbeat: () => Promise<void>;
8+
export class IntervalService {
9+
private _onInterval: () => Promise<void>;
10+
private _onError?: (error: unknown) => Promise<void>;
11+
1012
private _intervalMs: number;
11-
private _nextHeartbeat: NodeJS.Timeout | undefined;
13+
private _nextInterval: NodeJS.Timeout | undefined;
1214
private _leadingEdge: boolean;
13-
private _isHeartbeating: boolean;
14-
private _onError?: (error: unknown) => Promise<void>;
15+
private _isEnabled: boolean;
16+
17+
constructor(opts: IntervalServiceOptions) {
18+
this._onInterval = opts.onInterval;
19+
this._onError = opts.onError;
1520

16-
constructor(opts: HeartbeatServiceOptions) {
17-
this._heartbeat = opts.heartbeat;
1821
this._intervalMs = opts.intervalMs ?? 45_000;
19-
this._nextHeartbeat = undefined;
22+
this._nextInterval = undefined;
2023
this._leadingEdge = opts.leadingEdge ?? false;
21-
this._isHeartbeating = false;
22-
this._onError = opts.onError;
24+
this._isEnabled = false;
2325
}
2426

2527
start() {
26-
if (this._isHeartbeating) {
28+
if (this._isEnabled) {
2729
return;
2830
}
2931

30-
this._isHeartbeating = true;
32+
this._isEnabled = true;
3133

3234
if (this._leadingEdge) {
33-
this.#doHeartbeat();
35+
this.#doInterval();
3436
} else {
35-
this.#scheduleNextHeartbeat();
37+
this.#scheduleNextInterval();
3638
}
3739
}
3840

3941
stop() {
40-
if (!this._isHeartbeating) {
42+
if (!this._isEnabled) {
4143
return;
4244
}
4345

44-
this._isHeartbeating = false;
45-
this.#clearNextHeartbeat();
46+
this._isEnabled = false;
47+
this.#clearNextInterval();
4648
}
4749

4850
resetCurrentInterval() {
49-
if (!this._isHeartbeating) {
51+
if (!this._isEnabled) {
5052
return;
5153
}
5254

53-
this.#clearNextHeartbeat();
54-
this.#scheduleNextHeartbeat();
55+
this.#clearNextInterval();
56+
this.#scheduleNextInterval();
5557
}
5658

5759
updateInterval(intervalMs: number) {
5860
this._intervalMs = intervalMs;
5961
this.resetCurrentInterval();
6062
}
6163

62-
#doHeartbeat = async () => {
63-
this.#clearNextHeartbeat();
64+
#doInterval = async () => {
65+
this.#clearNextInterval();
6466

65-
if (!this._isHeartbeating) {
67+
if (!this._isEnabled) {
6668
return;
6769
}
6870

6971
try {
70-
await this._heartbeat();
72+
await this._onInterval();
7173
} catch (error) {
7274
if (this._onError) {
7375
try {
7476
await this._onError(error);
7577
} catch (error) {
76-
console.error("Error handling heartbeat error", error);
78+
console.error("Error during interval error handler", error);
7779
}
7880
}
7981
}
8082

81-
this.#scheduleNextHeartbeat();
83+
this.#scheduleNextInterval();
8284
};
8385

84-
#clearNextHeartbeat() {
85-
if (this._nextHeartbeat) {
86-
clearTimeout(this._nextHeartbeat);
86+
#clearNextInterval() {
87+
if (this._nextInterval) {
88+
clearTimeout(this._nextInterval);
8789
}
8890
}
8991

90-
#scheduleNextHeartbeat() {
91-
this._nextHeartbeat = setTimeout(this.#doHeartbeat, this._intervalMs);
92+
#scheduleNextInterval() {
93+
this._nextInterval = setTimeout(this.#doInterval, this._intervalMs);
9294
}
9395
}

0 commit comments

Comments
 (0)