Let agent retry to connecting to server (#1728)

Closes https://github.com/woodpecker-ci/woodpecker/issues/1721

PR introduces connection retries during agent startup.

Signed-off-by: Alexander Matyushentsev <AMatyushentsev@gmail.com>
This commit is contained in:
Alexander Matyushentsev 2023-05-03 04:31:29 -07:00 committed by GitHub
parent 80444c73af
commit 049c26229d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 49 additions and 2 deletions

View file

@ -32,10 +32,12 @@ import (
"github.com/tevino/abool"
"github.com/urfave/cli/v2"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
grpccredentials "google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/keepalive"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
"github.com/woodpecker-ci/woodpecker/agent"
agentRpc "github.com/woodpecker-ci/woodpecker/agent/rpc"
@ -46,7 +48,7 @@ import (
"github.com/woodpecker-ci/woodpecker/version"
)
func loop(c *cli.Context) error {
func run(c *cli.Context) error {
hostname := c.String("hostname")
if len(hostname) == 0 {
hostname, _ = os.Hostname()
@ -246,6 +248,21 @@ func loop(c *cli.Context) error {
return nil
}
func runWithRetry(context *cli.Context) error {
retryCount := context.Int("connect-retry-count")
retryDelay := context.Duration("connect-retry-delay")
var err error
for i := 0; i < retryCount; i++ {
if err = run(context); status.Code(err) == codes.Unavailable {
log.Warn().Err(err).Msg(fmt.Sprintf("cannot connect to server, retrying in %v", retryDelay))
time.Sleep(retryDelay)
} else {
break
}
}
return err
}
func stringSliceAddToMap(sl []string, m map[string]string) error {
if m == nil {
m = make(map[string]string)

View file

@ -190,4 +190,16 @@ var flags = []cli.Flag{
Usage: "backend k8s additional worker pod annotations",
Value: "",
},
&cli.IntFlag{
EnvVars: []string{"WOODPECKER_CONNECT_RETRY_COUNT"},
Name: "connect-retry-count",
Usage: "number of times to retry connecting to the server",
Value: 5,
},
&cli.DurationFlag{
EnvVars: []string{"WOODPECKER_CONNECT_RETRY_DELAY"},
Name: "connect-retry-delay",
Usage: "duration to wait before retrying to connect to the server",
Value: time.Second * 2,
},
}

View file

@ -29,7 +29,7 @@ func main() {
app.Name = "woodpecker-agent"
app.Version = version.String()
app.Usage = "woodpecker agent"
app.Action = loop
app.Action = runWithRetry
app.Commands = []*cli.Command{
{
Name: "ping",

View file

@ -132,3 +132,21 @@ See [Docker backend configuration](backends/docker/#configuration)
### `WOODPECKER_BACKEND_SSH_*`
See [SSH backend configuration](backends/ssh/#configuration)
## Advanced Settings
:::warning
Only change these If you know what you do.
:::
### `WOODPECKER_CONNECT_RETRY_COUNT`
> Default: `5`
Configures number of times agent retries to connect to the server.
### `WOODPECKER_CONNECT_RETRY_DELAY`
> Default: `2s`
Configures delay between agent connection retries to the server.