-
Notifications
You must be signed in to change notification settings - Fork 894
chore: executor_test: reduce test execution time #1876
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 4 commits
a40a41a
b0260d7
05e4c5e
0d96b34
4314760
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 |
---|---|---|
|
@@ -17,10 +17,18 @@ import ( | |
|
||
// Executor automatically starts or stops workspaces. | ||
type Executor struct { | ||
ctx context.Context | ||
db database.Store | ||
log slog.Logger | ||
tick <-chan time.Time | ||
ctx context.Context | ||
db database.Store | ||
log slog.Logger | ||
tick <-chan time.Time | ||
statsCh chan<- RunStats | ||
} | ||
|
||
// RunStats contains information about one run of Executor. | ||
type RunStats struct { | ||
Transitions map[uuid.UUID]database.WorkspaceTransition | ||
Elapsed time.Duration | ||
Error error | ||
} | ||
|
||
// New returns a new autobuild executor. | ||
|
@@ -34,22 +42,42 @@ func New(ctx context.Context, db database.Store, log slog.Logger, tick <-chan ti | |
return le | ||
} | ||
|
||
// WithStatsChannel will cause Executor to push a RunStats to ch after | ||
// every tick. | ||
func (e *Executor) WithStatsChannel(ch chan<- RunStats) *Executor { | ||
e.statsCh = ch | ||
return e | ||
} | ||
|
||
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. Should we always do this? 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. eg. maybe we could remove this and just make it the default behavior. 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. I don't have any major use-case for consuming these outside of the unit tests, hence the toggle. 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. Fair enough. I think it's fine for now! |
||
// Run will cause executor to start or stop workspaces on every | ||
// tick from its channel. It will stop when its context is Done, or when | ||
// its channel is closed. | ||
func (e *Executor) Run() { | ||
go func() { | ||
for t := range e.tick { | ||
if err := e.runOnce(t); err != nil { | ||
e.log.Error(e.ctx, "error running once", slog.Error(err)) | ||
stats := e.runOnce(t) | ||
if stats.Error != nil { | ||
e.log.Error(e.ctx, "error running once", slog.Error(stats.Error)) | ||
} | ||
if e.statsCh != nil { | ||
e.statsCh <- stats | ||
} | ||
e.log.Debug(e.ctx, "run stats", slog.F("elapsed", stats.Elapsed), slog.F("transitions", stats.Transitions)) | ||
} | ||
}() | ||
} | ||
|
||
func (e *Executor) runOnce(t time.Time) error { | ||
func (e *Executor) runOnce(t time.Time) RunStats { | ||
var err error | ||
stats := RunStats{ | ||
Transitions: make(map[uuid.UUID]database.WorkspaceTransition), | ||
} | ||
defer func() { | ||
stats.Elapsed = time.Since(t) | ||
stats.Error = err | ||
}() | ||
currentTick := t.Truncate(time.Minute) | ||
return e.db.InTx(func(db database.Store) error { | ||
err = 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 | ||
|
@@ -146,6 +174,7 @@ func (e *Executor) runOnce(t time.Time) error { | |
slog.F("transition", validTransition), | ||
) | ||
|
||
stats.Transitions[ws.ID] = validTransition | ||
if err := build(e.ctx, db, ws, validTransition, priorHistory, priorJob); err != nil { | ||
e.log.Error(e.ctx, "unable to transition workspace", | ||
slog.F("workspace_id", ws.ID), | ||
|
@@ -156,6 +185,7 @@ func (e *Executor) runOnce(t time.Time) error { | |
} | ||
return nil | ||
}) | ||
return stats | ||
} | ||
|
||
// TODO(cian): this function duplicates most of api.postWorkspaceBuilds. Refactor. | ||
|
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.
We could probably call this
Stats
. Sinceexecutor.Executor
is the primary export of this package, it seems reasonableexecutor.Stats
would attach to it.