mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-13 05:35:18 +01:00
refactor: add context handling in various functions
- Modified functions to accept context.Context as a parameter for better context management. - Updated Init methods in Proxmox and Config to use the provided context. - Adjusted UpdatePorts and NewProxmoxProvider to utilize the context for operations.
This commit is contained in:
@@ -292,7 +292,7 @@ func (state *state) initProxmox() error {
|
||||
|
||||
errs := gperr.NewBuilder()
|
||||
for _, cfg := range proxmoxCfg {
|
||||
if err := cfg.Init(); err != nil {
|
||||
if err := cfg.Init(state.task.Context()); err != nil {
|
||||
errs.Add(err.Subject(cfg.URL))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,14 +90,14 @@ func IsBlacklisted(c *types.Container) bool {
|
||||
return IsBlacklistedImage(c.Image) || isDatabase(c)
|
||||
}
|
||||
|
||||
func UpdatePorts(c *types.Container) error {
|
||||
func UpdatePorts(ctx context.Context, c *types.Container) error {
|
||||
dockerClient, err := NewClient(c.DockerCfg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer dockerClient.Close()
|
||||
|
||||
inspect, err := dockerClient.ContainerInspect(context.Background(), c.ContainerID)
|
||||
inspect, err := dockerClient.ContainerInspect(ctx, c.ContainerID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -25,14 +25,14 @@ const proxmoxStateCheckInterval = 1 * time.Second
|
||||
|
||||
var ErrNodeNotFound = gperr.New("node not found in pool")
|
||||
|
||||
func NewProxmoxProvider(nodeName string, vmid int) (idlewatcher.Provider, error) {
|
||||
func NewProxmoxProvider(ctx context.Context, nodeName string, vmid int) (idlewatcher.Provider, error) {
|
||||
node, ok := proxmox.Nodes.Get(nodeName)
|
||||
if !ok {
|
||||
return nil, ErrNodeNotFound.Subject(nodeName).
|
||||
Withf("available nodes: %s", proxmox.AvailableNodeNames())
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
lxcName, err := node.LXCName(ctx, vmid)
|
||||
|
||||
@@ -32,7 +32,7 @@ func (c *Config) Client() *Client {
|
||||
return c.client
|
||||
}
|
||||
|
||||
func (c *Config) Init() gperr.Error {
|
||||
func (c *Config) Init(ctx context.Context) gperr.Error {
|
||||
var tr *http.Transport
|
||||
if c.NoTLSVerify {
|
||||
// user specified
|
||||
@@ -56,7 +56,7 @@ func (c *Config) Init() gperr.Error {
|
||||
}
|
||||
c.client = NewClient(c.URL, opts...)
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
|
||||
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
|
||||
defer cancel()
|
||||
|
||||
if err := c.client.UpdateClusterInfo(ctx); err != nil {
|
||||
|
||||
@@ -79,7 +79,7 @@ func (p *DockerProvider) loadRoutesImpl() (route.Routes, gperr.Error) {
|
||||
}
|
||||
|
||||
if container.IsHostNetworkMode {
|
||||
err := docker.UpdatePorts(container)
|
||||
err := docker.UpdatePorts(ctx, container)
|
||||
if err != nil {
|
||||
errs.Add(gperr.PrependSubject(container.ContainerName, err))
|
||||
continue
|
||||
|
||||
Reference in New Issue
Block a user