-
Notifications
You must be signed in to change notification settings - Fork 902
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
Changes from 17 commits
a145d6d
8f401ca
6d8f5fe
cfd0d1e
ce63810
579f362
6e88f67
2b1a383
f31588e
80e0581
d176478
bd97c1a
0931d25
faebe2e
abc0854
e53946a
364a27c
e96414f
b5bf50e
d37cc2b
d11f5d7
f6388b4
fd0f8a3
7b6f2e1
a7143bd
7d9b696
5cba737
7627372
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -37,3 +37,4 @@ site/out/ | |
.terraform/ | ||
|
||
.vscode/*.log | ||
**/*.swp |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -41,6 +41,7 @@ import ( | |
"github.com/coder/coder/coderd/database/databasefake" | ||
"github.com/coder/coder/coderd/database/postgres" | ||
"github.com/coder/coder/coderd/gitsshkey" | ||
"github.com/coder/coder/coderd/lifecycle/executor" | ||
"github.com/coder/coder/coderd/turnconn" | ||
"github.com/coder/coder/codersdk" | ||
"github.com/coder/coder/cryptorand" | ||
|
@@ -57,6 +58,7 @@ type Options struct { | |
GoogleTokenValidator *idtoken.Validator | ||
SSHKeygenAlgorithm gitsshkey.Algorithm | ||
APIRateLimit int | ||
LifecycleTicker <-chan time.Time | ||
} | ||
|
||
// New constructs an in-memory coderd instance and returns | ||
|
@@ -72,6 +74,11 @@ func New(t *testing.T, options *Options) *codersdk.Client { | |
options.GoogleTokenValidator, err = idtoken.NewValidator(ctx, option.WithoutAuthentication()) | ||
require.NoError(t, err) | ||
} | ||
if options.LifecycleTicker == nil { | ||
ticker := make(chan time.Time) | ||
options.LifecycleTicker = ticker | ||
t.Cleanup(func() { close(ticker) }) | ||
johnstcn marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
// This can be hotswapped for a live database instance. | ||
db := databasefake.New() | ||
|
@@ -96,8 +103,16 @@ func New(t *testing.T, options *Options) *codersdk.Client { | |
}) | ||
} | ||
|
||
srv := httptest.NewUnstartedServer(nil) | ||
ctx, cancelFunc := context.WithCancel(context.Background()) | ||
lifecycleExecutor := executor.New( | ||
ctx, | ||
db, | ||
slogtest.Make(t, nil).Named("lifecycle.executor").Leveled(slog.LevelDebug), | ||
options.LifecycleTicker, | ||
) | ||
go lifecycleExecutor.Run() | ||
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. Since this |
||
|
||
srv := httptest.NewUnstartedServer(nil) | ||
srv.Config.BaseContext = func(_ net.Listener) context.Context { | ||
return ctx | ||
} | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
Comment(general): Your code looks great. I'm feeling a bit lost in understanding all that's happening in this
"Start a Coder server"
RunE
because it's growing quite large with some branching and Goroutines. I'm wondering if that is just a me thing, or if maybe the complexity here is growing large. To understand what I mean, this diff in isolation looks harmless. In the grand scheme of initializing everything within thisRunE
, though, it's harder to really evaluate.I'm under the impression we could extract out some initialization steps to named functions and call them in a pipeline, but maybe that's just a me thing and not the general preferred approach for Go + Cobra. Do you have any comments to that effect?
Regardless, this comment is not meant for your code/this PR, just a general observation.
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.
Agreed, I'd be in favour of extracting some initialization logic to make things slightly more concise, just would want to be careful to avoid too many layers of indirection.