Time out after five minutes of not reaching desired container count

This commit is contained in:
Frederik Ring 2024-01-27 19:42:21 +01:00
parent 26bbc66cd5
commit 2bc94d8a5b

View File

@ -42,7 +42,20 @@ func scaleService(cli *client.Client, serviceID string, replicas uint64) ([]stri
} }
func awaitContainerCountForService(cli *client.Client, serviceID string, count int) error { func awaitContainerCountForService(cli *client.Client, serviceID string, count int) error {
poll := time.NewTicker(time.Second)
timeout := time.NewTicker(5 * time.Minute)
defer timeout.Stop()
defer poll.Stop()
for { for {
select {
case <-timeout.C:
return fmt.Errorf(
"awaitContainerCount: timed out after waiting 5 minutes for service %s to reach desired container count of %d",
serviceID,
count,
)
case <-poll.C:
containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{ containers, err := cli.ContainerList(context.Background(), types.ContainerListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{ Filters: filters.NewArgs(filters.KeyValuePair{
Key: "label", Key: "label",
@ -53,12 +66,11 @@ func awaitContainerCountForService(cli *client.Client, serviceID string, count i
return fmt.Errorf("awaitContainerCount: error listing containers: %w", err) return fmt.Errorf("awaitContainerCount: error listing containers: %w", err)
} }
if len(containers) == count { if len(containers) == count {
break
}
time.Sleep(time.Second)
}
return nil return nil
} }
}
}
}
// stopContainersAndServices stops all Docker containers that are marked as to being // stopContainersAndServices stops all Docker containers that are marked as to being
// stopped during the backup and returns a function that can be called to // stopped during the backup and returns a function that can be called to