Task for continuously check for iTunes Library updates

This commit is contained in:
Deluan
2016-03-11 18:37:37 -05:00
parent 71f1fab575
commit 12b0350d3e
4 changed files with 59 additions and 3 deletions
+1 -1
View File
@@ -8,6 +8,6 @@ import (
type SyncController struct{ beego.Controller } type SyncController struct{ beego.Controller }
func (c *SyncController) Get() { func (c *SyncController) Get() {
scanner.StartImport() scanner.CheckForUpdates(true)
c.Ctx.WriteString("Import started. Check logs") c.Ctx.WriteString("Import started. Check logs")
} }
+1
View File
@@ -3,6 +3,7 @@ package main
import ( import (
"github.com/astaxie/beego" "github.com/astaxie/beego"
_ "github.com/deluan/gosonic/conf" _ "github.com/deluan/gosonic/conf"
_ "github.com/deluan/gosonic/tasks"
) )
func main() { func main() {
+38 -2
View File
@@ -6,6 +6,8 @@ import (
"strings" "strings"
"time" "time"
"os"
"github.com/astaxie/beego" "github.com/astaxie/beego"
"github.com/deluan/gosonic/consts" "github.com/deluan/gosonic/consts"
"github.com/deluan/gosonic/domain" "github.com/deluan/gosonic/domain"
@@ -23,17 +25,51 @@ type Scanner interface {
type tempIndex map[string]domain.ArtistInfo 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() { 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 // TODO Move all to DI
i := &Importer{mediaFolder: beego.AppConfig.String("musicFolder")} i := &Importer{mediaFolder: beego.AppConfig.String("musicFolder")}
utils.ResolveDependencies(&i.mfRepo, &i.albumRepo, &i.artistRepo, &i.idxRepo, &i.plsRepo, utils.ResolveDependencies(&i.mfRepo, &i.albumRepo, &i.artistRepo, &i.idxRepo, &i.plsRepo,
&i.propertyRepo, &i.search, &i.scanner) &i.propertyRepo, &i.search, &i.scanner)
i.Run() i.Run()
inProgress <- 1
}() }()
} }
// TODO Implement a flag 'inProgress'.
type Importer struct { type Importer struct {
scanner Scanner scanner Scanner
mediaFolder string mediaFolder string
+19
View File
@@ -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)
}