Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
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
Rename state -> lifecycle state
  • Loading branch information
mafredri committed Jan 23, 2023
commit 1e53635ca6ae5ff72e37c7df6f4831e5a1d13425
24 changes: 12 additions & 12 deletions agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ type Client interface {
WorkspaceAgentMetadata(ctx context.Context) (codersdk.WorkspaceAgentMetadata, error)
ListenWorkspaceAgent(ctx context.Context) (net.Conn, error)
AgentReportStats(ctx context.Context, log slog.Logger, stats func() *codersdk.AgentStats) (io.Closer, error)
PostWorkspaceAgentState(ctx context.Context, state codersdk.PostWorkspaceAgentStateRequest) error
PostWorkspaceAgentLifecycle(ctx context.Context, state codersdk.PostWorkspaceAgentLifecycleRequest) error
PostWorkspaceAgentAppHealth(ctx context.Context, req codersdk.PostWorkspaceAppHealthsRequest) error
PostWorkspaceAgentVersion(ctx context.Context, version string) error
}
Expand Down Expand Up @@ -128,8 +128,8 @@ type agent struct {
sessionToken atomic.Pointer[string]
sshServer *ssh.Server

stateMu sync.Mutex // Protects following.
state codersdk.WorkspaceAgentState
lifecycleMu sync.Mutex // Protects following.
lifecycleState codersdk.WorkspaceAgentLifecycle

network *tailnet.Conn
}
Expand Down Expand Up @@ -160,15 +160,15 @@ func (a *agent) runLoop(ctx context.Context) {
}
}

func (a *agent) setState(ctx context.Context, state codersdk.WorkspaceAgentState) {
a.stateMu.Lock()
defer a.stateMu.Unlock()
func (a *agent) setLifecycle(ctx context.Context, state codersdk.WorkspaceAgentLifecycle) {
a.lifecycleMu.Lock()
defer a.lifecycleMu.Unlock()

a.state = state
a.lifecycleState = state

var err error
for r := retry.New(time.Second, 30*time.Second); r.Wait(ctx); {
err = a.client.PostWorkspaceAgentState(ctx, codersdk.PostWorkspaceAgentStateRequest{
err = a.client.PostWorkspaceAgentLifecycle(ctx, codersdk.PostWorkspaceAgentLifecycleRequest{
State: state,
})
if err == nil {
Expand Down Expand Up @@ -224,14 +224,14 @@ func (a *agent) run(ctx context.Context) error {
timeout = t.C
}

a.setState(ctx, codersdk.WorkspaceAgentStateStarting)
a.setLifecycle(ctx, codersdk.WorkspaceAgentLifecycleStarting)

var err error
select {
case err = <-scriptDone:
case <-timeout:
a.logger.Warn(ctx, "startup script timed out")
a.setState(ctx, codersdk.WorkspaceAgentStateStartTimeout)
a.setLifecycle(ctx, codersdk.WorkspaceAgentLifecycleStartTimeout)
err = <-scriptDone // The script can still complete after a timeout.
}
if errors.Is(err, context.Canceled) {
Expand All @@ -240,7 +240,7 @@ func (a *agent) run(ctx context.Context) error {
execTime := time.Since(scriptStart)
if err != nil {
a.logger.Warn(ctx, "startup script failed", slog.F("execution_time", execTime), slog.Error(err))
a.setState(ctx, codersdk.WorkspaceAgentStateStartError)
a.setLifecycle(ctx, codersdk.WorkspaceAgentLifecycleStartError)
return
}
a.logger.Info(ctx, "startup script completed", slog.F("execution_time", execTime))
Expand All @@ -255,7 +255,7 @@ func (a *agent) run(ctx context.Context) error {
}
}

a.setState(ctx, codersdk.WorkspaceAgentStateReady)
a.setLifecycle(ctx, codersdk.WorkspaceAgentLifecycleReady)
}()
}

Expand Down
2 changes: 1 addition & 1 deletion agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ func (c *client) AgentReportStats(ctx context.Context, _ slog.Logger, stats func
}), nil
}

func (*client) PostWorkspaceAgentState(_ context.Context, _ codersdk.PostWorkspaceAgentStateRequest) error {
func (*client) PostWorkspaceAgentLifecycle(_ context.Context, _ codersdk.PostWorkspaceAgentLifecycleRequest) error {
return nil
}

Expand Down
74 changes: 28 additions & 46 deletions coderd/apidoc/docs.go

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

53 changes: 22 additions & 31 deletions coderd/apidoc/swagger.json

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

2 changes: 1 addition & 1 deletion coderd/coderd.go
Original file line number Diff line number Diff line change
Expand Up @@ -541,7 +541,7 @@ func New(options *Options) *API {
r.Get("/gitsshkey", api.agentGitSSHKey)
r.Get("/coordinate", api.workspaceAgentCoordinate)
r.Post("/report-stats", api.workspaceAgentReportStats)
r.Post("/report-state", api.workspaceAgentReportState)
r.Post("/report-lifecycle", api.workspaceAgentReportLifecycle)
})
r.Route("/{workspaceagent}", func(r chi.Router) {
r.Use(
Expand Down
8 changes: 5 additions & 3 deletions coderd/coderdtest/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func AGPLRoutes(a *AuthTester) (map[string]string, map[string]RouteCheck) {
"POST:/api/v2/workspaceagents/me/version": {NoAuthorize: true},
"POST:/api/v2/workspaceagents/me/app-health": {NoAuthorize: true},
"POST:/api/v2/workspaceagents/me/report-stats": {NoAuthorize: true},
"POST:/api/v2/workspaceagents/me/report-state": {NoAuthorize: true},
"POST:/api/v2/workspaceagents/me/report-lifecycle": {NoAuthorize: true},

// These endpoints have more assertions. This is good, add more endpoints to assert if you can!
"GET:/api/v2/organizations/{organization}": {AssertObject: rbac.ResourceOrganization.WithID(a.Admin.OrganizationID).InOrg(a.Admin.OrganizationID)},
Expand Down Expand Up @@ -277,9 +277,11 @@ func AGPLRoutes(a *AuthTester) (map[string]string, map[string]RouteCheck) {
// Routes like proxy routes support all HTTP methods. A helper func to expand
// 1 url to all http methods.
assertAllHTTPMethods := func(url string, check RouteCheck) {
methods := []string{http.MethodGet, http.MethodHead, http.MethodPost,
methods := []string{
http.MethodGet, http.MethodHead, http.MethodPost,
http.MethodPut, http.MethodPatch, http.MethodDelete,
http.MethodConnect, http.MethodOptions, http.MethodTrace}
http.MethodConnect, http.MethodOptions, http.MethodTrace,
}

for _, method := range methods {
route := method + ":" + url
Expand Down
4 changes: 2 additions & 2 deletions coderd/database/databasefake/databasefake.go
Original file line number Diff line number Diff line change
Expand Up @@ -4295,12 +4295,12 @@ func (q *fakeQuerier) GetQuotaConsumedForUser(_ context.Context, userID uuid.UUI
return sum, nil
}

func (q *fakeQuerier) UpdateWorkspaceAgentStateByID(_ context.Context, arg database.UpdateWorkspaceAgentStateByIDParams) error {
func (q *fakeQuerier) UpdateWorkspaceAgentLifecycleStateByID(_ context.Context, arg database.UpdateWorkspaceAgentLifecycleStateByIDParams) error {
q.mutex.Lock()
defer q.mutex.Unlock()
for i, agent := range q.workspaceAgents {
if agent.ID == arg.ID {
agent.State = arg.State
agent.LifecycleState = arg.LifecycleState
q.workspaceAgents[i] = agent
return nil
}
Expand Down
Loading