Mercurial > gemma
diff pkg/imports/queue.go @ 958:2818ad6c7d32
Started with import queue.
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 16 Oct 2018 14:59:32 +0200 |
parents | |
children | 544a5cfe07cd |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/pkg/imports/queue.go Tue Oct 16 14:59:32 2018 +0200 @@ -0,0 +1,52 @@ +package imports + +import ( + "container/list" + "context" + "database/sql" + "log" + "sync" + + "gemma.intevation.de/gemma/pkg/auth" +) + +type Job interface { + Who() string + Do(conn *sql.Conn) error + CleanUp() error +} + +var ( + queueCond = sync.NewCond(new(sync.Mutex)) + queue = list.New() +) + +func init() { + go importLoop() +} + +func AddJob(job Job) { + queueCond.L.Lock() + defer queueCond.L.Unlock() + queue.PushBack(job) + queueCond.Signal() +} + +func importLoop() { + for { + var job Job + queueCond.L.Lock() + for queue.Len() == 0 { + queueCond.Wait() + } + job = queue.Remove(queue.Front()).(Job) + queueCond.L.Unlock() + + if err := auth.RunAs(job.Who(), context.Background(), job.Do); err != nil { + log.Printf("import error: %v\n", err) + } + if err := job.CleanUp(); err != nil { + log.Printf("cleanup error: %v\n", err) + } + } +}