示例#1
0
func (this jobMacros) Execute() error {
	for _, job := range this.jobs {
		log.Debugf("Execute: %v", reflect.TypeOf(job))
		if err := job.Execute(); err != nil {
			return err
		}
	}

	return nil
}
示例#2
0
func (this *Config) Read(path []string) error {
	var (
		err  error
		file []byte
	)
	for _, folder := range path {
		filePath := filepath.Join(folder, configFile)
		file, err = ioutil.ReadFile(filePath)
		if err == nil {
			log.Debugf("Use settings from %s\n", filePath)
			break
		}
	}

	if err == nil {
		err = json.Unmarshal(file, this)
	}
	return err
}
示例#3
0
文件: main.go 项目: darvik80/monitor
func run(cfg *config.Config) {
	workerInst := worker.NewWorker()

	done := make(chan bool)
	interrupt := make(chan os.Signal, 1)
	signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM)

	wg := sync.WaitGroup{}
	wg.Add(2)
	go func() {
		defer func() {
			workerInst.Done()
			close(done)
			log.Info("Stop watch")
			wg.Done()
		}()

		killSignal, data := <-interrupt
		if data {
			log.Warningf("Got signal: %s", killSignal)
		} else {
			log.Warning("Kill self")
		}
	}()

	go func() {
		defer func() {
			log.Info("Stop monitoring")
			close(interrupt)
			wg.Done()
		}()

		log.Info("Start watch")
		watcher, err := fsevents.NewWatcher()
		if err != nil {
			log.Errorf("Create watcher failed: %s", err.Error())
			return
		}
		if events, err := watcher.Watch(cfg.WatchDir, done); err != nil {
			log.Errorf("Monitoring failed: %s", err.Error())
		} else {
			log.Info("Start monitoring")
			for event := range events {
				if event.IsCreate() {
					log.Debugf("Created: %s", event.Path())
					job := job.NewMacros(
						job.NewCheckExt(event.Path(), ".torrent"),
						job.NewWait(time.Second),
						job.NewMove(event.Path(), filepath.Join(cfg.TorrentsDir, filepath.Base(event.Path()))))
					workerInst.Push(job, worker.Priority_Low)
				} else if event.IsDelete() {
					log.Debugf("Deleted: %s", event.Path())
				} else if event.IsRename() {
					log.Debugf("Renamed: %s", event.Path())
				}
			}
		}

	}()

	wg.Wait()
}