2017-09-01 18:55:53 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-09-13 23:27:18 +02:00
|
|
|
"context"
|
2017-09-10 16:13:05 +02:00
|
|
|
"fmt"
|
2017-09-13 23:27:18 +02:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
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 Job interface {
|
|
|
|
JobName() string
|
2017-09-13 23:27:18 +02:00
|
|
|
JobStart(ctxt context.Context)
|
2017-09-10 16:13:05 +02:00
|
|
|
}
|
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-17 18:20:05 +02:00
|
|
|
|
2017-09-22 14:02:07 +02:00
|
|
|
conf, err := ParseConfig(rootArgs.configFile)
|
2017-09-17 18:20:05 +02:00
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
fmt.Fprintf(os.Stderr, "error parsing config: %s", err)
|
2017-09-17 18:20:05 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
log := conf.Global.logging.MakeLogrus()
|
|
|
|
log.Debug("starting daemon")
|
|
|
|
ctx := context.WithValue(context.Background(), contextKeyLog, log)
|
2017-09-22 14:02:07 +02:00
|
|
|
ctx = context.WithValue(ctx, contextKeyLog, log)
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
d := NewDaemon(conf)
|
|
|
|
d.Loop(ctx)
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
2017-09-01 18:55:53 +02:00
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
type contextKey string
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
const (
|
|
|
|
contextKeyLog contextKey = contextKey("log")
|
|
|
|
)
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
type Daemon struct {
|
2017-09-17 18:20:05 +02:00
|
|
|
conf *Config
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewDaemon(initialConf *Config) *Daemon {
|
|
|
|
return &Daemon{initialConf}
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
func (d *Daemon) Loop(ctx context.Context) {
|
|
|
|
|
|
|
|
log := ctx.Value(contextKeyLog).(Logger)
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
|
|
|
|
|
|
|
sigChan := make(chan os.Signal, 1)
|
2017-09-13 23:27:18 +02:00
|
|
|
finishs := make(chan Job)
|
2017-09-17 18:20:05 +02:00
|
|
|
|
|
|
|
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
|
2017-09-13 23:27:18 +02:00
|
|
|
|
|
|
|
log.Printf("starting jobs from config")
|
|
|
|
i := 0
|
2017-09-17 18:20:05 +02:00
|
|
|
for _, job := range d.conf.Jobs {
|
2017-09-10 16:13:05 +02:00
|
|
|
log.Printf("starting job %s", job.JobName())
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
logger := log.WithField("job", job.JobName())
|
2017-09-13 23:27:18 +02:00
|
|
|
i++
|
2017-09-17 18:20:05 +02:00
|
|
|
jobCtx := context.WithValue(ctx, contextKeyLog, logger)
|
2017-09-10 16:13:05 +02:00
|
|
|
go func(j Job) {
|
2017-09-17 18:20:05 +02:00
|
|
|
j.JobStart(jobCtx)
|
2017-09-13 23:27:18 +02:00
|
|
|
finishs <- j
|
2017-09-10 16:13:05 +02:00
|
|
|
}(job)
|
2017-09-01 18:55:53 +02:00
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
finishCount := 0
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case j := <-finishs:
|
|
|
|
log.Printf("job finished: %s", j.JobName())
|
|
|
|
finishCount++
|
2017-09-17 18:20:05 +02:00
|
|
|
if finishCount == len(d.conf.Jobs) {
|
2017-09-13 23:27:18 +02:00
|
|
|
log.Printf("all jobs finished")
|
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
|
|
|
case sig := <-sigChan:
|
|
|
|
log.Printf("received signal: %s", sig)
|
|
|
|
log.Printf("cancelling all jobs")
|
2017-09-17 18:20:05 +02:00
|
|
|
cancel()
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signal.Stop(sigChan)
|
|
|
|
|
|
|
|
log.Printf("exiting")
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-01 18:55:53 +02:00
|
|
|
}
|