mirror of
https://github.com/coder/coder.git
synced 2026-09-24 15:04:27 +08:00
fix: coderd: decouple ttl and deadline (#2282)
This commit makes the following changes: - Partially reverts the changes of feat: update workspace deadline when workspace ttl updated #2165, making the deadline of a running workspace build independant of TTL, once started. - CLI: updating a workspace TTL no longer updates the deadline of the workspace. - UI: updating a workspace TTL no longer updates the deadline of the workspace. - Drive-by: API: When creating a workspace, default TTL to min(12 hours, template max_ttl) if not instructed otherwise. - Drive-by: CLI: list: measure workspace extension correctly (+X in last column) from the time the provisioner job was completed - Drive-by: WorkspaceSchedule: show timezone of schedule if it is set, defaulting to dayjs guess otherwise. - Drive-by: WorkspaceScheduleForm: fixed an issue where deleting the "TTL" value in the form would show the text "Your workspace will shut down a few seconds after start".
This commit is contained in:
+3
-2
@@ -58,8 +58,9 @@ func bump() *cobra.Command {
|
||||
|
||||
_, _ = fmt.Fprintf(
|
||||
cmd.OutOrStdout(),
|
||||
"Workspace %q will now stop at %s\n", workspace.Name,
|
||||
newDeadline.Format(time.RFC822),
|
||||
"Workspace %q will now stop at %s on %s\n", workspace.Name,
|
||||
newDeadline.Format(timeFormat),
|
||||
newDeadline.Format(dateFormat),
|
||||
)
|
||||
|
||||
return nil
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
package cli
|
||||
|
||||
const (
|
||||
timeFormat = "3:04:05 PM MST"
|
||||
dateFormat = "Jan 2, 2006"
|
||||
)
|
||||
+4
-1
@@ -127,6 +127,9 @@ func hasExtension(ws codersdk.Workspace) (bool, time.Duration) {
|
||||
if ws.LatestBuild.Transition != codersdk.WorkspaceTransitionStart {
|
||||
return false, 0
|
||||
}
|
||||
if ws.LatestBuild.Job.CompletedAt == nil {
|
||||
return false, 0
|
||||
}
|
||||
if ws.LatestBuild.Deadline.IsZero() {
|
||||
return false, 0
|
||||
}
|
||||
@@ -134,7 +137,7 @@ func hasExtension(ws codersdk.Workspace) (bool, time.Duration) {
|
||||
return false, 0
|
||||
}
|
||||
ttl := time.Duration(*ws.TTLMillis) * time.Millisecond
|
||||
delta := ws.LatestBuild.Deadline.Add(-ttl).Sub(ws.LatestBuild.CreatedAt)
|
||||
delta := ws.LatestBuild.Deadline.Add(-ttl).Sub(*ws.LatestBuild.Job.CompletedAt)
|
||||
if delta < time.Minute {
|
||||
return false, 0
|
||||
}
|
||||
|
||||
+21
-41
@@ -1,14 +1,14 @@
|
||||
package cli
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"golang.org/x/xerrors"
|
||||
|
||||
"github.com/coder/coder/cli/cliui"
|
||||
"github.com/coder/coder/coderd/autobuild/schedule"
|
||||
"github.com/coder/coder/coderd/util/ptr"
|
||||
"github.com/coder/coder/codersdk"
|
||||
)
|
||||
|
||||
@@ -91,30 +91,6 @@ func ttlset() *cobra.Command {
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "warning: ttl rounded down to %s\n", truncated)
|
||||
}
|
||||
|
||||
if changed, newDeadline := changedNewDeadline(workspace, truncated); changed {
|
||||
// For the purposes of the user, "less than a minute" is essentially the same as "immediately".
|
||||
timeRemaining := time.Until(newDeadline).Truncate(time.Minute)
|
||||
humanRemaining := "in " + timeRemaining.String()
|
||||
if timeRemaining <= 0 {
|
||||
humanRemaining = "immediately"
|
||||
}
|
||||
_, err = cliui.Prompt(cmd, cliui.PromptOptions{
|
||||
Text: fmt.Sprintf(
|
||||
"Workspace %q will be stopped %s. Are you sure?",
|
||||
workspace.Name,
|
||||
humanRemaining,
|
||||
),
|
||||
Default: "yes",
|
||||
IsConfirm: true,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, cliui.Canceled) {
|
||||
return nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
millis := truncated.Milliseconds()
|
||||
if err = client.UpdateWorkspaceTTL(cmd.Context(), workspace.ID, codersdk.UpdateWorkspaceTTLRequest{
|
||||
TTLMillis: &millis,
|
||||
@@ -122,6 +98,25 @@ func ttlset() *cobra.Command {
|
||||
return xerrors.Errorf("update workspace ttl: %w", err)
|
||||
}
|
||||
|
||||
if ptr.NilOrEmpty(workspace.AutostartSchedule) {
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%q will shut down %s after start.\n", workspace.Name, truncated)
|
||||
return nil
|
||||
}
|
||||
|
||||
sched, err := schedule.Weekly(*workspace.AutostartSchedule)
|
||||
if err != nil {
|
||||
return xerrors.Errorf("parse workspace schedule: %w", err)
|
||||
}
|
||||
|
||||
nextShutdown := sched.Next(time.Now()).Add(truncated).In(sched.Location())
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%q will shut down at %s on %s (%s after start).\n",
|
||||
workspace.Name,
|
||||
nextShutdown.Format(timeFormat),
|
||||
nextShutdown.Format(dateFormat),
|
||||
truncated,
|
||||
)
|
||||
|
||||
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "NOTE: this will only take effect the next time the workspace is started.\n")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
@@ -157,18 +152,3 @@ func ttlunset() *cobra.Command {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func changedNewDeadline(ws codersdk.Workspace, newTTL time.Duration) (changed bool, newDeadline time.Time) {
|
||||
if ws.LatestBuild.Transition != codersdk.WorkspaceTransitionStart {
|
||||
// not running
|
||||
return false, newDeadline
|
||||
}
|
||||
|
||||
if ws.LatestBuild.Job.CompletedAt == nil {
|
||||
// still building
|
||||
return false, newDeadline
|
||||
}
|
||||
|
||||
newDeadline = ws.LatestBuild.Job.CompletedAt.Add(newTTL)
|
||||
return true, newDeadline
|
||||
}
|
||||
|
||||
@@ -3,7 +3,6 @@ package cli_test
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -109,9 +108,6 @@ func TestTTL(t *testing.T) {
|
||||
assert.NoError(t, err, "unexpected error")
|
||||
}()
|
||||
|
||||
pty.ExpectMatch(fmt.Sprintf("warning: ttl rounded down to %s", ttl.Truncate(time.Minute)))
|
||||
pty.ExpectMatch(fmt.Sprintf("Workspace %q will be stopped in 8h29m0s. Are you sure?", workspace.Name))
|
||||
pty.WriteLine("yes")
|
||||
// Ensure ttl updated
|
||||
updated, err := client.Workspace(ctx, workspace.ID)
|
||||
require.NoError(t, err, "fetch updated workspace")
|
||||
|
||||
Reference in New Issue
Block a user