Run starts the main loop of the watcher. This method blocks until the ctx is canceled by the caller
(ctx context.Context)
| 87 | // Run starts the main loop of the watcher. |
| 88 | // This method blocks until the ctx is canceled by the caller |
| 89 | func (watcher *SyncWatcher) Run(ctx context.Context) error { |
| 90 | for { |
| 91 | err := func() error { |
| 92 | // register self process to the cluster |
| 93 | lockKey := processPathPrefix + "/" + watcher.sync.GetProcessID() |
| 94 | lost, err := watcher.sync.Lock(lockKey, true) |
| 95 | if err != nil { |
| 96 | return err |
| 97 | } |
| 98 | defer watcher.sync.Unlock(lockKey) |
| 99 | |
| 100 | watchCtx, watchCancel := context.WithCancel(ctx) |
| 101 | defer watchCancel() |
| 102 | events := watcher.sync.WatchContext(watchCtx, processPathPrefix, int64(gohan_sync.RevisionCurrent)) |
| 103 | watchErr := make(chan error, 1) |
| 104 | go func() { |
| 105 | watchErr <- watcher.processWatchLoop(events) |
| 106 | }() |
| 107 | |
| 108 | select { |
| 109 | case err := <-watchErr: |
| 110 | return err |
| 111 | case <-ctx.Done(): |
| 112 | <-watchErr |
| 113 | return nil |
| 114 | case <-lost: |
| 115 | watchCancel() |
| 116 | <-watchErr |
| 117 | return fmt.Errorf("lock is lost") |
| 118 | } |
| 119 | }() |
| 120 | |
| 121 | if err != nil { |
| 122 | log.Error("process watch intrupted: %s", err) |
| 123 | } |
| 124 | |
| 125 | select { |
| 126 | case <-time.After(watcher.backoff): |
| 127 | continue |
| 128 | case <-ctx.Done(): |
| 129 | return ctx.Err() |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // processWatchLoop handles events from the watch on the process list. |
| 135 | // When this method detects a change, spawns new goroutines for sync event handling. |
no test coverage detected