Exemple #1
0
func main() {
	flag.Parse()
	mdbChannel := mdb.StartMdbDaemon(path.Join(*stateDir, "mdb"))
	interval, _ := time.ParseDuration(fmt.Sprintf("%ds", *minInterval))
	var fleet fleet.Fleet
	for {
		minCycleStopTime := time.Now().Add(interval)
		select {
		case mdb := <-mdbChannel:
			fleet.MdbUpdate(mdb)
			if *debug {
				b, _ := json.Marshal(mdb)
				var out bytes.Buffer
				json.Indent(&out, b, "", "    ")
				fmt.Println()
				out.WriteTo(os.Stdout)
				fmt.Println()
			}
		default:
			// Do work.
			fleet.PollNextSub()
		}
		fmt.Print(".")
		runtime.GC() // An opportune time to take out the garbage.
		time.Sleep(minCycleStopTime.Sub(time.Now()))
	}
}
Exemple #2
0
func main() {
	flag.Parse()
	if os.Geteuid() == 0 {
		fmt.Fprintln(os.Stderr, "Do not run the Dominator as root")
		os.Exit(1)
	}
	fi, err := os.Lstat(*stateDir)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Cannot stat: %s\t%s\n", *stateDir, err)
		os.Exit(1)
	}
	if !fi.IsDir() {
		fmt.Fprintf(os.Stderr, "%s is not a directory\n", *stateDir)
		os.Exit(1)
	}
	interval := time.Duration(*minInterval) * time.Second
	circularBuffer := logbuf.New(*logbufLines)
	logger := log.New(circularBuffer, "", log.LstdFlags)
	mdbChannel := mdb.StartMdbDaemon(path.Join(*stateDir, "mdb"), logger)
	herd := herd.NewHerd(fmt.Sprintf("%s:%d", *imageServerHostname,
		*imageServerPortNum), logger)
	herd.AddHtmlWriter(circularBuffer)
	if err = herd.StartServer(*portNum, true); err != nil {
		fmt.Fprintf(os.Stderr, "Unable to create http server\t%s\n", err)
		os.Exit(1)
	}
	nextCycleStopTime := time.Now().Add(interval)
	for {
		select {
		case mdb := <-mdbChannel:
			herd.MdbUpdate(mdb)
			if *debug {
				showMdb(mdb)
			}
			runtime.GC() // An opportune time to take out the garbage.
		default:
			// Do work.
			if herd.PollNextSub() {
				if *debug {
					fmt.Print(".")
				}
				sleepTime := nextCycleStopTime.Sub(time.Now())
				time.Sleep(sleepTime)
				nextCycleStopTime = time.Now().Add(interval)
				if sleepTime < 0 { // There was no time to rest.
					runtime.GC() // An opportune time to take out the garbage.
				}
			}
		}
	}
}