func runFileWatcherLoop()

in addons/addon-raas-s3-copy/packages/s3-synchronizer/src/s3-upload-file-watcher.go [238:296]


func runFileWatcherLoop(wg *sync.WaitGroup, watcher *dirWatcher, stopAfter int, dirRequiringCrawlCh *chan string, uploadDir func(dw *dirWatcher, dirToUpload string, debug bool), debug bool, processFileWatcherEvent func(dw *dirWatcher, event *fsnotify.Event), stopLoopCh *chan bool) *chan bool {
	// Increment wait group counter everytime we spawn file upload watcher thread to make sure
	// the caller (main) can wait
	wg.Add(1)

	timeOut := func() {
		if debug {
			log.Printf("\n\n THE FILE WATCHER LOOP TIMEOUT \n\n")
		}
		*stopLoopCh <- true
		// Decrement from the wait group indicating we are done
		wg.Done()
	}

TheWatcherLoop:
	for {
		if stopAfter > 0 {
			select {
			case <-time.After(time.Duration(stopAfter) * time.Second):
				timeOut()
				break
			case <-*stopLoopCh:
				if debug {
					log.Printf("\n\n RECEIVED STOP SIGNAL IN THE FILE WATCHER LOOP \n\n")
				}
				// Stop the watcher and exit
				watcher.Stop()
				break TheWatcherLoop
			case dirToUpload := <-*dirRequiringCrawlCh:
				uploadDir(watcher, dirToUpload, debug)
			case event := <-watcher.FsEvents():
				processFileWatcherEvent(watcher, &event)
			case err := <-watcher.FsErrors():
				log.Println("error:", err)
				//log.Printf("\n\n WATCHER IS ALREADY STOPPED. EXITING THE WATCHER LOOP \n\n")
				//break TheWatcherLoop
			}
		} else {
			select {
			case <-*stopLoopCh:
				if debug {
					log.Printf("\n\n RECEIVED STOP SIGNAL IN THE FILE WATCHER LOOP \n\n")
				}
				// Stop the watcher and exit
				watcher.Stop()
				break TheWatcherLoop
			case dirToUpload := <-*dirRequiringCrawlCh:
				uploadDir(watcher, dirToUpload, debug)
			case event := <-watcher.FsEvents():
				processFileWatcherEvent(watcher, &event)
			case err := <-watcher.FsErrors():
				log.Println("error:", err)
				//log.Printf("\n\n WATCHER IS ALREADY STOPPED. EXITING THE WATCHER LOOP \n\n")
				//break TheWatcherLoop
			}
		}
	}
	return stopLoopCh
}