processWatchLoop handles events from the watch on the process list. When this method detects a change, spawns new goroutines for sync event handling. This method blocks until the events channel is closed by the caller or an error event is given from the channel.
(events <-chan *gohan_sync.Event)
| 136 | // This method blocks until the events channel is closed by the caller or |
| 137 | // an error event is given from the channel. |
| 138 | func (watcher *SyncWatcher) processWatchLoop(events <-chan *gohan_sync.Event) error { |
| 139 | processList := []string{} |
| 140 | |
| 141 | previousCancel := func() {} |
| 142 | previousDone := make(chan struct{}) |
| 143 | close(previousDone) |
| 144 | defer func() { |
| 145 | previousCancel() |
| 146 | <-previousDone |
| 147 | }() |
| 148 | |
| 149 | for event := range events { |
| 150 | previousCancel() |
| 151 | <-previousDone |
| 152 | |
| 153 | if event.Err != nil { |
| 154 | return event.Err |
| 155 | } |
| 156 | |
| 157 | log.Debug("cluster change detected: %s process %s", event.Action, event.Key) |
| 158 | |
| 159 | // modify gohan process list |
| 160 | pos := -1 |
| 161 | for p, v := range processList { |
| 162 | if v == event.Key { |
| 163 | pos = p |
| 164 | } |
| 165 | } |
| 166 | switch event.Action { |
| 167 | case "delete": |
| 168 | // remove detected process from list |
| 169 | if pos > -1 { |
| 170 | processList = append((processList)[:pos], (processList)[pos+1:]...) |
| 171 | } |
| 172 | default: |
| 173 | // add detected process from list |
| 174 | if pos == -1 { |
| 175 | processList = append(processList, event.Key) |
| 176 | sort.Sort(sort.StringSlice(processList)) |
| 177 | } |
| 178 | } |
| 179 | |
| 180 | myPosition := -1 |
| 181 | myValue := processPathPrefix + "/" + watcher.sync.GetProcessID() |
| 182 | for p, v := range processList { |
| 183 | if v == myValue { |
| 184 | myPosition = p |
| 185 | break |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | if myPosition >= 0 && len(processList) > 0 { |
| 190 | log.Debug("Current cluster consists of following processes: %s, my poistion: %d", processList, myPosition) |
| 191 | |
| 192 | var cctx context.Context |
| 193 | cctx, previousCancel = context.WithCancel(context.Background()) |
| 194 | previousDone = make(chan struct{}) |
| 195 |
no test coverage detected