-
Notifications
You must be signed in to change notification settings - Fork 887
feat: increase autostop deadline if it passes autostart #10035
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,7 +19,6 @@ import ( | |
"github.com/coder/coder/v2/coderd/database/provisionerjobs" | ||
"github.com/coder/coder/v2/coderd/database/pubsub" | ||
"github.com/coder/coder/v2/coderd/schedule" | ||
"github.com/coder/coder/v2/coderd/schedule/cron" | ||
"github.com/coder/coder/v2/coderd/wsbuilder" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
|
@@ -116,7 +115,7 @@ func (e *Executor) runOnce(t time.Time) Stats { | |
// NOTE: If a workspace build is created with a given TTL and then the user either | ||
// changes or unsets the TTL, the deadline for the workspace build will not | ||
// have changed. This behavior is as expected per #2229. | ||
workspaces, err := e.db.GetWorkspacesEligibleForTransition(e.ctx, t) | ||
workspaces, err := e.db.GetWorkspacesEligibleForTransition(e.ctx) | ||
if err != nil { | ||
e.log.Error(e.ctx, "get workspaces for autostart or autostop", slog.Error(err)) | ||
return stats | ||
|
@@ -183,7 +182,26 @@ func (e *Executor) runOnce(t time.Time) Stats { | |
} | ||
} | ||
|
||
// Transition the workspace to dormant if it has breached the template's | ||
if reason == database.BuildReasonBump { | ||
newDeadline := shouldBump(ws, latestBuild, latestJob, templateSchedule, currentTick) | ||
if !newDeadline.IsZero() { | ||
err := tx.UpdateWorkspaceBuildDeadlineByID(e.ctx, database.UpdateWorkspaceBuildDeadlineByIDParams{ | ||
ID: latestBuild.ID, | ||
UpdatedAt: dbtime.Now(), | ||
Deadline: newDeadline, | ||
MaxDeadline: latestBuild.MaxDeadline, | ||
}) | ||
if err != nil { | ||
log.Error(e.ctx, "unable to bump workspace deadline", | ||
slog.F("new_deadline", newDeadline), | ||
slog.Error(err), | ||
) | ||
return nil | ||
} | ||
} | ||
} | ||
|
||
// Transition the workspace to dormant if it has breached the template's | ||
// threshold for inactivity. | ||
if reason == database.BuildReasonAutolock { | ||
ws, err = tx.UpdateWorkspaceDormantDeletingAt(e.ctx, database.UpdateWorkspaceDormantDeletingAtParams{ | ||
|
@@ -295,6 +313,8 @@ func getNextTransition( | |
|
||
case isEligibleForDelete(ws, templateSchedule, currentTick): | ||
return database.WorkspaceTransitionDelete, database.BuildReasonAutodelete, nil | ||
case !shouldBump(ws, latestBuild, latestJob, templateSchedule, currentTick).IsZero(): | ||
return "", database.BuildReasonBump, nil | ||
default: | ||
return "", "", xerrors.Errorf("last transition not valid for autostart or autostop") | ||
} | ||
|
@@ -320,14 +340,11 @@ func isEligibleForAutostart(ws database.Workspace, build database.WorkspaceBuild | |
|
||
// If autostart isn't enabled, or the schedule isn't valid/populated we can't | ||
// autostart the workspace. | ||
if !templateSchedule.UserAutostartEnabled || !ws.AutostartSchedule.Valid || ws.AutostartSchedule.String == "" { | ||
sched, err := schedule.GetWorkspaceAutostartSchedule(templateSchedule, ws) | ||
if err != nil || sched == nil { | ||
return false | ||
} | ||
|
||
sched, err := cron.Weekly(ws.AutostartSchedule.String) | ||
if err != nil { | ||
return false | ||
} | ||
// Round down to the nearest minute, as this is the finest granularity cron supports. | ||
// Truncate is probably not necessary here, but doing it anyway to be sure. | ||
nextTransition := sched.Next(build.CreatedAt).Truncate(time.Minute) | ||
|
@@ -385,3 +402,49 @@ func isEligibleForFailedStop(build database.WorkspaceBuild, job database.Provisi | |
job.CompletedAt.Valid && | ||
currentTick.Sub(job.CompletedAt.Time) > templateSchedule.FailureTTL | ||
} | ||
|
||
// shouldBump returns a non-zero time if the workspace deadline should | ||
// should be bumped. | ||
func shouldBump(ws database.Workspace, build database.WorkspaceBuild, job database.ProvisionerJob, templateSchedule schedule.TemplateScheduleOptions, currentTick time.Time) time.Time { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: suggest renaming this to |
||
if build.Transition != database.WorkspaceTransitionStart { | ||
return time.Time{} | ||
} | ||
if db2sdk.ProvisionerJobStatus(job) != codersdk.ProvisionerJobSucceeded { | ||
return time.Time{} | ||
} | ||
if build.Deadline.IsZero() { | ||
return time.Time{} | ||
} | ||
if currentTick.After(build.Deadline.Add(time.Hour)) { | ||
// If the workspace has already breached its deadline + 1h, we should | ||
// not bump it, because we don't want to race autostop code. | ||
return time.Time{} | ||
} | ||
|
||
ttl := schedule.WorkspaceTTL(templateSchedule, ws) | ||
if ttl <= 0 { | ||
return time.Time{} | ||
} | ||
|
||
// If autostart isn't enabled, or the schedule isn't valid/populated we | ||
// don't care about bumping it. | ||
sched, err := schedule.GetWorkspaceAutostartSchedule(templateSchedule, ws) | ||
if err != nil || sched == nil { | ||
return time.Time{} | ||
} | ||
|
||
newDeadline := schedule.MaybeBumpDeadline(sched, build.Deadline, ttl) | ||
if newDeadline.IsZero() { | ||
return time.Time{} | ||
} | ||
if newDeadline.Before(build.Deadline) { | ||
// Don't reduce the deadline. | ||
return time.Time{} | ||
} | ||
if !build.MaxDeadline.IsZero() && newDeadline.After(build.MaxDeadline) { | ||
// Don't exceed the max deadline. | ||
return time.Time{} | ||
} | ||
|
||
return newDeadline | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently we only bump a workspace build if there are a non-zero number of connections. Will this end up bumping workspace deadlines regardless of connection count?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, add a test to make sure it doesn't double bump
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes this will bump regardless of connection count. We want to bump if the build lasts until the next autostart time.