Skip to content

Commit a00c62a

Browse files
theanurincliffmccarthy
authored andcommitted
Single workflow mode. Configures the agent to exit(shutdown) after first workflow.
1 parent e2d6c71 commit a00c62a

File tree

3 files changed

+41
-6
lines changed

3 files changed

+41
-6
lines changed

cmd/agent/core/agent.go

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,14 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
9090
hostname, _ = os.Hostname()
9191
}
9292

93-
counter.Polling = c.Int("max-workflows")
93+
maxWorkflows := c.Int("max-workflows")
94+
singleWorkflow := c.Bool("single-workflow")
95+
if singleWorkflow && maxWorkflows > 1 {
96+
log.Warn().Msgf("max-workflows forced from %d to 1 due to agent running single workflow mode.", maxWorkflows)
97+
maxWorkflows = 1
98+
}
99+
100+
counter.Polling = maxWorkflows
94101
counter.Running = 0
95102

96103
if c.Bool("healthcheck") {
@@ -201,8 +208,6 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
201208
}
202209
log.Debug().Msgf("loaded %s backend engine", backendEngine.Name())
203210

204-
maxWorkflows := c.Int("max-workflows")
205-
206211
customLabels := make(map[string]string)
207212
if err := stringSliceAddToMap(c.StringSlice("labels"), customLabels); err != nil {
208213
return err
@@ -298,6 +303,9 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
298303

299304
log.Debug().Msg("polling new workflow")
300305
if err := runner.Run(agentCtx, shutdownCtx); err != nil {
306+
if singleWorkflow {
307+
ctxCancel(nil)
308+
}
301309
log.Error().Err(err).Msg("runner error, retrying...")
302310
// Check if context is canceled
303311
if agentCtx.Err() != nil {
@@ -311,13 +319,25 @@ func run(ctx context.Context, c *cli.Command, backends []types.Backend) error {
311319
// Continue to next iteration
312320
}
313321
}
322+
323+
if singleWorkflow {
324+
log.Info().Msg("shutdown single workflow runner")
325+
ctxCancel(nil)
326+
return nil
327+
}
314328
}
315329
})
316330
}
317331

318-
log.Info().Msgf(
319-
"starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up to %d pipelines in parallel",
320-
version.String(), backendEngine.Name(), engInfo.Platform, maxWorkflows)
332+
if singleWorkflow {
333+
log.Info().Msgf(
334+
"starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up in single workflow mode",
335+
version.String(), backendEngine.Name(), engInfo.Platform)
336+
} else {
337+
log.Info().Msgf(
338+
"starting Woodpecker agent with version '%s' and backend '%s' using platform '%s' running up to %d pipelines in parallel",
339+
version.String(), backendEngine.Name(), engInfo.Platform, maxWorkflows)
340+
}
321341

322342
return serviceWaitingGroup.Wait()
323343
}

cmd/agent/core/flags.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,12 @@ var flags = []cli.Flag{
7777
Usage: "agent parallel workflows",
7878
Value: 1,
7979
},
80+
&cli.BoolFlag{
81+
Sources: cli.EnvVars("WOODPECKER_AGENT_SINGLE_WORKFLOW"),
82+
Name: "single-workflow",
83+
Usage: "exit the agent after first workflow",
84+
Value: false,
85+
},
8086
&cli.BoolFlag{
8187
Sources: cli.EnvVars("WOODPECKER_HEALTHCHECK"),
8288
Name: "healthcheck",

docs/docs/30-administration/10-configuration/30-agent.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,15 @@ Configures the number of parallel workflows.
148148

149149
---
150150

151+
### WOODPECKER_AGENT_SINGLE_WORKFLOW
152+
153+
- Name: `WOODPECKER_AGENT_SINGLE_WORKFLOW`
154+
- Default: `false`
155+
156+
Configures the agent to exit(shutdown) after first workflow. When configured, `WOODPECKER_MAX_WORKFLOWS` is forced to 1.
157+
158+
---
159+
151160
### AGENT_LABELS
152161

153162
- Name: `WOODPECKER_AGENT_LABELS`

0 commit comments

Comments
 (0)