예제 #1
0
// Wait until filename in dir exists and is complete
func waitForAllFiles(dir string) error {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	defer watcher.Close()
	done := make(chan bool)
	var goerr error
	go func() {
		for {
			timer := time.NewTimer(100 * time.Millisecond)
			select {
			case event := <-watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
				}
			case <-timer.C:
				done <- true
				return
			case err := <-watcher.Errors:
				goerr = err
				done <- true
			}
			timer.Stop()
		}

	}()

	err = watcher.Add(dir)
	if err != nil {
		return err
	}
	<-done
	return goerr
}
예제 #2
0
// Wait until the file filename gets created in directory dir
func waitForFile(dir string, filename string) error {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	defer watcher.Close()
	requestedFile := filepath.Join(dir, filename)
	done := make(chan bool)
	var goerr error
	go func() {
		fileStarted := make(map[string]bool)
		for {
			timer := time.NewTimer(100 * time.Millisecond)
			select {
			case event := <-watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
					fileStarted[event.Name] = true
				}
			case <-timer.C:
				if len(fileStarted) > 0 {
					for n, _ := range fileStarted {
						delete(fileStarted, n)
						if n == requestedFile {
							done <- true
							return
						}
					}
				}
			case err := <-watcher.Errors:
				goerr = err
				done <- true
			}
			timer.Stop()
		}

	}()

	err = watcher.Add(dir)
	if err != nil {
		return err
	}
	<-done
	return goerr
}
예제 #3
0
파일: video_merge.go 프로젝트: szqh97/test
func main() {
	install_logger()
	pid_file := "./var/run/video_merge.pid"
	pid_locker, err := pidlock.Lock_write_pid_file(pid_file)
	logger.Info("starting ...")
	//monitorpath := "/home/li_yun/Projects/test/go"
	monitorpath, err := getMonitorPath()
	if err != nil {
		logger.Fatal("get monitor path error: %s", err)
		os.Exit(1)
	}
	log.Println(monitorpath)
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		logger.Fatal("newWatcher error: %s", err)
	}

	done := make(chan bool)
	// Process events
	go func() {
		avfile_list := make(av_list)
		for {
			select {
			case ev := <-watcher.Event:
				(&avfile_list).process_in_moved_to(ev.Name)
			case err := <-watcher.Error:
				logger.Warn("errror: %s", err)
			}
		}
	}()

	// only monitor FSN_RENAME event
	err = watcher.WatchFlags(monitorpath, fsnotify.FSN_MOVEDTO)
	if err != nil {
		logger.Fatal("add watch flags error: %s", err)
	}

	<-done
	/* ... do stuff ... */
	watcher.Close()
	pid_locker.Close()
	logger.Info("done ...")

}
예제 #4
0
// Wait until the file filename gets created in directory dir
func watchDirectory(dir string, events []Event) {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}
	defer watcher.Close()

	done := make(chan bool)
	go func() {
		fileStarted := make(map[string]bool)
		for {
			timer := time.NewTimer(100 * time.Millisecond)
			select {
			case event := <-watcher.Events:
				if event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
					fileStarted[event.Name] = true
				}
			case <-timer.C:
				if len(fileStarted) > 0 {
					for n, _ := range fileStarted {
						f(n, events)
						delete(fileStarted, n)
					}
				}
			case err := <-watcher.Errors:
				log.Println("error:", err)
			}
			timer.Stop()
		}

	}()

	err = watcher.Add("/tmp/foo")
	if err != nil {
		log.Fatal(err)
	}
	<-done
}
예제 #5
0
파일: config.go 프로젝트: ohlinux/autogo
// Watch 监控配置文件
func Watch(configFile string) error {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	eventNum := make(chan int)
	go func() {
		for {
			i := 0
		GetEvent:
			for {
				select {
				case <-watcher.Event:
					i++
				case <-time.After(200e6):
					break GetEvent
				}
			}
			if i > 0 {
				eventNum <- i
			}
		}
	}()

	go func() {
		for {
			select {
			case <-eventNum:
				logger.LogNoticef("[INFO] ReloadConfig...")
				Load(configFile)
			}
		}
	}()

	return watcher.Watch(configFile)
}
예제 #6
0
파일: project.go 프로젝트: cw2018/autogo
// Watch 监听该项目,源码有改动会重新编译运行
func (this *Project) Watch() error {
	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		return err
	}
	eventNum := make(chan int)
	go func() {
		for {
			i := 0
		GetEvent:
			for {
				select {
				case <-watcher.Event:
					i++
				// 修改可能会有多次modify事件
				case <-time.After(500e6):
					break GetEvent
				}
			}
			if i > 0 {
				eventNum <- i
			}
		}
	}()

	go func() {
		for {
			var err error
			select {
			case <-eventNum:
				if this.GoWay == "run" {
					if err = this.Run(); err != nil {
						log.Println("run error,详细信息如下:")
						fmt.Println(err)
					}
					break
				}
				if err = this.Compile(); err != nil {
					log.Println("complie error,详细信息如下:")
					fmt.Println(err)
					break
				}
				if this.deamon {
					if err = this.Stop(); err != nil {
						log.Println("stop error,详细信息如下:")
						fmt.Println(err)
					}
				}
				if err = this.Start(); err != nil {
					log.Println("start error,详细信息如下:")
					fmt.Println(err)
				}
			}
			if this.deamon && err == nil {
				log.Println("重启完成!")
			}
		}
	}()

	addWatch(watcher, this.srcAbsolutePath)
	return nil
}
예제 #7
0
func main() {

	// Read the CMD options

	inDir := flag.String("in", "", "input directory")    // input directory
	outDir := flag.String("out", "", "output directory") // output directory
	width := flag.Int("width", 128, "the new width")     // width
	height := flag.Int("height", 128, "the new height")  // height

	flag.Parse()

	if *inDir == "" || *outDir == "" {
		log.Fatal("usage: \n imageResizer -in inputDir -out outputDir -width 128 -height 128")
	}

	// Print the cmd options

	fmt.Printf("image resize daemon \n")

	fmt.Printf("Input:  %s \n", *inDir)
	fmt.Printf("Output: %s \n", *outDir)
	fmt.Printf("Width:  %d \n", *width)
	fmt.Printf("Height: %d \n", *height)

	// Create a channel to use it to handle the idle state
	c := make(chan int)

	watcher, err := fsnotify.NewWatcher()
	if err != nil {
		log.Fatal(err)
	}

	defer watcher.Close()

	// Process file events
	go func() {
		for {
			select {
			case ev := <-watcher.Event:

				log.Println("event:", ev)

				if ev.IsCreate() {
					// Combine the directory path with the filename to get
					// the full path of the image
					fullImagePath := ev.Name
					fullImageOutPath := filepath.Join(*outDir, filepath.Base(ev.Name))

					// Launch asychronously a resize operation
					go resizeImage(fullImagePath, fullImageOutPath, *width, *height)
				}
			case err := <-watcher.Error:
				log.Println("error:", err)
			}
		}
	}()

	err = watcher.Watch(*inDir)

	if err != nil {
		log.Fatal(err)
	}

	<-c
}