mirror of
https://github.com/coder/coder.git
synced 2025-07-15 22:20:27 +00:00
feat: add API/SDK support for autostop extension (#1778)
* Adds deadline column to workspace_builds, associated DB/API plumbing * database: Upon inserting a row into workspace_builds, deadline will initially be zero. * autobuild: Executor now checks the Deadline field of the workspace_build for the purpose of autostop logic. * coderd: Adds a new route /api/v2/workspaces/:workspace/extend which allows updating the deadline of the currently active workspace build. The new deadline must be after the existing deadline, and not the zero time. * provisionerd: updates workspace_build.deadline upon successful workspace build completion (equal to now plus workspace TTL, if it exists).
This commit is contained in:
@ -50,6 +50,18 @@ func (e *Executor) Run() {
|
||||
func (e *Executor) runOnce(t time.Time) error {
|
||||
currentTick := t.Truncate(time.Minute)
|
||||
return e.db.InTx(func(db database.Store) error {
|
||||
// TTL is set at the workspace level, and deadline at the workspace build level.
|
||||
// When a workspace build is created, its deadline initially starts at zero.
|
||||
// When provisionerd successfully completes a provision job, the deadline is
|
||||
// set to now + TTL if the associated workspace has a TTL set. This deadline
|
||||
// is what we compare against when performing autostop operations, rounded down
|
||||
// to the minute.
|
||||
//
|
||||
// NOTE: Currently, 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. So, autostop will still happen at the
|
||||
// original TTL value from when the workspace build was created.
|
||||
// Whether this is expected behavior from a user's perspective is not yet known.
|
||||
eligibleWorkspaces, err := db.GetWorkspacesAutostart(e.ctx)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("get eligible workspaces for autostart or autostop: %w", err)
|
||||
@ -88,18 +100,15 @@ func (e *Executor) runOnce(t time.Time) error {
|
||||
switch priorHistory.Transition {
|
||||
case database.WorkspaceTransitionStart:
|
||||
validTransition = database.WorkspaceTransitionStop
|
||||
if !ws.Ttl.Valid || ws.Ttl.Int64 == 0 {
|
||||
e.log.Debug(e.ctx, "invalid or zero ws ttl, skipping",
|
||||
if priorHistory.Deadline.IsZero() {
|
||||
e.log.Debug(e.ctx, "latest workspace build has zero deadline, skipping",
|
||||
slog.F("workspace_id", ws.ID),
|
||||
slog.F("ttl", time.Duration(ws.Ttl.Int64)),
|
||||
slog.F("workspace_build_id", priorHistory.ID),
|
||||
)
|
||||
continue
|
||||
}
|
||||
ttl := time.Duration(ws.Ttl.Int64)
|
||||
// Measure TTL from the time the workspace finished building.
|
||||
// Truncate to nearest minute for consistency with autostart
|
||||
// behavior, and add one minute for padding.
|
||||
nextTransition = priorHistory.UpdatedAt.Truncate(time.Minute).Add(ttl + time.Minute)
|
||||
// Truncate to nearest minute for consistency with autostart behavior
|
||||
nextTransition = priorHistory.Deadline.Truncate(time.Minute)
|
||||
case database.WorkspaceTransitionStop:
|
||||
validTransition = database.WorkspaceTransitionStart
|
||||
sched, err := schedule.Weekly(ws.AutostartSchedule.String)
|
||||
|
Reference in New Issue
Block a user