Exemplo n.º 1
0
func WatchForChanges(onChange func(id string)) {
	// Create watcher
	watcher, err := inotify.NewWatcher()
	if err != nil {
		log.Printf("warning: Cannot create inotify.Watcher")
		return
	}

	// watch dirs in levels 1-3 from the roots
	watchList := []string{} // prevent walkDirs from generating events
	for _, root := range roots {
		watchList = append(watchList, root)
		walkDirs(root, func(reldir string, level int) {
			watchList = append(watchList, filepath.Join(root, reldir))
		})
	}
	for _, dir := range watchList {
		watcher.Watch(dir)
	}

	// go wait for events
	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				rel := removeRoot(ev.Name)
				if onChange != nil && isChange(ev) {
					if !isDir(ev) {
						rel = filepath.Dir(rel)
					}
					id := ToID(rel)
					onChange(id)
					if isDir(ev) {
						onChange(parentID(id))
					}
				}
				// watch new dirs
				if isDir(ev) {
					lv := numLevels(rel)
					if lv > 0 && lv < 4 {
						watcher.Watch(ev.Name)
					}
				}
			case err := <-watcher.Error:
				log.Printf("watcher error: %s", err)
			}
		}
	}()
}
Exemplo n.º 2
0
func startWatcher() {
	// Start the event loops. Use inotify to watch the configfile and each
	// repository path.
	Config, e := conf.ReadConfigFile(ConfigName)
	if e != nil {
		log.Fatal(e)
	}
	watcher, e := inotify.NewWatcher()
	if e != nil {
		log.Fatal(e)
	}
	log.Println("watching configfile")
	e = watcher.Watch(ConfigName)
	if e != nil {
		log.Fatal(e)
	}
	Locks = make([]int64, RemoteCount)
	Paths = make([]string, RemoteCount)
	for i := 1; i <= RemoteCount; i++ {
		section := strings.Join([]string{"remote-", strconv.Itoa(i)}, "")
		if !Config.HasOption(section, "path") {
			log.Println("doesnt have path")
			continue
		}
		path, e := Config.GetRawString(section, "path")
		if e != nil {
			log.Fatal(e)
		}
		log.Println("watching path", path)
		e = watcher.Watch(path)
		if e != nil {
			log.Fatal(e)
		}
		Locks[i-1] = 0
		Paths[i-1] = path
	}
	for {
		select {
		case ev := <-watcher.Event:
			go handleEvent(ev)
		case err := <-watcher.Error:
			log.Println("error:", err)
		}
	}
}
Exemplo n.º 3
0
func watchTemplates() {
	watcher, err := inotify.NewWatcher()
	if err != nil {
		log.Printf("Warning: Cannot watch templates.")
	}

	watcher.Watch(srvdir + "/templates")

	go func() {
		for {
			select {
			case ev := <-watcher.Event:
				if isChange(ev) {
					fmt.Printf("Changed: %s\n", ev.Name)
					readTemplates()
					cache.Touch("/templates")
				}
			}
		}
	}()
}