Skip to content

refactor: workspace autostop_schedule -> ttl#1578

Merged
greyscaled merged 15 commits into
mainfrom
cj/workspace-autostop-ttl
May 19, 2022
Merged

refactor: workspace autostop_schedule -> ttl#1578
greyscaled merged 15 commits into
mainfrom
cj/workspace-autostop-ttl

Conversation

@johnstcn
Copy link
Copy Markdown
Member

@johnstcn johnstcn commented May 18, 2022

This PR makes the following changes:

  • Adds a migration to replace the autostop_schedule column of workspaces with a ttl.
  • Replaces all existing usages of autostop_schedule to ttl in database code etc.
  • Renames CLI command autostop to ttl, updates API routes accordingly
    • Note: To maintain consistency with the autostart behaviour, ttl is truncated to time.Minute. A zero value or only seconds value for TTL is not valid per the CLI.
  • Lifecycle executor now checks if TTL is set and greater than 0 instead of checking the next scheduled time of autostop schedule.
    • Note: I am doing a small bit of fudging of the autostop deadline by adding one minute to ensure that we don't terminate too early.

@johnstcn johnstcn self-assigned this May 18, 2022
Comment thread coderd/database/migrations/999999_rename_this_autostop_ttl.down.sql
Comment thread coderd/database/migrations/999999_rename_this_autostop_ttl.up.sql
Comment thread coderd/database/queries/workspaces.sql
@johnstcn johnstcn force-pushed the cj/workspace-autostop-ttl branch 2 times, most recently from 0593de8 to 22cd000 Compare May 19, 2022 11:21
@johnstcn johnstcn marked this pull request as ready for review May 19, 2022 13:41
Comment thread cli/ttl.go Outdated
@johnstcn johnstcn requested a review from a team May 19, 2022 13:55
Comment thread cli/ssh.go
Comment on lines +273 to 274
if ws.TTL == nil || *ws.TTL == 0 {
return time.Time{}, nil
}
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Instead of using a pointer why not just have 0 mean disabled?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I considered that initially, but I think using the zero value could lead to bugs and/or unintended behaviour. Grey also preferred the semantics of nil | time.Duration from a FE consumption perspective.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

You do have a point, though -- 0 is already an invalid TTL.

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.

Comment: I think the FE or other clients having to know 0 means "manual" is unfortunate, but I'm willing to accept it if it's cleaner for our backend/APIs.

As stated earlier, from v2 forwards I believe it's best that we look at the Coder system as a frontend for a backend, not the other way around.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we then consider that in the API response? We could return something like {"ttl": 0, "ttl_mode": "manual"}?

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.

It's certainly golang convention to make use of "zero-values" in this way, but I don't think we should extend that to the HTTP API, CLI, or frontend, which should behave in ways that are independent of golang's idioms.

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.

@spikecurtis are you referring to @deansheather 's comment here: #1578 (comment)

why not just have 0 mean disabled?

Or the fact that we're using a pointer such that the value can be nil or a number.

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.

Yeah, I feel like among go practitioners, 0 means disabled would be preferred to making it a pointer and nil means disabled.

Comment thread coderd/coderd.go Outdated
Comment thread agent/usershell/usershell_darwin.go
Copy link
Copy Markdown
Contributor

@greyscaled greyscaled left a comment

Choose a reason for hiding this comment

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

Might need to generate the types, since I see codersdk changed types.

This will have FE breakages.

If you don't mind, I can push those fixes to this PR so that it's all taken care of in one. Are you cool with that?

Comment thread cli/list.go
Comment thread cli/ssh.go
Comment on lines +273 to 274
if ws.TTL == nil || *ws.TTL == 0 {
return time.Time{}, nil
}
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.

Comment: I think the FE or other clients having to know 0 means "manual" is unfortunate, but I'm willing to accept it if it's cleaner for our backend/APIs.

As stated earlier, from v2 forwards I believe it's best that we look at the Coder system as a frontend for a backend, not the other way around.

Comment thread cli/ssh.go
Comment thread cli/ttl.go
Comment thread cli/ttl.go Outdated
Comment thread cli/ttl.go
Comment thread cli/ttl_test.go Outdated
Comment thread cli/ttl_test.go
Comment thread coderd/coderd.go Outdated
@johnstcn johnstcn requested a review from a team as a code owner May 19, 2022 16:35
Copy link
Copy Markdown
Member

@mafredri mafredri left a comment

Choose a reason for hiding this comment

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

LGTM 👍🏻. Attached some (optional) suggestions.

Comment thread codersdk/workspaces.go
type UpdateWorkspaceAutostopRequest struct {
Schedule string `json:"schedule"`
type UpdateWorkspaceTTLRequest struct {
TTL *time.Duration `json:"ttl"`
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggestion: Should we implement a custom Duration type like type NullDuration { Duration time.Duration; Valid bool } (optionally in a types or null package) with a custom json.Marshaler? I'm mostly thinking about (Go) consumers here. It's not possible to e.g. do:

req := UpdateWorkspaceTTLRequest {
    TTL: &time.Duration(time.Second),
}

without first assigning to a variable. Similarly the pointer usage could lead to null pointer dereferences (which arguably would be the "correct" behavior, unless it's known that the zero value is OK).

Comment thread cli/ttl.go Outdated
@johnstcn johnstcn force-pushed the cj/workspace-autostop-ttl branch from badcfec to b3168c1 Compare May 19, 2022 18:34
Comment thread site/src/components/WorkspaceSchedule/WorkspaceSchedule.tsx Outdated
Comment thread site/src/components/WorkspaceSchedule/WorkspaceSchedule.tsx Outdated
@greyscaled greyscaled changed the title refactor: workspace: autostop_schedule -> ttl refactor: workspace autostop_schedule -> ttl May 19, 2022
@greyscaled greyscaled merged commit d72c45e into main May 19, 2022
@greyscaled greyscaled deleted the cj/workspace-autostop-ttl branch May 19, 2022 19:09
kylecarbs pushed a commit that referenced this pull request Jun 10, 2022
Co-authored-by: G r e y <grey@coder.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants