Пример #1
0
func (rs *RombaService) startRefreshDats(cmd *commander.Command, args []string) error {
	rs.jobMutex.Lock()
	defer rs.jobMutex.Unlock()

	if rs.busy {
		p := rs.pt.GetProgress()

		fmt.Fprintf(cmd.Stdout, "still busy with %s: (%d of %d files) and (%s of %s) \n", rs.jobName,
			p.FilesSoFar, p.TotalFiles, humanize.IBytes(uint64(p.BytesSoFar)), humanize.IBytes(uint64(p.TotalBytes)))
		return nil
	}

	rs.pt.Reset()
	rs.busy = true
	rs.jobName = "refresh-dats"

	go func() {
		glog.Infof("service starting refresh-dats")
		rs.broadCastProgress(time.Now(), true, false, "")
		ticker := time.NewTicker(time.Second * 5)
		stopTicker := make(chan bool)
		go func() {
			glog.Infof("starting progress broadcaster")
			for {
				select {
				case t := <-ticker.C:
					rs.broadCastProgress(t, false, false, "")
				case <-stopTicker:
					glog.Info("stopped progress broadcaster")
					return
				}
			}
		}()

		numWorkers := cmd.Flag.Lookup("workers").Value.Get().(int)
		missingSha1s := cmd.Flag.Lookup("missingSha1s").Value.Get().(string)

		endMsg, err := db.Refresh(rs.romDB, rs.dats, numWorkers, rs.pt, missingSha1s)
		if err != nil {
			glog.Errorf("error refreshing dats: %v", err)
		}

		ticker.Stop()
		stopTicker <- true

		rs.jobMutex.Lock()
		rs.busy = false
		rs.jobName = ""
		rs.jobMutex.Unlock()

		rs.broadCastProgress(time.Now(), false, true, endMsg)
		glog.Infof("service finished refresh-dats")
	}()

	fmt.Fprintf(cmd.Stdout, "started refresh dats")
	return nil
}
Пример #2
0
func refreshDats(cmd *commander.Command, args []string) {
	logPath := filepath.Join(config.General.LogDir, fmt.Sprintf("refresh-%s.log", time.Now().Format("2006-01-02-15_04_05")))

	logfile, err := os.Create(logPath)
	if err != nil {
		fmt.Fprintf(os.Stderr, "failed to create log file %s: %v\n", logPath, err)
		os.Exit(1)
	}
	defer logfile.Close()

	buflog := bufio.NewWriter(logfile)
	defer buflog.Flush()

	err = db.Refresh(romDB, config.Index.Dats, log.New(buflog, "", 0))
	if err != nil {
		fmt.Fprintf(os.Stderr, "refreshing dat index failed: %v\n", err)
		os.Exit(1)
	}
}