Beispiel #1
0
func run(log termlog.TermLog, cnf *conf.Config, watchconf string) *conf.Config {
	modchan := make(chan *watch.Mod, 1024)
	if *ignores {
		for _, patt := range watch.CommonExcludes {
			fmt.Println(patt)
		}
		os.Exit(0)
	}

	daemonPens := make([]*modd.DaemonPen, len(cnf.Blocks))
	for i, b := range cnf.Blocks {
		if !b.NoCommonFilter {
			b.Exclude = append(b.Exclude, watch.CommonExcludes...)
		}
		cnf.Blocks[i] = b

		_, err := prepsAndNotify(b, cnf.GetVariables(), nil, log)
		if err != nil {
			log.Shout("%s", err)
			return nil
		}

		d := modd.DaemonPen{}
		c := make(chan os.Signal, 1)
		signal.Notify(c, os.Interrupt, os.Kill)
		go func() {
			d.Shutdown(<-c)
			os.Exit(0)
		}()
		if !*prep {
			d.Start(b.Daemons, cnf.GetVariables(), log)
		}
		daemonPens[i] = &d
	}
	if *prep {
		os.Exit(0)
	}

	watchpaths := cnf.WatchPaths()
	if watchconf != "" {
		watchpaths = append(watchpaths, watchconf)
	}

	// FIXME: This takes a long time. We could start it in parallel with the
	// first process run in a goroutine
	watcher, err := watch.Watch(watchpaths, lullTime, modchan)
	defer watcher.Stop()
	if err != nil {
		kingpin.Fatalf("Fatal error: %s", err)
	}

	for mod := range modchan {
		if watchconf != "" && mod.Has(watchconf) {
			ret, err := ioutil.ReadFile(watchconf)
			if err != nil {
				log.Warn("Reloading config - error reading %s: %s", watchconf, err)
				continue
			}
			newcnf, err := conf.Parse(*file, string(ret))
			if err != nil {
				log.Warn("Reloading config - error reading %s: %s", watchconf, err)
				continue
			}
			log.Notice("Reloading config %s", watchconf)
			return newcnf
		}
		if mod == nil {
			break
		}
		log.SayAs("debug", "Delta: \n%s", mod.String())
		for i, b := range cnf.Blocks {
			lmod, err := mod.Filter(b.Include, b.Exclude)
			if err != nil {
				log.Shout("Error filtering events: %s", err)
				continue
			}
			if lmod.Empty() {
				continue
			}

			proceed, err := prepsAndNotify(b, cnf.GetVariables(), lmod, log)
			if err != nil {
				log.Shout("%s", err)
				return nil
			}
			if !proceed {
				continue
			}
			daemonPens[i].Restart()
		}
	}
	return nil
}