Skip to content

feat: add lifecycle.Executor to manage autostart and autostop #1183

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 28 commits into from
May 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
a145d6d
feat: add lifecycle.Executor to autostart workspaces.
johnstcn Apr 19, 2022
8f401ca
refactor: do not expose Store in coderdtest.Options
johnstcn Apr 26, 2022
6d8f5fe
fixup! refactor: do not expose Store in coderdtest.Options
johnstcn Apr 26, 2022
cfd0d1e
stop accessing db directly, only query workspaces with autostart enabled
johnstcn Apr 26, 2022
ce63810
refactor unit tests, add tests for autostop
johnstcn Apr 26, 2022
579f362
make the new tests pass with some refactoring
johnstcn Apr 30, 2022
6e88f67
gitignore *.swp
johnstcn Apr 30, 2022
2b1a383
remove unused methods
johnstcn Apr 30, 2022
f31588e
fixup! remove unused methods
johnstcn Apr 30, 2022
80e0581
fix: range over channel, add continue to default switch case
johnstcn May 9, 2022
d176478
add test for deleted workspace
johnstcn May 9, 2022
bd97c1a
workspaces.sql: remove unused methods
johnstcn May 10, 2022
0931d25
unexport test helper methods
johnstcn May 10, 2022
faebe2e
chore: rename package autostart/lifecycle to lifecycle/executor
johnstcn May 10, 2022
abc0854
add test to ensure workspaces are not autostarted before time
johnstcn May 10, 2022
e53946a
wire up executor to coderd
johnstcn May 10, 2022
364a27c
fix: executor: skip workspaces whose last build was not successful
johnstcn May 10, 2022
e96414f
address PR comments
johnstcn May 11, 2022
b5bf50e
add goleak TestMain
johnstcn May 11, 2022
d37cc2b
fmt
johnstcn May 11, 2022
d11f5d7
mustTransitionWorkspace should return the updated workspace
johnstcn May 11, 2022
f6388b4
remove usage of require.Eventually/Never which is flaky on Windows
johnstcn May 11, 2022
fd0f8a3
make lifecycle executor spawn a new goroutine automatically
johnstcn May 11, 2022
7b6f2e1
rename unit tests
johnstcn May 11, 2022
a7143bd
s/doBuild/build
johnstcn May 11, 2022
7d9b696
rename parent package lifecycle to autobuild
johnstcn May 11, 2022
5cba737
add unit test for behaviour with an updated template
johnstcn May 11, 2022
7627372
add ticket to reference TODO
johnstcn May 11, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
stop accessing db directly, only query workspaces with autostart enabled
  • Loading branch information
johnstcn committed May 10, 2022
commit cfd0d1e521eb00c4ef0985fb6fb1bfdb74a26b78
9 changes: 2 additions & 7 deletions coderd/autostart/lifecycle/lifecycle_executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,12 @@ func (e *Executor) Run() error {
func (e *Executor) runOnce(t time.Time) error {
currentTick := t.Round(time.Minute)
return e.db.InTx(func(db database.Store) error {
allWorkspaces, err := db.GetWorkspaces(e.ctx)
autostartWorkspaces, err := db.GetWorkspacesAutostart(e.ctx)
if err != nil {
return xerrors.Errorf("get all workspaces: %w", err)
}

for _, ws := range allWorkspaces {
// We only care about workspaces with autostart enabled.
if ws.AutostartSchedule.String == "" {
continue
}
for _, ws := range autostartWorkspaces {
sched, err := schedule.Weekly(ws.AutostartSchedule.String)
if err != nil {
e.log.Warn(e.ctx, "workspace has invalid autostart schedule",
Expand All @@ -73,7 +69,6 @@ func (e *Executor) runOnce(t time.Time) error {
}

// Determine the workspace state based on its latest build. We expect it to be stopped.
// TODO(cian): is this **guaranteed** to be the latest build???
latestBuild, err := db.GetWorkspaceBuildByWorkspaceIDWithoutAfter(e.ctx, ws.ID)
if err != nil {
return xerrors.Errorf("get latest build for workspace %q: %w", ws.ID, err)
Expand Down
77 changes: 48 additions & 29 deletions coderd/autostart/lifecycle/lifecycle_executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ import (
"testing"
"time"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/slogtest"

"github.com/coder/coder/coderd/autostart/lifecycle"
"github.com/coder/coder/coderd/autostart/schedule"
"github.com/coder/coder/coderd/coderdtest"
"github.com/coder/coder/coderd/database"
"github.com/coder/coder/coderd/database/databasefake"
"github.com/coder/coder/codersdk"

"github.com/stretchr/testify/require"
Expand All @@ -25,15 +20,11 @@ func Test_Executor_Run(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
cancelCtx, cancel = context.WithCancel(context.Background())
log = slogtest.Make(t, nil).Named("lifecycle.executor").Leveled(slog.LevelDebug)
err error
tickCh = make(chan time.Time)
db = databasefake.New()
le = lifecycle.NewExecutor(cancelCtx, db, log, tickCh)
client = coderdtest.New(t, &coderdtest.Options{
Ticker: tickCh,
ctx = context.Background()
err error
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
_ = coderdtest.NewProvisionerDaemon(t, client)
Expand Down Expand Up @@ -71,31 +62,25 @@ func Test_Executor_Run(t *testing.T) {
// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
cancel()
}()
require.NoError(t, le.Run())

// Then: the workspace should be started
require.Eventually(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.Job.Status == codersdk.ProvisionerJobSucceeded &&
ws.LatestBuild.Transition == database.WorkspaceTransitionStart
}, 10*time.Second, 1000*time.Millisecond)
}, 5*time.Second, 250*time.Millisecond)
})

t.Run("AlreadyRunning", func(t *testing.T) {
t.Parallel()

var (
ctx = context.Background()
cancelCtx, cancel = context.WithCancel(context.Background())
log = slogtest.Make(t, nil).Named("lifecycle.executor").Leveled(slog.LevelDebug)
err error
tickCh = make(chan time.Time)
db = databasefake.New()
le = lifecycle.NewExecutor(cancelCtx, db, log, tickCh)
client = coderdtest.New(t, &coderdtest.Options{
Ticker: tickCh,
ctx = context.Background()
err error
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
_ = coderdtest.NewProvisionerDaemon(t, client)
Expand Down Expand Up @@ -123,14 +108,48 @@ func Test_Executor_Run(t *testing.T) {
// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
cancel()
}()
require.NoError(t, le.Run())

// Then: the workspace should not be started.
require.Never(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID != workspace.LatestBuild.ID
}, 10*time.Second, 1000*time.Millisecond)
}, 5*time.Second, 250*time.Millisecond)
})

t.Run("NotEnabled", func(t *testing.T) {
t.Parallel()

var (
tickCh = make(chan time.Time)
client = coderdtest.New(t, &coderdtest.Options{
LifecycleTicker: tickCh,
})
// Given: we have a user with a workspace
_ = coderdtest.NewProvisionerDaemon(t, client)
user = coderdtest.CreateFirstUser(t, client)
version = coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, nil)
template = coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
_ = coderdtest.AwaitTemplateVersionJob(t, client, version.ID)
workspace = coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
_ = coderdtest.AwaitWorkspaceBuildJob(t, client, workspace.LatestBuild.ID)
)

// Given: we ensure the workspace is now in a stopped state
require.Equal(t, database.WorkspaceTransitionStart, workspace.LatestBuild.Transition)

// Given: the workspace has autostart disabled
require.Empty(t, workspace.AutostartSchedule)

// When: the lifecycle executor ticks
go func() {
tickCh <- time.Now().UTC().Add(time.Minute)
}()

// Then: the workspace should not be started.
require.Never(t, func() bool {
ws := coderdtest.MustWorkspace(t, client, workspace.ID)
return ws.LatestBuild.ID != workspace.LatestBuild.ID
}, 5*time.Second, 250*time.Millisecond)
})
}
12 changes: 6 additions & 6 deletions coderd/coderdtest/coderdtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ type Options struct {
GoogleTokenValidator *idtoken.Validator
SSHKeygenAlgorithm gitsshkey.Algorithm
APIRateLimit int
Ticker <-chan time.Time
LifecycleTicker <-chan time.Time
}

// New constructs an in-memory coderd instance and returns
Expand All @@ -75,10 +75,10 @@ func New(t *testing.T, options *Options) *codersdk.Client {
options.GoogleTokenValidator, err = idtoken.NewValidator(ctx, option.WithoutAuthentication())
require.NoError(t, err)
}
if options.Ticker == nil {
ticker := time.NewTicker(time.Second)
options.Ticker = ticker.C
t.Cleanup(ticker.Stop)
if options.LifecycleTicker == nil {
ticker := make(chan time.Time)
options.LifecycleTicker = ticker
t.Cleanup(func() { close(ticker) })
}

// This can be hotswapped for a live database instance.
Expand Down Expand Up @@ -109,7 +109,7 @@ func New(t *testing.T, options *Options) *codersdk.Client {
ctx,
db,
slogtest.Make(t, nil).Named("lifecycle.executor").Leveled(slog.LevelDebug),
options.Ticker,
options.LifecycleTicker,
)
go lifecycleExecutor.Run()
Copy link
Member

Choose a reason for hiding this comment

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

Since this Run function is always called in a goroutine, could we do that automatically for the caller in New()?


Expand Down
12 changes: 12 additions & 0 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,18 @@ func (q *fakeQuerier) GetWorkspaces(_ context.Context) ([]database.Workspace, er
return workspaces, nil
}

func (q *fakeQuerier) GetWorkspacesAutostart(_ context.Context) ([]database.Workspace, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
workspaces := make([]database.Workspace, 0)
for _, ws := range q.workspaces {
if ws.AutostartSchedule.String != "" {
workspaces = append(workspaces, ws)
}
}
return workspaces, nil
}

func (q *fakeQuerier) GetWorkspaceOwnerCountsByTemplateIDs(_ context.Context, templateIDs []uuid.UUID) ([]database.GetWorkspaceOwnerCountsByTemplateIDsRow, error) {
q.mutex.RLock()
defer q.mutex.RUnlock()
Expand Down
2 changes: 0 additions & 2 deletions coderd/database/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions coderd/database/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 45 additions & 2 deletions coderd/database/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions coderd/database/queries/workspaces.sql
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,17 @@ FROM
WHERE
deleted = false;

-- name: GetWorkspacesAutostart :many
SELECT
*
FROM
workspaces
WHERE
deleted = false
AND
autostart_schedule <> ''
;

-- name: GetWorkspacesByTemplateID :many
SELECT
*
Expand Down