Skip to content

Commit 24ccaee

Browse files
committed
fix: derive running ws stop time from deadline
Summary: When a workspace is on, the remaining time until shutdown needs to be derived from the deadline timestamp, not implied from the TTL
1 parent a72b187 commit 24ccaee

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

site/src/components/WorkspaceSchedule/WorkspaceSchedule.tsx

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ import { Link as RouterLink } from "react-router-dom"
1111
import { Workspace } from "../../api/typesGenerated"
1212
import { MONOSPACE_FONT_FAMILY } from "../../theme/constants"
1313
import { extractTimezone, stripTimezone } from "../../util/schedule"
14+
import { isWorkspaceOn } from "../../util/workspace"
1415
import { Stack } from "../Stack/Stack"
1516

1617
dayjs.extend(duration)
1718
dayjs.extend(relativeTime)
1819

19-
const Language = {
20+
export const Language = {
2021
autoStartDisplay: (schedule: string): string => {
2122
if (schedule) {
2223
return cronstrue.toString(stripTimezone(schedule), { throwExceptionOnParseError: false })
@@ -33,24 +34,31 @@ const Language = {
3334
}
3435
},
3536
autoStopDisplay: (workspace: Workspace): string => {
36-
const latest = workspace.latest_build
37+
const deadline = workspace.latest_build.deadline
38+
const ttl = workspace.ttl
3739

38-
if (!workspace.ttl || workspace.ttl < 1) {
39-
return "Manual"
40-
}
41-
42-
if (latest.transition === "start") {
40+
if (isWorkspaceOn(workspace)) {
41+
// Workspace is on --> derive from latest_build.deadline. Note that the
42+
// user may modify their workspace object (ttl) while the workspace is
43+
// running and depending on system semantics, the deadline may still
44+
// represent the previously defined ttl. Thus, we always derive from the
45+
// deadline as the source of truth.
4346
const now = dayjs()
44-
const updatedAt = dayjs(latest.updated_at)
45-
const deadline = updatedAt.add(workspace.ttl / 1_000_000, "ms")
4647
if (now.isAfter(deadline)) {
4748
return "Workspace is shutting down now"
49+
} else {
50+
return now.to(deadline)
4851
}
49-
return now.to(deadline)
52+
} else if (!ttl || ttl < 1) {
53+
// If the workspace is not on, and the ttl is 0 or undefined, then the
54+
// workspace is set to manually shutdown.
55+
return "Manual"
56+
} else {
57+
// The workspace has a ttl set, but is either in an unknown state or is
58+
// not running. Therefore, we derive from workspace.ttl.
59+
const duration = dayjs.duration(ttl / 1_000_000, "milliseconds")
60+
return `${duration.humanize()} after start`
5061
}
51-
52-
const duration = dayjs.duration(workspace.ttl / 1_000_000, "milliseconds")
53-
return `${duration.humanize()} after start`
5462
},
5563
editScheduleLink: "Edit schedule",
5664
schedule: "Schedule",

0 commit comments

Comments
 (0)