move jobs prints to funcExecute

This commit is contained in:
sentriz
2020-04-18 20:08:01 +01:00
parent 1ff5845a02
commit 488fd83f7d

View File

@@ -181,7 +181,6 @@ type funcExecute func() error
type funcInterrupt func(error) type funcInterrupt func(error)
func (s *Server) StartHTTP(listenAddr string) (funcExecute, funcInterrupt) { func (s *Server) StartHTTP(listenAddr string) (funcExecute, funcInterrupt) {
log.Print("starting job 'http'\n")
list := &http.Server{ list := &http.Server{
Addr: listenAddr, Addr: listenAddr,
Handler: s.router, Handler: s.router,
@@ -190,18 +189,20 @@ func (s *Server) StartHTTP(listenAddr string) (funcExecute, funcInterrupt) {
IdleTimeout: 60 * time.Second, IdleTimeout: 60 * time.Second,
} }
execute := func() error { execute := func() error {
log.Print("starting job 'http'\n")
return list.ListenAndServe() return list.ListenAndServe()
} }
return execute, func(_ error) { return execute, func(_ error) {
// stop job
list.Close() list.Close()
} }
} }
func (s *Server) StartScanTicker(dur time.Duration) (funcExecute, funcInterrupt) { func (s *Server) StartScanTicker(dur time.Duration) (funcExecute, funcInterrupt) {
log.Printf("starting job 'scan timer'\n")
ticker := time.NewTicker(dur) ticker := time.NewTicker(dur)
done := make(chan struct{}) done := make(chan struct{})
execute := func() error { execute := func() error {
log.Printf("starting job 'scan timer'\n")
for { for {
select { select {
case <-done: case <-done:
@@ -214,17 +215,19 @@ func (s *Server) StartScanTicker(dur time.Duration) (funcExecute, funcInterrupt)
} }
} }
return execute, func(_ error) { return execute, func(_ error) {
// stop job
ticker.Stop() ticker.Stop()
done <- struct{}{} done <- struct{}{}
} }
} }
func (s *Server) StartJukebox() (funcExecute, funcInterrupt) { func (s *Server) StartJukebox() (funcExecute, funcInterrupt) {
log.Printf("starting job 'jukebox'\n")
execute := func() error { execute := func() error {
log.Printf("starting job 'jukebox'\n")
return s.jukebox.Listen() return s.jukebox.Listen()
} }
return execute, func(_ error) { return execute, func(_ error) {
// stop job
s.jukebox.Quit() s.jukebox.Quit()
} }
} }