package main import ( "context" "os" "os/exec" "os/signal" "sync" "syscall" "git.milar.in/milarin/channel" "git.milar.in/nyaanime/logic" "github.com/fsnotify/fsnotify" ) var ( // AppCtx notifies all threads to finish their work and shutdown AppCtx, cancelAppCtx = context.WithCancel(context.Background()) // AppExitWg is waiting for all threads until they are done AppExitWg = &sync.WaitGroup{} Runner = InitializeRunner() ) func main() { signalChan := make(chan os.Signal, 1) signal.Notify(signalChan, syscall.SIGTERM, syscall.SIGINT) go func() { <-signalChan exit() }() // check for ffprobe in PATH if _, err := exec.LookPath("ffprobe"); err != nil { panic(err) // TODO error handling } // get access token once at startup to be sure that an access token is obtainable at all if _, err := logic.GetAnilistAccessToken(); err != nil { panic(err) // TODO error handling } if err := InitTelegramBot(); err != nil { panic(err) // TODO error handling } logic.PrintPriorityTables() fileChan, err := WatchDirectory(fsnotify.Create, DownloadPath) if err != nil { panic(err) // TODO error handling } fileHandleChan := channel.Map(fileChan, NewFileHandle) workChan, logChan := channel.Tee(fileHandleChan) AppExitWg.Add(1) go func() { defer AppExitWg.Done() channel.Each(workChan, HandleFileInRunner) }() channel.Each(logChan, PrintFileHandle) } func HandleFileInRunner(fh *FileHandle) { Runner.Run(func() { HandleFile(fh) }) } func exit() { cancelAppCtx() // notify all threads to shutdown AppExitWg.Wait() // wait for threads until shutdown }