func (this workerInstance) process() { defer close(this.done) var err error for { select { case job := <-this.high: err = job.Execute() case <-this.done: return default: select { case job := <-this.high: err = job.Execute() case job := <-this.low: err = job.Execute() case <-this.done: return } } if err != nil && err != ErrCancel { log.Warning(err.Error()) } } }
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() }