diff --git a/controllers/sync.go b/controllers/sync.go index 93499d7dd..6877b57bd 100644 --- a/controllers/sync.go +++ b/controllers/sync.go @@ -8,6 +8,6 @@ import ( type SyncController struct{ beego.Controller } func (c *SyncController) Get() { - scanner.StartImport() + scanner.CheckForUpdates(true) c.Ctx.WriteString("Import started. Check logs") } diff --git a/main.go b/main.go index 2ea828c5a..f95b6dabc 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "github.com/astaxie/beego" _ "github.com/deluan/gosonic/conf" + _ "github.com/deluan/gosonic/tasks" ) func main() { diff --git a/scanner/importer.go b/scanner/importer.go index 8d4587cce..739e21bb1 100644 --- a/scanner/importer.go +++ b/scanner/importer.go @@ -6,6 +6,8 @@ import ( "strings" "time" + "os" + "github.com/astaxie/beego" "github.com/deluan/gosonic/consts" "github.com/deluan/gosonic/domain" @@ -23,17 +25,51 @@ type Scanner interface { type tempIndex map[string]domain.ArtistInfo -func StartImport() { +var ( + inProgress = make(chan int) + lastUpdated time.Time + itunesLibrary string +) + +func init() { + startImport() +} + +func CheckForUpdates(force bool) { + <-inProgress + + if force { + lastUpdated = time.Time{} + } + + startImport() +} + +func startImport() { go func() { + itunesLibrary = beego.AppConfig.String("musicFolder") + + info, err := os.Stat(itunesLibrary) + if err != nil { + inProgress <- 1 + beego.Error(err) + return + } + if lastUpdated.After(info.ModTime()) { + inProgress <- 1 + return + } + lastUpdated = time.Now() + // TODO Move all to DI i := &Importer{mediaFolder: beego.AppConfig.String("musicFolder")} utils.ResolveDependencies(&i.mfRepo, &i.albumRepo, &i.artistRepo, &i.idxRepo, &i.plsRepo, &i.propertyRepo, &i.search, &i.scanner) i.Run() + inProgress <- 1 }() } -// TODO Implement a flag 'inProgress'. type Importer struct { scanner Scanner mediaFolder string diff --git a/tasks/scan.go b/tasks/scan.go new file mode 100644 index 000000000..cdae7d3d5 --- /dev/null +++ b/tasks/scan.go @@ -0,0 +1,19 @@ +package tasks + +import ( + "github.com/astaxie/beego/toolbox" + "github.com/deluan/gosonic/scanner" +) + +const TaskItunesScan = "iTunes Library Scanner" + +func init() { + scan := toolbox.NewTask(TaskItunesScan, "0/5 * * * * *", func() error { + scanner.CheckForUpdates(false) + return nil + }) + + toolbox.AddTask(TaskItunesScan, scan) + toolbox.StartTask() + defer toolbox.DeleteTask(TaskItunesScan) +}