func watchDaemon(mdbFileName string, mdbChannel chan<- *mdb.Mdb, logger *log.Logger) { var lastStat syscall.Stat_t var lastMdb *mdb.Mdb for ; ; time.Sleep(time.Second) { var stat syscall.Stat_t if err := syscall.Stat(mdbFileName, &stat); err != nil { logger.Printf("Error stating file: %s\t%s\n", mdbFileName, err) continue } stat.Atim = lastStat.Atim if stat != lastStat { mdb := loadFile(mdbFileName, logger) if mdb == nil { continue } compareStartTime := time.Now() if lastMdb == nil || !compare(lastMdb, mdb) { if lastMdb != nil { mdbCompareTimeDistribution.Add(time.Since(compareStartTime)) } mdbChannel <- mdb lastMdb = mdb } lastStat = stat } } }
func watchDaemon(mdbFileName string, mdbChannel chan *Mdb, logger *log.Logger) { var lastStat syscall.Stat_t var lastMdb *Mdb for ; ; time.Sleep(time.Second) { var stat syscall.Stat_t if err := syscall.Stat(mdbFileName, &stat); err != nil { logger.Printf("Error stating file: %s\t%s\n", mdbFileName, err) continue } stat.Atim = lastStat.Atim if stat != lastStat { file, err := os.Open(mdbFileName) if err != nil { logger.Printf("Error opening file\t%s\n", err) continue } decoder := json.NewDecoder(file) var mdb Mdb decodeStartTime := time.Now() if err = decoder.Decode(&mdb.Machines); err != nil { logger.Printf("Error decoding\t%s\n", err) continue } sortStartTime := time.Now() mdbDecodeTimeDistribution.Add(sortStartTime.Sub(decodeStartTime)) sort.Sort(&mdb) compareStartTime := time.Now() mdbSortTimeDistribution.Add(compareStartTime.Sub(sortStartTime)) if lastMdb == nil || !compare(lastMdb, &mdb) { if lastMdb != nil { mdbCompareTimeDistribution.Add(time.Since(compareStartTime)) } mdbChannel <- &mdb lastMdb = &mdb } lastStat = stat } } }