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"
|
2017-09-23 18:20:22 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2017-09-13 23:27:18 +02:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
2017-09-23 18:20:22 +02:00
|
|
|
"time"
|
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-23 18:20:22 +02:00
|
|
|
log := logger.NewLogger(conf.Global.logging.Outlets, 1*time.Second)
|
|
|
|
|
2017-09-22 14:13:58 +02:00
|
|
|
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
|
|
|
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("starting jobs from config")
|
2017-09-13 23:27:18 +02:00
|
|
|
i := 0
|
2017-09-17 18:20:05 +02:00
|
|
|
for _, job := range d.conf.Jobs {
|
2017-09-23 11:24:36 +02:00
|
|
|
logger := log.WithField(logJobField, job.JobName())
|
2017-09-23 17:52:29 +02:00
|
|
|
logger.Info("starting")
|
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 {
|
2017-09-23 17:52:29 +02:00
|
|
|
case <-finishs:
|
2017-09-13 23:27:18 +02:00
|
|
|
finishCount++
|
2017-09-17 18:20:05 +02:00
|
|
|
if finishCount == len(d.conf.Jobs) {
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("all jobs finished")
|
2017-09-13 23:27:18 +02:00
|
|
|
break outer
|
|
|
|
}
|
|
|
|
|
|
|
|
case sig := <-sigChan:
|
2017-09-23 17:52:29 +02:00
|
|
|
log.WithField("signal", sig).Info("received signal")
|
|
|
|
log.Info("cancelling all jobs")
|
2017-09-17 18:20:05 +02:00
|
|
|
cancel()
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
signal.Stop(sigChan)
|
|
|
|
|
2017-09-23 17:52:29 +02:00
|
|
|
log.Info("exiting")
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2017-09-01 18:55:53 +02:00
|
|
|
}
|