mirror of
https://github.com/yusing/godoxy.git
synced 2026-03-19 07:43:54 +01:00
v0.5.0-rc5: check release
This commit is contained in:
@@ -2,13 +2,13 @@ package watcher
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/docker/docker/api/types/events"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
D "github.com/yusing/go-proxy/docker"
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
. "github.com/yusing/go-proxy/watcher/event"
|
||||
)
|
||||
|
||||
type DockerWatcher struct {
|
||||
@@ -34,13 +34,14 @@ func (w *DockerWatcher) Events(ctx context.Context) (<-chan Event, <-chan E.Nest
|
||||
if err.NoError() {
|
||||
break
|
||||
}
|
||||
errCh <- E.From(err)
|
||||
errCh <- err
|
||||
time.Sleep(1 * time.Second)
|
||||
}
|
||||
if err.HasError() {
|
||||
errCh <- E.Failure("connecting to docker")
|
||||
return
|
||||
}
|
||||
defer cl.Close()
|
||||
|
||||
cEventCh, cErrCh := cl.Events(ctx, dwOptions)
|
||||
started <- struct{}{}
|
||||
@@ -58,13 +59,16 @@ func (w *DockerWatcher) Events(ctx context.Context) (<-chan Event, <-chan E.Nest
|
||||
case events.ActionStart:
|
||||
Action = ActionCreated
|
||||
case events.ActionDie:
|
||||
Action = ActionDeleted
|
||||
Action = ActionStopped
|
||||
default: // NOTE: should not happen
|
||||
Action = ActionModified
|
||||
}
|
||||
eventCh <- Event{
|
||||
ActorName: fmt.Sprintf("container %q", msg.Actor.Attributes["name"]),
|
||||
Action: Action,
|
||||
Type: EventTypeDocker,
|
||||
ActorID: msg.Actor.ID,
|
||||
ActorAttributes: msg.Actor.Attributes, // labels
|
||||
ActorName: msg.Actor.Attributes["name"],
|
||||
Action: Action,
|
||||
}
|
||||
case err := <-cErrCh:
|
||||
if err == nil {
|
||||
|
||||
@@ -1,26 +0,0 @@
|
||||
package watcher
|
||||
|
||||
import "fmt"
|
||||
|
||||
type (
|
||||
Event struct {
|
||||
ActorName string
|
||||
Action Action
|
||||
}
|
||||
Action string
|
||||
)
|
||||
|
||||
const (
|
||||
ActionModified Action = "MODIFIED"
|
||||
ActionCreated Action = "CREATED"
|
||||
ActionStarted Action = "STARTED"
|
||||
ActionDeleted Action = "DELETED"
|
||||
)
|
||||
|
||||
func (e Event) String() string {
|
||||
return fmt.Sprintf("%s %s", e.ActorName, e.Action)
|
||||
}
|
||||
|
||||
func (a Action) IsDelete() bool {
|
||||
return a == ActionDeleted
|
||||
}
|
||||
34
src/watcher/event/event.go
Normal file
34
src/watcher/event/event.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package event
|
||||
|
||||
import "fmt"
|
||||
|
||||
type (
|
||||
Event struct {
|
||||
Type EventType
|
||||
ActorName string
|
||||
ActorID string
|
||||
ActorAttributes map[string]string
|
||||
Action Action
|
||||
}
|
||||
Action string
|
||||
EventType string
|
||||
)
|
||||
|
||||
const (
|
||||
ActionModified Action = "modified"
|
||||
ActionCreated Action = "created"
|
||||
ActionStarted Action = "started"
|
||||
ActionDeleted Action = "deleted"
|
||||
ActionStopped Action = "stopped"
|
||||
|
||||
EventTypeDocker EventType = "docker"
|
||||
EventTypeFile EventType = "file"
|
||||
)
|
||||
|
||||
func (e Event) String() string {
|
||||
return fmt.Sprintf("%s %s", e.ActorName, e.Action)
|
||||
}
|
||||
|
||||
func (a Action) IsDelete() bool {
|
||||
return a == ActionDeleted
|
||||
}
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
|
||||
"github.com/yusing/go-proxy/common"
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
. "github.com/yusing/go-proxy/watcher/event"
|
||||
)
|
||||
|
||||
type fileWatcher struct {
|
||||
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/fsnotify/fsnotify"
|
||||
"github.com/sirupsen/logrus"
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
. "github.com/yusing/go-proxy/watcher/event"
|
||||
)
|
||||
|
||||
type fileWatcherHelper struct {
|
||||
@@ -93,7 +94,10 @@ func (h *fileWatcherHelper) start() {
|
||||
continue
|
||||
}
|
||||
|
||||
msg := Event{ActorName: w.filename}
|
||||
msg := Event{
|
||||
Type: EventTypeFile,
|
||||
ActorName: w.filename,
|
||||
}
|
||||
switch {
|
||||
case event.Has(fsnotify.Create):
|
||||
msg.Action = ActionCreated
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
|
||||
E "github.com/yusing/go-proxy/error"
|
||||
. "github.com/yusing/go-proxy/watcher/event"
|
||||
)
|
||||
|
||||
type Watcher interface {
|
||||
|
||||
Reference in New Issue
Block a user