Skip to content

feat: allow bumping workspace deadline #1828

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 14 commits into from
May 27, 2022
Merged
Prev Previous commit
Next Next commit
fix: less rounding of deadlines
  • Loading branch information
johnstcn committed May 27, 2022
commit cfeb27463e7a752e34e8df85d52eb3c9ae1f72a5
6 changes: 3 additions & 3 deletions cli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func list() *cobra.Command {
if workspace.TTL != nil {
autostopDisplay = durationDisplay(*workspace.TTL)
if has, ext := hasExtension(workspace); has {
autostopDisplay += fmt.Sprintf(" (+%s)", durationDisplay(ext))
autostopDisplay += fmt.Sprintf(" (+%s)", durationDisplay(ext.Round(time.Minute)))
}
}

Expand Down Expand Up @@ -131,8 +131,8 @@ func hasExtension(ws codersdk.Workspace) (bool, time.Duration) {
if ws.TTL == nil {
return false, 0
}
delta := ws.LatestBuild.Deadline.Add(-*ws.TTL).Sub(ws.LatestBuild.UpdatedAt).Round(time.Minute)
if delta <= 0 {
delta := ws.LatestBuild.Deadline.Add(-*ws.TTL).Sub(ws.LatestBuild.CreatedAt)
if delta < time.Minute {
return false, 0
}

Expand Down
5 changes: 3 additions & 2 deletions coderd/autobuild/executor/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,9 @@ func (e *Executor) runOnce(t time.Time) error {
)
continue
}
// Truncate to nearest minute for consistency with autostart behavior
nextTransition = priorHistory.Deadline.Truncate(time.Minute)
// For stopping, do not truncate. This is inconsistent with autostart, but
// it ensures we will not stop too early.
nextTransition = priorHistory.Deadline
Comment on lines +110 to +112
Copy link
Member Author

Choose a reason for hiding this comment

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

review: I decided to stop truncating this as the consistency didn't seem worth losing the fidelity in other places. In the worst case, we'll stop a little bit later (but not too early).

case database.WorkspaceTransitionStop:
validTransition = database.WorkspaceTransitionStart
sched, err := schedule.Weekly(ws.AutostartSchedule.String)
Expand Down
2 changes: 1 addition & 1 deletion coderd/provisionerdaemons.go
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ func (server *provisionerdServer) CompleteJob(ctx context.Context, completed *pr
workspace, err := db.GetWorkspaceByID(ctx, workspaceBuild.WorkspaceID)
if err == nil {
if workspace.Ttl.Valid {
workspaceDeadline = now.Add(time.Duration(workspace.Ttl.Int64)).Truncate(time.Minute)
workspaceDeadline = now.Add(time.Duration(workspace.Ttl.Int64))
}
} else {
// Huh? Did the workspace get deleted?
Expand Down
15 changes: 12 additions & 3 deletions coderd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) {
return xerrors.Errorf("workspace must be started, current status: %s", build.Transition)
}

newDeadline := req.Deadline.Truncate(time.Minute).UTC()
newDeadline := req.Deadline.UTC()
Copy link
Member

Choose a reason for hiding this comment

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

Very nice getting rid of the truncates! 💪🏻

if newDeadline.IsZero() {
// This should not be possible because the struct validation field enforces a non-zero value.
code = http.StatusBadRequest
Expand All @@ -609,8 +609,8 @@ func (api *API) putExtendWorkspace(rw http.ResponseWriter, r *http.Request) {
return xerrors.Errorf("new deadline %q must be after existing deadline %q", newDeadline.Format(time.RFC3339), build.Deadline.Format(time.RFC3339))
}

// both newDeadline and build.Deadline are truncated to time.Minute
if newDeadline == build.Deadline {
// Disallow updates within than one minute
if withinDuration(newDeadline, build.Deadline, time.Minute) {
Copy link
Member Author

Choose a reason for hiding this comment

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

review: it's an edge case, but it doesn't make sense to allow this.

code = http.StatusNotModified
return nil
}
Expand Down Expand Up @@ -839,3 +839,12 @@ func convertSQLNullInt64(i sql.NullInt64) *time.Duration {

return (*time.Duration)(&i.Int64)
}

func withinDuration(t1, t2 time.Time, d time.Duration) bool {
dt := t1.Sub(t2)
if dt < -d || dt > d {
return false
}
Comment on lines +845 to +847
Copy link
Member Author

Choose a reason for hiding this comment

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

review: I just lifted this from require.WithinDuration.


return true
}