2017-09-01 18:55:53 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/spf13/cobra"
|
2017-09-10 16:13:05 +02:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2017-09-01 18:55:53 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// daemonCmd represents the daemon command
|
|
|
|
var daemonCmd = &cobra.Command{
|
|
|
|
Use: "daemon",
|
2017-09-10 16:13:05 +02:00
|
|
|
Short: "start daemon",
|
2017-09-01 18:55:53 +02:00
|
|
|
Run: doDaemon,
|
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
RootCmd.AddCommand(daemonCmd)
|
|
|
|
}
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
type jobLogger struct {
|
|
|
|
MainLog Logger
|
|
|
|
JobName string
|
|
|
|
}
|
2017-09-01 18:55:53 +02:00
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
func (l jobLogger) Printf(format string, v ...interface{}) {
|
|
|
|
l.MainLog.Printf(fmt.Sprintf("[%s]: %s", l.JobName, format), v...)
|
|
|
|
}
|
2017-09-01 18:55:53 +02:00
|
|
|
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
type Job interface {
|
|
|
|
JobName() string
|
|
|
|
JobDo(log Logger) (err error)
|
|
|
|
}
|
2017-09-01 18:55:53 +02:00
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
func doDaemon(cmd *cobra.Command, args []string) {
|
2017-09-01 18:55:53 +02:00
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
var wg sync.WaitGroup
|
|
|
|
|
|
|
|
log.Printf("starting jobs from config")
|
|
|
|
|
|
|
|
for _, job := range conf.Jobs {
|
|
|
|
log.Printf("starting job %s", job.JobName())
|
|
|
|
logger := jobLogger{log, job.JobName()}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(j Job) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := job.JobDo(logger)
|
|
|
|
if err != nil {
|
|
|
|
logger.Printf("returned error: %+v", err)
|
2017-09-01 18:55:53 +02:00
|
|
|
}
|
2017-09-10 16:13:05 +02:00
|
|
|
}(job)
|
2017-09-01 18:55:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-10 16:13:05 +02:00
|
|
|
log.Printf("waiting for jobs from config to finish")
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-09-01 18:55:53 +02:00
|
|
|
}
|