Example #1
0
func runAndWatch(godoFile string) {
	done := make(chan bool, 1)
	run := func(forceBuild bool) (*exec.Cmd, string) {
		cmd, exe := buildCommand(godoFile, forceBuild)
		cmd.Start()
		go func() {
			err := cmd.Wait()
			done <- true
			if err != nil {
				if isVerbose {
					util.Debug("godo", "godo process killed\n")
				}
			}
		}()
		return cmd, exe
	}

	bufferSize := 2048
	watchr, err := watcher.NewWatcher(bufferSize)
	if err != nil {
		util.Panic("project", "%v\n", err)
	}
	godoDir := filepath.Dir(godoFile)
	watchr.WatchRecursive(godoDir)
	watchr.ErrorHandler = func(err error) {
		util.Error("godo", "Watcher error %v\n", err)
	}

	cmd, exe := run(false)
	// this function will block forever, Ctrl+C to quit app
	// var lastHappenedTime int64
	watchr.Start()
	util.Info("godo", "watching %s ...\n", godoDir)

	<-time.After(godo.GetWatchDelay() + (300 * time.Millisecond))

	// forloop:
	for {
		select {
		case event := <-watchr.Event:
			if event.Path == exe {
				continue
			}
			util.Debug("watchmain", "%+v\n", event)
			syscall.Kill(cmd.Process.Pid, syscall.SIGQUIT)
			cmd.Process.Kill()
			<-done
			cmd, _ = run(true)
		}
	}

}
Example #2
0
func (project *Project) watchTask(task *Task, root string, logName string, handler func(e *watcher.FileEvent)) {
	ignorePathFn := func(p string) bool {
		return watcher.DefaultIgnorePathFn(p) || !task.isWatchedFile(p)
	}

	// if len(task.EffectiveWatchRegexps) == 0 {
	// 	util.Error("godo", "EffectiveWatchRegexps should not be zero")
	// } else {
	// 	ignorePathFn = func(p string) bool {
	// 		return watcher.DefaultIgnorePathFn(p) || !task.isWatchedFile(p)
	// 	}
	// }

	bufferSize := 2048
	watchr, err := watcher.NewWatcher(bufferSize)
	if err != nil {
		util.Panic("project", "%v\n", err)
	}
	watchr.IgnorePathFn = ignorePathFn
	watchr.ErrorHandler = func(err error) {
		util.Error("project", "Watcher error %v\n", err)
	}
	watchr.WatchRecursive(root)

	// this function will block forever, Ctrl+C to quit app
	// var lastHappenedTime int64
	util.Info(logName, "watching %s ...\n", root)

	// not sure why this need to be unbuffered, but it was blocking
	// on cquit <- true
	cquit := make(chan bool, 1)
	project.Lock()
	project.cwatchTasks[cquit] = true
	project.Unlock()
	watchr.Start()
forloop:
	for {
		select {
		case event := <-watchr.Event:
			//util.Debug("DBG", "handling watchr.Event %+v\n", event)
			handler(event)
		case <-cquit:
			watchr.Stop()
			break forloop
		}
	}
}