v0.5: (BREAKING) replacing path with path_patterns, improved docker monitoring mechanism, bug fixes

This commit is contained in:
yusing
2024-09-16 13:05:04 +08:00
parent 2e7ba51521
commit 7a0478164f
20 changed files with 252 additions and 246 deletions

View File

@@ -2,6 +2,7 @@ package watcher
import (
"context"
"fmt"
"time"
"github.com/docker/docker/api/types/events"
@@ -47,14 +48,28 @@ func (w *DockerWatcher) Events(ctx context.Context) (<-chan Event, <-chan E.Nest
for {
select {
case <-ctx.Done():
errCh <- E.From(<-cErrCh)
if err := <-cErrCh; err != nil {
errCh <- E.From(err)
}
return
case msg := <-cEventCh:
var Action Action
switch msg.Action {
case events.ActionStart:
Action = ActionCreated
case events.ActionDie:
Action = ActionDeleted
default: // NOTE: should not happen
Action = ActionModified
}
eventCh <- Event{
ActorName: msg.Actor.Attributes["name"],
Action: ActionModified,
ActorName: fmt.Sprintf("container %q", msg.Actor.Attributes["name"]),
Action: Action,
}
case err := <-cErrCh:
if err == nil {
continue
}
errCh <- E.From(err)
select {
case <-ctx.Done():
@@ -74,7 +89,7 @@ func (w *DockerWatcher) Events(ctx context.Context) (<-chan Event, <-chan E.Nest
}
var dwOptions = events.ListOptions{Filters: filters.NewArgs(
filters.Arg("type", "container"),
filters.Arg("event", "start"),
filters.Arg("event", "die"), // 'stop' already triggering 'die'
filters.Arg("type", string(events.ContainerEventType)),
filters.Arg("event", string(events.ActionStart)),
filters.Arg("event", string(events.ActionDie)), // 'stop' already triggering 'die'
)}

View File

@@ -12,8 +12,9 @@ type (
const (
ActionModified Action = "MODIFIED"
ActionDeleted Action = "DELETED"
ActionCreated Action = "CREATED"
ActionStarted Action = "STARTED"
ActionDeleted Action = "DELETED"
)
func (e Event) String() string {
@@ -23,11 +24,3 @@ func (e Event) String() string {
func (a Action) IsDelete() bool {
return a == ActionDeleted
}
func (a Action) IsModify() bool {
return a == ActionModified
}
func (a Action) IsCreate() bool {
return a == ActionCreated
}

View File

@@ -18,10 +18,6 @@ func NewFileWatcher(filename string) Watcher {
return &fileWatcher{filename: filename}
}
func StopAllFileWatchers() {
fwHelper.close()
}
func (f *fileWatcher) Events(ctx context.Context) (<-chan Event, <-chan E.NestedError) {
return fwHelper.Add(ctx, f)
}

View File

@@ -80,13 +80,6 @@ func (h *fileWatcherHelper) Remove(w *fileWatcher) {
delete(h.m, w.filename)
}
// deinit closes the fs watcher
// and waits for the start() loop to finish
func (h *fileWatcherHelper) close() {
_ = h.w.Close()
h.wg.Wait() // wait for `start()` loop to finish
}
func (h *fileWatcherHelper) start() {
defer h.wg.Done()