watch: Clean up watch when restarting mirror after an error (#3993)
Add cleanup for watch code when it exits and mirroring is restarted.
This commit is contained in:
@@ -181,6 +181,9 @@ func (f *fsClient) Watch(ctx context.Context, options WatchOptions) (*WatchObjec
|
||||
close(eventChan)
|
||||
close(errorChan)
|
||||
notify.Stop(in)
|
||||
// At this point, notify is guaranteed to not write
|
||||
// in 'in' channel so we can close it.
|
||||
close(in)
|
||||
}()
|
||||
|
||||
timeFormatFS := "2006-01-02T15:04:05.000Z"
|
||||
|
||||
+25
-17
@@ -783,26 +783,34 @@ func (c *S3Client) Watch(ctx context.Context, options WatchOptions) (*WatchObjec
|
||||
}
|
||||
|
||||
go func() {
|
||||
// Start listening on all bucket events.
|
||||
for notificationInfo := range eventsCh {
|
||||
if notificationInfo.Err != nil {
|
||||
var perr *probe.Error
|
||||
if minio.ToErrorResponse(notificationInfo.Err).Code == "NotImplemented" {
|
||||
perr = probe.NewError(APINotImplemented{
|
||||
API: "Watch",
|
||||
APIType: c.GetURL().String(),
|
||||
})
|
||||
} else {
|
||||
perr = probe.NewError(notificationInfo.Err)
|
||||
defer close(wo.EventInfoChan)
|
||||
defer close(wo.ErrorChan)
|
||||
|
||||
for {
|
||||
// Start listening on all bucket events.
|
||||
select {
|
||||
case notificationInfo, ok := <-eventsCh:
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
wo.Errors() <- perr
|
||||
} else {
|
||||
wo.Events() <- c.notificationToEventsInfo(notificationInfo)
|
||||
if notificationInfo.Err != nil {
|
||||
var perr *probe.Error
|
||||
if minio.ToErrorResponse(notificationInfo.Err).Code == "NotImplemented" {
|
||||
perr = probe.NewError(APINotImplemented{
|
||||
API: "Watch",
|
||||
APIType: c.GetURL().String(),
|
||||
})
|
||||
} else {
|
||||
perr = probe.NewError(notificationInfo.Err)
|
||||
}
|
||||
wo.Errors() <- perr
|
||||
} else {
|
||||
wo.Events() <- c.notificationToEventsInfo(notificationInfo)
|
||||
}
|
||||
case <-wo.DoneChan:
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
close(wo.EventInfoChan)
|
||||
close(wo.ErrorChan)
|
||||
}()
|
||||
|
||||
return wo, nil
|
||||
|
||||
@@ -645,6 +645,8 @@ func (mj *mirrorJob) watchMirrorEvents(ctx context.Context, events []EventInfo)
|
||||
|
||||
// this goroutine will watch for notifications, and add modified objects to the queue
|
||||
func (mj *mirrorJob) watchMirror(ctx context.Context) {
|
||||
defer mj.watcher.Stop()
|
||||
|
||||
for {
|
||||
select {
|
||||
case events, ok := <-mj.watcher.Events():
|
||||
|
||||
+7
-7
@@ -103,13 +103,11 @@ func (w *Watcher) Events() chan []EventInfo {
|
||||
return w.EventInfoChan
|
||||
}
|
||||
|
||||
// Watching returns if the watcher is watching for notifications
|
||||
func (w *Watcher) Watching() bool {
|
||||
return (len(w.o) > 0)
|
||||
}
|
||||
|
||||
// Wait for watcher to wait
|
||||
func (w *Watcher) Wait() {
|
||||
// Stop all watchers
|
||||
func (w *Watcher) Stop() {
|
||||
for _, w := range w.o {
|
||||
close(w.DoneChan)
|
||||
}
|
||||
w.wg.Wait()
|
||||
}
|
||||
|
||||
@@ -135,6 +133,8 @@ func (w *Watcher) Join(ctx context.Context, client Client, recursive bool) *prob
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-wo.DoneChan:
|
||||
return
|
||||
case events, ok := <-wo.Events():
|
||||
if !ok {
|
||||
return
|
||||
|
||||
Reference in New Issue
Block a user