Thanks to visit codestin.com
Credit goes to github.com

Skip to content

refactor: workspace autostop_schedule -> ttl #1578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 15 commits into from
May 19, 2022
Merged

Conversation

johnstcn
Copy link
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
@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
@johnstcn johnstcn requested a review from a team May 19, 2022 13:55
Comment on lines +273 to 274
if ws.TTL == nil || *ws.TTL == 0 {
return time.Time{}, nil
}
Copy link
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
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
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
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
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
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
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
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.

Copy link
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 on lines +273 to 274
if ws.TTL == nil || *ws.TTL == 0 {
return time.Time{}, nil
}
Copy link
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.

@johnstcn johnstcn requested a review from a team as a code owner May 19, 2022 16:35
Copy link
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.

type UpdateWorkspaceAutostopRequest struct {
Schedule string `json:"schedule"`
type UpdateWorkspaceTTLRequest struct {
TTL *time.Duration `json:"ttl"`
Copy link
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).

@johnstcn johnstcn force-pushed the cj/workspace-autostop-ttl branch from badcfec to b3168c1 Compare May 19, 2022 18:34
@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
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