Skip to content
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

DEVPROD-13572 Update agent to end after running single task host task #8789

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
8 changes: 7 additions & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type Options struct {
// sent to the global agent file log.
SendTaskLogsToGlobalSender bool
HomeDirectory string
SingleTaskDistro bool
}

// AddLoggableInfo is a helper to add relevant information about the agent
Expand Down Expand Up @@ -287,7 +288,12 @@ func (a *Agent) loop(ctx context.Context) error {
if ntr.tc != nil {
tc = ntr.tc
}

// Single task distros should exit after running a single task.
// However, if the task group needs tearing down, we should continue
// the loop so the teardown group can run in the next iteration.
if !needTeardownGroup && a.opts.SingleTaskDistro {
return a.comm.DisableHost(ctx, a.opts.HostID, apimodels.DisableInfo{Reason: "Single task distro ran a task"})
}
if ntr.shouldExit {
return nil
}
Expand Down
11 changes: 11 additions & 0 deletions agent/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,17 @@ func (s *AgentSuite) TestAgentEndTaskShouldExit() {
s.Empty(endDetail.FailingCommand, "should not include end task failing command for successful task")
}

func (s *AgentSuite) TestAgentExitsSingleTaskDistros() {
s.setupRunTask(defaultProjYml)
s.mockCommunicator.EndTaskResponse = &apimodels.EndTaskResponse{}
s.a.opts.SingleTaskDistro = true
ctx, cancel := context.WithTimeout(s.ctx, 5*time.Second)
defer cancel()

// The loop should exit after one execution because it is on a single host distro
s.NoError(s.a.loop(ctx))
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we test if it only looped once somehow?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so

there isnt a super good test for the agents working in general 😂 because most of them checks that we break under certain conditions

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah I thought so. I had some ideas of having it run a command and the command modifies a test variable but we don't have anything like that setup and it's non-trivial to do. I'm honestly surprised we haven't had a test command like this yet. This tests looks good to me then!

}

func (s *AgentSuite) TestFinishTaskWithNormalCompletedTask() {
s.mockCommunicator.EndTaskResponse = &apimodels.EndTaskResponse{}

Expand Down
2 changes: 1 addition & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ var (

// Agent version to control agent rollover. The format is the calendar date
// (YYYY-MM-DD).
AgentVersion = "2025-03-11-b"
AgentVersion = "2025-03-11-c"
)

const (
Expand Down
7 changes: 6 additions & 1 deletion model/host/hostutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ func (h *Host) AgentCommand(settings *evergreen.Settings, executablePath string)
if executablePath == "" {
executablePath = h.Distro.AbsPathCygwinCompatible(h.Distro.HomeDir(), h.Distro.BinaryName())
}
return []string{
args := []string{
executablePath,
"agent",
fmt.Sprintf("--api_server=%s", settings.Api.URL),
Expand All @@ -1033,6 +1033,11 @@ func (h *Host) AgentCommand(settings *evergreen.Settings, executablePath string)
fmt.Sprintf("--working_directory=%s", h.Distro.WorkDir),
"--cleanup",
}

if h.Distro.SingleTaskDistro {
args = append(args, "--single_task_distro")
}
return args
}

// AgentEnv returns the environment variables required to start the agent.
Expand Down
15 changes: 11 additions & 4 deletions operations/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ const (
agentCloudProviderFlagName = "provider"
agentHostIDFlagName = "host_id"
agentHostSecretFlagName = "host_secret"
singleTaskDistroFlagName = "single_task_distro"
)

func Agent() cli.Command {
Expand Down Expand Up @@ -112,6 +113,10 @@ func Agent() cli.Command {
Name: joinFlagNames(versionFlagName, "v"),
Usage: "print the agent revision of the current binary and exit",
},
cli.BoolFlag{
Name: singleTaskDistroFlagName,
Usage: "marks the agent as running in single task distro",
},
},
Before: mergeBeforeFuncs(
func(c *cli.Context) error {
Expand Down Expand Up @@ -159,6 +164,7 @@ func Agent() cli.Command {
Cleanup: c.Bool(cleanupFlagName),
CloudProvider: c.String(agentCloudProviderFlagName),
SendTaskLogsToGlobalSender: c.Bool(sendTaskLogsToGlobalSenderFlagName),
SingleTaskDistro: c.Bool(singleTaskDistroFlagName),
}

// Once the agent has retrieved the host ID and secret, unset those
Expand All @@ -176,10 +182,11 @@ func Agent() cli.Command {
}

grip.Info(message.Fields{
"message": "starting agent",
"commands": command.RegisteredCommandNames(),
"dir": opts.WorkingDirectory,
"host_id": opts.HostID,
"message": "starting agent",
"commands": command.RegisteredCommandNames(),
"dir": opts.WorkingDirectory,
"host_id": opts.HostID,
"single_task_distro": opts.SingleTaskDistro,
})

ctx, cancel := context.WithCancel(context.Background())
Expand Down
Loading