Skip to content

Commit

Permalink
docker backend: rewrite restart mechanism with retry.Do
Browse files Browse the repository at this point in the history
  • Loading branch information
motoki317 committed Oct 31, 2023
1 parent 4fae54c commit 92187e2
Showing 1 changed file with 5 additions and 21 deletions.
26 changes: 5 additions & 21 deletions pkg/infrastructure/backend/dockerimpl/backend.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"github.com/traPtitech/neoshowcase/pkg/util/retry"
"sync"
"time"

Expand All @@ -13,8 +14,6 @@ import (
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
"github.com/friendsofgo/errors"
log "github.com/sirupsen/logrus"

"github.com/traPtitech/neoshowcase/pkg/domain"
"github.com/traPtitech/neoshowcase/pkg/domain/builder"
"github.com/traPtitech/neoshowcase/pkg/util/ds"
Expand All @@ -40,7 +39,7 @@ type Backend struct {
image builder.ImageConfig

eventSubs domain.PubSub[*domain.ContainerEvent]
eventCancel func()
stopWatcher func()

reloadLock sync.Mutex
}
Expand Down Expand Up @@ -78,27 +77,12 @@ func (b *Backend) Start(ctx context.Context) error {
}

eventCtx, eventCancel := context.WithCancel(context.Background())
b.eventCancel = eventCancel
go b.eventListenerLoop(eventCtx)
b.stopWatcher = eventCancel
go retry.Do(eventCtx, b.eventListener, "container watcher")

return nil
}

func (b *Backend) eventListenerLoop(ctx context.Context) {
for {
err := b.eventListener(ctx)
if err == nil {
return
}
log.Errorf("docker event listner errored, retrying in 1s: %+v", err)
select {
case <-time.After(time.Second):
case <-ctx.Done():
return
}
}
}

func (b *Backend) eventListener(ctx context.Context) error {
// https://docs.docker.com/engine/reference/commandline/events/
ch, errCh := b.c.Events(ctx, types.EventsOptions{Filters: filters.NewArgs(filters.Arg("type", "container"))})
Expand Down Expand Up @@ -126,7 +110,7 @@ func (b *Backend) eventListener(ctx context.Context) error {
}

func (b *Backend) Dispose(_ context.Context) error {
b.eventCancel()
b.stopWatcher()
return nil
}

Expand Down

0 comments on commit 92187e2

Please sign in to comment.