Update idle state to expired

This commit is contained in:
Andrew Baldwin 2025-10-25 21:29:58 +02:00
parent cecb83018f
commit bbbd6edc6a
3 changed files with 6 additions and 6 deletions

View file

@ -9,9 +9,9 @@ export class Heart {
private heartbeatTimer?: NodeJS.Timeout
private heartbeatInterval = 60000
public lastHeartbeat = 0
private readonly _onChange = new Emitter<"alive" | "idle" | "unknown">()
private readonly _onChange = new Emitter<"alive" | "expired" | "unknown">()
readonly onChange = this._onChange.event
private state: "alive" | "idle" | "unknown" = "idle"
private state: "alive" | "expired" | "unknown" = "expired"
public constructor(
private readonly heartbeatPath: string,
@ -54,7 +54,7 @@ export class Heart {
if (await this.isActive()) {
this.beat()
} else {
this.setState("idle")
this.setState("expired")
}
} catch (error: unknown) {
logger.warn((error as Error).message)

View file

@ -182,7 +182,7 @@ export const runCodeServer = async (
heart.onChange((state) => {
clearTimeout(idleShutdownTimer)
if (state === "idle") {
if (state === "expired") {
startIdleShutdownTimer()
}
})

View file

@ -147,7 +147,7 @@ describe("stateChange", () => {
expect(mockOnChange.mock.calls[0][0]).toBe("alive")
})
it.only("should change to idle when not active", async () => {
it.only("should change to expired when not active", async () => {
jest.useFakeTimers()
heart = new Heart(`${testDir}/shutdown.txt`, () => new Promise((resolve) => resolve(false)))
const mockOnChange = jest.fn()
@ -155,7 +155,7 @@ describe("stateChange", () => {
await heart.beat()
await jest.advanceTimersByTime(60 * 1000)
expect(mockOnChange.mock.calls[1][0]).toBe("idle")
expect(mockOnChange.mock.calls[1][0]).toBe("expired")
jest.clearAllTimers()
jest.useRealTimers()
})