Skip to content
Merged
Prev Previous commit
Next Next commit
address PR comments
  • Loading branch information
johnstcn committed Jun 16, 2022
commit 81b2656bef1284a8d2fd0796f84d6444bffaab8a
6 changes: 3 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ site/src/api/typesGenerated.ts: scripts/apitypings/main.go $(shell find codersdk

.PHONY: test
test: test-clean
gotestsum -- -v -short ./... -- -timeout=5m
gotestsum -- -v -short ./...

.PHONY: test-postgres
test-postgres: test-clean
DB=ci gotestsum --junitfile="gotests.xml" --packages="./..." -- \
-covermode=atomic -coverprofile="gotests.coverage" -timeout=10m \
-covermode=atomic -coverprofile="gotests.coverage" -timeout=5m \
-coverpkg=./...,github.com/coder/coder/codersdk \
-count=1 -parallel=2 -race -failfast
-count=1 -parallel=1 -race -failfast


.PHONY: test-postgres-docker
Expand Down
52 changes: 23 additions & 29 deletions cli/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"io"
"time"

"github.com/jedib0t/go-pretty/v6/table"
"github.com/spf13/cobra"
"golang.org/x/xerrors"

Expand All @@ -13,27 +14,22 @@ import (
"github.com/coder/coder/coderd/util/ptr"
"github.com/coder/coder/coderd/util/tz"
"github.com/coder/coder/codersdk"

"github.com/jedib0t/go-pretty/v6/table"
)

const (
scheduleDescriptionLong = `
Modify scheduled stop and start times for your workspace:
scheduleDescriptionLong = `Modify scheduled stop and start times for your workspace:
* schedule show: show workspace schedule
* schedule start: edit workspace start schedule
* schedule stop: edit workspace stop schedule
* schedule override: edit stop time of active workspace
* schedule override-stop: edit stop time of active workspace
`
scheduleShowDescriptionLong = `
Shows the following information for the given workspace:
* The workspace's automatic start schedule
scheduleShowDescriptionLong = `Shows the following information for the given workspace:
* The automatic start schedule
* The next scheduled start time
* The duration after which the workspace will stop
* The next scheduled stop time of the workspace.
* The duration after which it will stop
* The next scheduled stop time
`
scheduleStartDescriptionLong = `
Schedules a workspace to regularly start at a specific time.
scheduleStartDescriptionLong = `Schedules a workspace to regularly start at a specific time.
Schedule format: <start-time> [day-of-week] [location].
* Start-time (required) is accepted either in 12-hour (hh:mm{am|pm}) format, or 24-hour format hh:mm.
* Day-of-week (optional) allows specifying in the cron format, e.g. 1,3,5 or Mon-Fri.
Expand All @@ -43,8 +39,7 @@ Schedule format: <start-time> [day-of-week] [location].
If omitted, we will fall back to either the TZ environment variable or /etc/localtime.
You can check your corresponding location by visiting https://ipinfo.io - it shows in the demo widget on the right.
`
scheduleStopDescriptionLong = `
Schedules a workspace to stop after a given duration has elapsed.
scheduleStopDescriptionLong = `Schedules a workspace to stop after a given duration has elapsed.
* Workspace runtime is measured from the time that the workspace build completed.
* The minimum scheduled stop time is 1 minute.
* The workspace template may place restrictions on the maximum shutdown time.
Expand All @@ -57,8 +52,7 @@ When enabling scheduled stop, enter a duration in one of the following formats:
* 2m (2 minutes)
* 2 (2 minutes)
`
scheduleOverrideDescriptionLong = `
Override the stop time of a currently active workspace instance.
scheduleOverrideDescriptionLong = `Override the stop time of a currently running workspace instance.
* The new stop time is calculated from *now*.
* The new stop time must be at least 30 minutes in the future.
* The workspace template may restrict the maximum workspace runtime.
Expand All @@ -69,7 +63,7 @@ func schedules() *cobra.Command {
scheduleCmd := &cobra.Command{
Annotations: workspaceCommand,
Use: "schedule { show | start | stop | override } <workspace>",
Short: "Modify scheduled stop and start times for your workspace.",
Short: "Modify scheduled stop and start times for your workspace",
Long: scheduleDescriptionLong,
}

Expand All @@ -84,7 +78,7 @@ func schedules() *cobra.Command {
func scheduleShow() *cobra.Command {
showCmd := &cobra.Command{
Annotations: workspaceCommand,
Use: "show <workspace_name>",
Use: "show <workspace-name>",
Short: "Show workspace schedule",
Long: scheduleShowDescriptionLong,
Args: cobra.ExactArgs(1),
Expand All @@ -108,7 +102,7 @@ func scheduleShow() *cobra.Command {
func scheduleStart() *cobra.Command {
cmd := &cobra.Command{
Annotations: workspaceCommand,
Use: "start <workspace_name> { <start-time> [day-of-week] [location] | manual }",
Use: "start <workspace-name> { <start-time> [day-of-week] [location] | manual }",
Example: `start my-workspace 9:30AM Mon-Fri Europe/Dublin`,
Short: "Edit workspace start schedule",
Long: scheduleStartDescriptionLong,
Expand Down Expand Up @@ -156,7 +150,7 @@ func scheduleStop() *cobra.Command {
return &cobra.Command{
Annotations: workspaceCommand,
Args: cobra.ExactArgs(2),
Use: "stop <workspace_name> { <duration> | manual }",
Use: "stop <workspace-name> { <duration> | manual }",
Example: `stop my-workspace 2h30m`,
Short: "Edit workspace stop schedule",
Long: scheduleStopDescriptionLong,
Copy link
Member

Choose a reason for hiding this comment

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

all great help 👍

Expand Down Expand Up @@ -199,12 +193,12 @@ func scheduleOverride() *cobra.Command {
overrideCmd := &cobra.Command{
Args: cobra.ExactArgs(2),
Annotations: workspaceCommand,
Use: "override <workspace-name> <duration from now>",
Example: "override my-workspace 90m",
Use: "override-stop <workspace-name> <duration from now>",
Example: "override-stop my-workspace 90m",
Short: "Edit stop time of active workspace",
Long: scheduleOverrideDescriptionLong,
RunE: func(cmd *cobra.Command, args []string) error {
bumpDuration, err := parseDuration(args[1])
overrideDuration, err := parseDuration(args[1])
if err != nil {
return err
}
Expand All @@ -224,15 +218,15 @@ func scheduleOverride() *cobra.Command {
loc = time.UTC // best effort
Copy link
Member

Choose a reason for hiding this comment

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

Yeah, we tried... ❤️

}

if bumpDuration < 29*time.Minute {
if overrideDuration < 29*time.Minute {
_, _ = fmt.Fprintf(
cmd.OutOrStdout(),
"Please specify a duration of at least 30 minutes.\n",
)
return nil
}

newDeadline := time.Now().In(loc).Add(bumpDuration)
newDeadline := time.Now().In(loc).Add(overrideDuration)
if err := client.PutExtendWorkspace(cmd.Context(), workspace.ID, codersdk.PutExtendWorkspaceRequest{
Deadline: newDeadline,
}); err != nil {
Expand Down Expand Up @@ -288,10 +282,10 @@ func displaySchedule(workspace codersdk.Workspace, out io.Writer) error {
}

tw := cliui.Table()
tw.AppendRow(table.Row{"Starts at", ":", schedStart})
tw.AppendRow(table.Row{"Starts next", ":", schedNextStart})
tw.AppendRow(table.Row{"Stops at", ":", schedStop})
tw.AppendRow(table.Row{"Stops next", ":", schedNextStop})
tw.AppendRow(table.Row{"Starts at", schedStart})
tw.AppendRow(table.Row{"Starts next", schedNextStart})
tw.AppendRow(table.Row{"Stops at", schedStop})
tw.AppendRow(table.Row{"Stops next", schedNextStop})

_, _ = fmt.Fprintln(out, tw.Render())
return nil
Expand Down
48 changes: 23 additions & 25 deletions cli/schedule_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import (
"testing"
"time"

"github.com/coder/coder/coderd/database"

"github.com/stretchr/testify/assert"

"github.com/stretchr/testify/require"

"github.com/coder/coder/cli/clitest"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/util/ptr"
"github.com/coder/coder/codersdk"
)
Expand Down Expand Up @@ -52,10 +50,10 @@ func TestScheduleShow(t *testing.T) {
require.NoError(t, err, "unexpected error")
lines := strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
if assert.Len(t, lines, 4) {
assert.Contains(t, lines[0], "Starts at : 7:30AM Mon-Fri (Europe/Dublin)")
assert.Contains(t, lines[1], "Starts next : 7:30AM IST on ")
assert.Contains(t, lines[2], "Stops at : 8h after start")
assert.NotContains(t, lines[3], "Stops next : -")
assert.Contains(t, lines[0], "Starts at 7:30AM Mon-Fri (Europe/Dublin)")
assert.Contains(t, lines[1], "Starts next 7:30AM IST on ")
assert.Contains(t, lines[2], "Stops at 8h after start")
assert.NotContains(t, lines[3], "Stops next -")
}
})

Expand Down Expand Up @@ -87,10 +85,10 @@ func TestScheduleShow(t *testing.T) {
require.NoError(t, err, "unexpected error")
lines := strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
if assert.Len(t, lines, 4) {
assert.Contains(t, lines[0], "Starts at : manual")
assert.Contains(t, lines[1], "Starts next : -")
assert.Contains(t, lines[2], "Stops at : manual")
assert.Contains(t, lines[3], "Stops next : -")
assert.Contains(t, lines[0], "Starts at manual")
assert.Contains(t, lines[1], "Starts next -")
assert.Contains(t, lines[2], "Stops at manual")
assert.Contains(t, lines[3], "Stops next -")
}
})

Expand Down Expand Up @@ -140,8 +138,8 @@ func TestScheduleStart(t *testing.T) {
assert.NoError(t, err, "unexpected error")
lines := strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
if assert.Len(t, lines, 4) {
assert.Contains(t, lines[0], "Starts at : 9:30AM Mon-Fri (Europe/Dublin)")
assert.Contains(t, lines[1], "Starts next : 9:30AM IST on")
assert.Contains(t, lines[0], "Starts at 9:30AM Mon-Fri (Europe/Dublin)")
assert.Contains(t, lines[1], "Starts next 9:30AM IST on")
}

// Ensure autostart schedule updated
Expand All @@ -161,8 +159,8 @@ func TestScheduleStart(t *testing.T) {
assert.NoError(t, err, "unexpected error")
lines = strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
if assert.Len(t, lines, 4) {
assert.Contains(t, lines[0], "Starts at : manual")
assert.Contains(t, lines[1], "Starts next : -")
assert.Contains(t, lines[0], "Starts at manual")
assert.Contains(t, lines[1], "Starts next -")
}
}

Expand Down Expand Up @@ -190,9 +188,9 @@ func TestScheduleStop(t *testing.T) {
assert.NoError(t, err, "unexpected error")
lines := strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
if assert.Len(t, lines, 4) {
assert.Contains(t, lines[2], "Stops at : 8h30m after start")
assert.Contains(t, lines[2], "Stops at 8h30m after start")
// Should not be manual
assert.NotContains(t, lines[3], "Stops next -:")
assert.NotContains(t, lines[3], "Stops next -")
}

// Reset stdout
Expand All @@ -207,9 +205,9 @@ func TestScheduleStop(t *testing.T) {
assert.NoError(t, err, "unexpected error")
lines = strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
if assert.Len(t, lines, 4) {
assert.Contains(t, lines[2], "Stops at : manual")
assert.Contains(t, lines[2], "Stops at manual")
// Deadline of a running workspace is not updated.
assert.NotContains(t, lines[3], "Stops next : -")
assert.NotContains(t, lines[3], "Stops next -")
}
}

Expand All @@ -229,7 +227,7 @@ func TestScheduleOverride(t *testing.T) {
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
cmdArgs = []string{"schedule", "override", workspace.Name, "10h"}
cmdArgs = []string{"schedule", "override-stop", workspace.Name, "10h"}
stdoutBuf = &bytes.Buffer{}
)

Expand Down Expand Up @@ -270,7 +268,7 @@ func TestScheduleOverride(t *testing.T) {
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
project = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID)
cmdArgs = []string{"schedule", "override", workspace.Name, "kwyjibo"}
cmdArgs = []string{"schedule", "override-stop", workspace.Name, "kwyjibo"}
stdoutBuf = &bytes.Buffer{}
)

Expand Down Expand Up @@ -308,7 +306,7 @@ func TestScheduleOverride(t *testing.T) {
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, project.ID, func(cwr *codersdk.CreateWorkspaceRequest) {
cwr.TTLMillis = nil
})
cmdArgs = []string{"schedule", "override", workspace.Name, "1h"}
cmdArgs = []string{"schedule", "override-stop", workspace.Name, "1h"}
stdoutBuf = &bytes.Buffer{}
)
// Unset the workspace TTL
Expand Down Expand Up @@ -371,8 +369,8 @@ func TestScheduleStartDefaults(t *testing.T) {
require.NoError(t, err, "unexpected error")
lines := strings.Split(strings.TrimSpace(stdoutBuf.String()), "\n")
if assert.Len(t, lines, 4) {
assert.Contains(t, lines[0], "Starts at : 9:30AM daily (UTC)")
assert.Contains(t, lines[1], "Starts next : 9:30AM UTC on")
assert.Contains(t, lines[2], "Stops at : 8h after start")
assert.Contains(t, lines[0], "Starts at 9:30AM daily (UTC)")
assert.Contains(t, lines[1], "Starts next 9:30AM UTC on")
assert.Contains(t, lines[2], "Stops at 8h after start")
}
}