watch/events: Add eventInfo struct for brevity. (#2120)
This commit is contained in:
committed by
Krishna Srinivas
parent
018bcbf629
commit
9ea42680b0
+20
-38
@@ -96,10 +96,7 @@ func (f *fsClient) GetURL() clientURL {
|
||||
|
||||
// Watches for all fs events on an input path.
|
||||
func (f *fsClient) Watch(params watchParams) (*watchObject, *probe.Error) {
|
||||
eventChan := make(chan struct {
|
||||
Event Event
|
||||
Source Source
|
||||
})
|
||||
eventChan := make(chan EventInfo)
|
||||
errorChan := make(chan *probe.Error)
|
||||
doneChan := make(chan bool)
|
||||
// Make the channel buffered to ensure no event is dropped. Notify will drop
|
||||
@@ -164,50 +161,35 @@ func (f *fsClient) Watch(params watchParams) (*watchObject, *probe.Error) {
|
||||
// we want files
|
||||
continue
|
||||
}
|
||||
eventChan <- struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}{
|
||||
Event: Event{
|
||||
Time: time.Now().Format(timeFormatFS),
|
||||
Size: i.Size(),
|
||||
Path: event.Path(),
|
||||
Client: f,
|
||||
Type: EventCreate,
|
||||
},
|
||||
eventChan <- EventInfo{
|
||||
Time: time.Now().Format(timeFormatFS),
|
||||
Size: i.Size(),
|
||||
Path: event.Path(),
|
||||
Client: f,
|
||||
Type: EventCreate,
|
||||
}
|
||||
} else if IsDeleteEvent(event.Event()) {
|
||||
eventChan <- struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}{
|
||||
Event: Event{
|
||||
Time: time.Now().Format(timeFormatFS),
|
||||
Path: event.Path(),
|
||||
Client: f,
|
||||
Type: EventRemove,
|
||||
},
|
||||
eventChan <- EventInfo{
|
||||
Time: time.Now().Format(timeFormatFS),
|
||||
Path: event.Path(),
|
||||
Client: f,
|
||||
Type: EventRemove,
|
||||
}
|
||||
} else if IsGetEvent(event.Event()) {
|
||||
eventChan <- struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}{
|
||||
Event: Event{
|
||||
Time: time.Now().Format(timeFormatFS),
|
||||
Path: event.Path(),
|
||||
Client: f,
|
||||
Type: EventAccessed,
|
||||
},
|
||||
eventChan <- EventInfo{
|
||||
Time: time.Now().Format(timeFormatFS),
|
||||
Path: event.Path(),
|
||||
Client: f,
|
||||
Type: EventAccessed,
|
||||
}
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
return &watchObject{
|
||||
events: eventChan,
|
||||
errors: errorChan,
|
||||
done: doneChan,
|
||||
eventInfoChan: eventChan,
|
||||
errorChan: errorChan,
|
||||
doneChan: doneChan,
|
||||
}, nil
|
||||
}
|
||||
|
||||
|
||||
+39
-70
@@ -368,10 +368,7 @@ func (c *s3Client) ListNotificationConfigs(arn string) ([]notificationConfig, *p
|
||||
|
||||
// Start watching on all bucket events for a given account ID.
|
||||
func (c *s3Client) Watch(params watchParams) (*watchObject, *probe.Error) {
|
||||
eventChan := make(chan struct {
|
||||
Event Event
|
||||
Source Source
|
||||
})
|
||||
eventChan := make(chan EventInfo)
|
||||
errorChan := make(chan *probe.Error)
|
||||
doneChan := make(chan bool)
|
||||
|
||||
@@ -417,9 +414,9 @@ func (c *s3Client) Watch(params watchParams) (*watchObject, *probe.Error) {
|
||||
eventsCh := c.api.ListenBucketNotification(bucket, params.prefix, params.suffix, events, doneCh)
|
||||
|
||||
wo := &watchObject{
|
||||
events: eventChan,
|
||||
errors: errorChan,
|
||||
done: doneChan,
|
||||
eventInfoChan: eventChan,
|
||||
errorChan: errorChan,
|
||||
doneChan: doneChan,
|
||||
}
|
||||
|
||||
// wait for events to occur and sent them through the eventChan and errorChan
|
||||
@@ -448,76 +445,48 @@ func (c *s3Client) Watch(params watchParams) (*watchObject, *probe.Error) {
|
||||
u := *c.targetURL
|
||||
u.Path = path.Join(string(u.Separator), bucketName, key)
|
||||
if strings.HasPrefix(record.EventName, "s3:ObjectCreated:") {
|
||||
eventChan <- struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}{
|
||||
Event: Event{
|
||||
Time: record.EventTime,
|
||||
Size: record.S3.Object.Size,
|
||||
Path: u.String(),
|
||||
Client: c,
|
||||
Type: EventCreate,
|
||||
},
|
||||
Source: Source{
|
||||
IP: record.Source.Host,
|
||||
Port: record.Source.Port,
|
||||
UserAgent: record.Source.UserAgent,
|
||||
},
|
||||
eventChan <- EventInfo{
|
||||
Time: record.EventTime,
|
||||
Size: record.S3.Object.Size,
|
||||
Path: u.String(),
|
||||
Client: c,
|
||||
Type: EventCreate,
|
||||
Host: record.Source.Host,
|
||||
Port: record.Source.Port,
|
||||
UserAgent: record.Source.UserAgent,
|
||||
}
|
||||
|
||||
} else if strings.HasPrefix(record.EventName, "s3:ObjectRemoved:") {
|
||||
eventChan <- struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}{
|
||||
Event: Event{
|
||||
Time: record.EventTime,
|
||||
Path: u.String(),
|
||||
Client: c,
|
||||
Type: EventRemove,
|
||||
},
|
||||
Source: Source{
|
||||
IP: record.Source.Host,
|
||||
Port: record.Source.Port,
|
||||
UserAgent: record.Source.UserAgent,
|
||||
},
|
||||
eventChan <- EventInfo{
|
||||
Time: record.EventTime,
|
||||
Path: u.String(),
|
||||
Client: c,
|
||||
Type: EventRemove,
|
||||
Host: record.Source.Host,
|
||||
Port: record.Source.Port,
|
||||
UserAgent: record.Source.UserAgent,
|
||||
}
|
||||
} else if record.EventName == minio.ObjectAccessedGet {
|
||||
eventChan <- struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}{
|
||||
Event: Event{
|
||||
Time: record.EventTime,
|
||||
Size: record.S3.Object.Size,
|
||||
Path: u.String(),
|
||||
Client: c,
|
||||
Type: EventAccessedRead,
|
||||
},
|
||||
Source: Source{
|
||||
IP: record.Source.Host,
|
||||
Port: record.Source.Port,
|
||||
UserAgent: record.Source.UserAgent,
|
||||
},
|
||||
eventChan <- EventInfo{
|
||||
Time: record.EventTime,
|
||||
Size: record.S3.Object.Size,
|
||||
Path: u.String(),
|
||||
Client: c,
|
||||
Type: EventAccessedRead,
|
||||
Host: record.Source.Host,
|
||||
Port: record.Source.Port,
|
||||
UserAgent: record.Source.UserAgent,
|
||||
}
|
||||
} else if record.EventName == minio.ObjectAccessedHead {
|
||||
eventChan <- struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}{
|
||||
Event: Event{
|
||||
Time: record.EventTime,
|
||||
Size: record.S3.Object.Size,
|
||||
Path: u.String(),
|
||||
Client: c,
|
||||
Type: EventAccessedStat,
|
||||
},
|
||||
Source: Source{
|
||||
IP: record.Source.Host,
|
||||
Port: record.Source.Port,
|
||||
UserAgent: record.Source.UserAgent,
|
||||
},
|
||||
eventChan <- EventInfo{
|
||||
Time: record.EventTime,
|
||||
Size: record.S3.Object.Size,
|
||||
Path: u.String(),
|
||||
Client: c,
|
||||
Type: EventAccessedStat,
|
||||
Host: record.Source.Host,
|
||||
Port: record.Source.Port,
|
||||
UserAgent: record.Source.UserAgent,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+8
-8
@@ -289,19 +289,19 @@ func (mj *mirrorJob) watchMirror() {
|
||||
}
|
||||
}
|
||||
|
||||
sourceURL := newClientURL(event.Event.Path)
|
||||
aliasedPath := strings.Replace(event.Event.Path, sourceURLFull, mj.sourceURL, -1)
|
||||
sourceURL := newClientURL(event.Path)
|
||||
aliasedPath := strings.Replace(event.Path, sourceURLFull, mj.sourceURL, -1)
|
||||
|
||||
// build target path, it is the relative of the event.Path with the sourceUrl
|
||||
// joined to the targetURL.
|
||||
sourceSuffix := strings.TrimPrefix(event.Event.Path, sourceURLFull)
|
||||
sourceSuffix := strings.TrimPrefix(event.Path, sourceURLFull)
|
||||
targetPath := urlJoinPath(mj.targetURL, sourceSuffix)
|
||||
|
||||
// newClient needs the unexpanded path, newCLientURL needs the expanded path
|
||||
targetAlias, expandedTargetPath, _ := mustExpandAlias(targetPath)
|
||||
targetURL := newClientURL(expandedTargetPath)
|
||||
|
||||
if event.Event.Type == EventCreate {
|
||||
if event.Type == EventCreate {
|
||||
// we are checking if a destination file exists now, and if we only
|
||||
// overwrite it when force is enabled.
|
||||
mirrorURL := URLs{
|
||||
@@ -310,7 +310,7 @@ func (mj *mirrorJob) watchMirror() {
|
||||
TargetAlias: targetAlias,
|
||||
TargetContent: &clientContent{URL: *targetURL},
|
||||
}
|
||||
if event.Event.Size == 0 {
|
||||
if event.Size == 0 {
|
||||
sourceClient, err := newClient(aliasedPath)
|
||||
if err != nil {
|
||||
// cannot create sourceclient
|
||||
@@ -361,14 +361,14 @@ func (mj *mirrorJob) watchMirror() {
|
||||
shouldQueue = true
|
||||
}
|
||||
if shouldQueue || mj.isForce {
|
||||
mirrorURL.SourceContent.Size = event.Event.Size
|
||||
mirrorURL.SourceContent.Size = event.Size
|
||||
mirrorURL.TotalCount = mj.TotalObjects
|
||||
mirrorURL.TotalSize = mj.TotalBytes
|
||||
// adjust total, because we want to show progress of the itemj stiil queued to be copied.
|
||||
mj.status.SetTotal(mj.status.Total() + event.Event.Size).Update()
|
||||
mj.status.SetTotal(mj.status.Total() + event.Size).Update()
|
||||
mj.statusCh <- mj.doMirror(mirrorURL)
|
||||
}
|
||||
} else if event.Event.Type == EventRemove {
|
||||
} else if event.Type == EventRemove {
|
||||
mirrorURL := URLs{
|
||||
SourceAlias: sourceAlias,
|
||||
SourceContent: nil,
|
||||
|
||||
+20
-7
@@ -97,8 +97,17 @@ func checkWatchSyntax(ctx *cli.Context) {
|
||||
// watchMessage container to hold one event notification
|
||||
type watchMessage struct {
|
||||
Status string `json:"status"`
|
||||
Event Event `json:"events"`
|
||||
Source Source `json:"source,omitempty"`
|
||||
Event struct {
|
||||
Time string `json:"time"`
|
||||
Size int64 `json:"size"`
|
||||
Path string `json:"path"`
|
||||
Type EventType `json:"type"`
|
||||
} `json:"events"`
|
||||
Source struct {
|
||||
Host string `json:"host,omitempty"`
|
||||
Port string `json:"port,omitempty"`
|
||||
UserAgent string `json:"userAgent,omitempty"`
|
||||
} `json:"source,omitempty"`
|
||||
}
|
||||
|
||||
func (u watchMessage) JSON() string {
|
||||
@@ -170,16 +179,20 @@ func mainWatch(ctx *cli.Context) error {
|
||||
select {
|
||||
case <-trapCh:
|
||||
// Signal received we are done.
|
||||
close(wo.done)
|
||||
close(wo.doneChan)
|
||||
return
|
||||
case event, ok := <-wo.Events():
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
msg := watchMessage{
|
||||
Event: event.Event,
|
||||
Source: event.Source,
|
||||
}
|
||||
msg := watchMessage{}
|
||||
msg.Event.Path = event.Path
|
||||
msg.Event.Size = event.Size
|
||||
msg.Event.Time = event.Time
|
||||
msg.Event.Type = event.Type
|
||||
msg.Source.Host = event.Host
|
||||
msg.Source.Port = event.Port
|
||||
msg.Source.UserAgent = event.UserAgent
|
||||
printMsg(msg)
|
||||
case err, ok := <-wo.Errors():
|
||||
if !ok {
|
||||
|
||||
+32
-50
@@ -40,20 +40,17 @@ const (
|
||||
EventAccessedStat = "ObjectAccessed:Stat"
|
||||
)
|
||||
|
||||
// Event contains the information of the event that occurred
|
||||
type Event struct {
|
||||
Time string `json:"time"`
|
||||
Size int64 `json:"size"`
|
||||
Path string `json:"path"`
|
||||
Client Client `json:"-"`
|
||||
Type EventType `json:"type"`
|
||||
}
|
||||
|
||||
// Source obtains the information of the client which generated the event.
|
||||
type Source struct {
|
||||
IP string `json:"ip"`
|
||||
Port string `json:"port"`
|
||||
UserAgent string `json:"userAgent"`
|
||||
// EventInfo contains the information of the event that occurred and the source
|
||||
// IP:PORT of the client which triggerred the event.
|
||||
type EventInfo struct {
|
||||
Time string
|
||||
Size int64
|
||||
Path string
|
||||
Client Client
|
||||
Type EventType
|
||||
Host string
|
||||
Port string
|
||||
UserAgent string
|
||||
}
|
||||
|
||||
type watchParams struct {
|
||||
@@ -65,46 +62,37 @@ type watchParams struct {
|
||||
}
|
||||
|
||||
type watchObject struct {
|
||||
// events will be put on this chan
|
||||
events chan struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}
|
||||
// eventInfo will be put on this chan
|
||||
eventInfoChan chan EventInfo
|
||||
// errors will be put on this chan
|
||||
errors chan *probe.Error
|
||||
errorChan chan *probe.Error
|
||||
// will stop the watcher goroutines
|
||||
done chan bool
|
||||
doneChan chan bool
|
||||
}
|
||||
|
||||
// Events returns the chan receiving events
|
||||
func (w *watchObject) Events() chan struct {
|
||||
Event Event
|
||||
Source Source
|
||||
} {
|
||||
return w.events
|
||||
func (w *watchObject) Events() chan EventInfo {
|
||||
return w.eventInfoChan
|
||||
}
|
||||
|
||||
// Errors returns the chan receiving errors
|
||||
func (w *watchObject) Errors() chan *probe.Error {
|
||||
return w.errors
|
||||
return w.errorChan
|
||||
}
|
||||
|
||||
// Close the watcher, will stop all goroutines
|
||||
func (w *watchObject) Close() {
|
||||
close(w.done)
|
||||
close(w.doneChan)
|
||||
}
|
||||
|
||||
// Watcher can be used to have one or multiple clients watch for notifications
|
||||
type Watcher struct {
|
||||
sessionStartTime time.Time
|
||||
|
||||
// all errors will be added to this chan
|
||||
errorsChan chan *probe.Error
|
||||
// all error will be added to this chan
|
||||
errorChan chan *probe.Error
|
||||
// all events will be added to this chan
|
||||
eventsChan chan struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}
|
||||
eventInfoChan chan EventInfo
|
||||
|
||||
// array of watchers joined
|
||||
o []*watchObject
|
||||
@@ -117,26 +105,20 @@ type Watcher struct {
|
||||
func NewWatcher(sessionStartTime time.Time) *Watcher {
|
||||
return &Watcher{
|
||||
sessionStartTime: sessionStartTime,
|
||||
errorsChan: make(chan *probe.Error),
|
||||
eventsChan: make(chan struct {
|
||||
Event Event
|
||||
Source Source
|
||||
}),
|
||||
o: []*watchObject{},
|
||||
errorChan: make(chan *probe.Error),
|
||||
eventInfoChan: make(chan EventInfo),
|
||||
o: []*watchObject{},
|
||||
}
|
||||
}
|
||||
|
||||
// Errors returns a channel which will receive errors
|
||||
func (w *Watcher) Errors() chan *probe.Error {
|
||||
return w.errorsChan
|
||||
return w.errorChan
|
||||
}
|
||||
|
||||
// Events returns a channel which will receive events
|
||||
func (w *Watcher) Events() chan struct {
|
||||
Event Event
|
||||
Source Source
|
||||
} {
|
||||
return w.eventsChan
|
||||
func (w *Watcher) Events() chan EventInfo {
|
||||
return w.eventInfoChan
|
||||
}
|
||||
|
||||
// Stop watcher
|
||||
@@ -148,8 +130,8 @@ func (w *Watcher) Stop() {
|
||||
|
||||
w.wg.Wait()
|
||||
|
||||
close(w.errorsChan)
|
||||
close(w.eventsChan)
|
||||
close(w.errorChan)
|
||||
close(w.eventInfoChan)
|
||||
}
|
||||
|
||||
// Watching returns if the watcher is watching for notifications
|
||||
@@ -190,13 +172,13 @@ func (w *Watcher) Join(client Client, recursive bool) *probe.Error {
|
||||
return
|
||||
}
|
||||
|
||||
w.eventsChan <- event
|
||||
w.eventInfoChan <- event
|
||||
case err, ok := <-wo.Errors():
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
w.errorsChan <- err
|
||||
w.errorChan <- err
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user