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"
|
2018-08-26 16:49:40 +02:00
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
"syscall"
|
2017-09-23 18:20:22 +02:00
|
|
|
"time"
|
2018-08-26 21:58:58 +02:00
|
|
|
"github.com/zrepl/zrepl/cmd/daemon"
|
|
|
|
"github.com/zrepl/zrepl/cmd/daemon/job"
|
|
|
|
"github.com/zrepl/zrepl/logger"
|
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
|
2018-04-05 22:22:55 +02:00
|
|
|
JobType() JobType
|
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
|
|
|
|
2018-04-05 22:22:55 +02:00
|
|
|
type JobType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
JobTypePull JobType = "pull"
|
|
|
|
JobTypeSource JobType = "source"
|
|
|
|
JobTypeLocal JobType = "local"
|
2018-04-13 23:37:49 +02:00
|
|
|
JobTypePrometheus JobType = "prometheus"
|
2018-04-05 22:22:55 +02:00
|
|
|
JobTypeControl JobType = "control"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ParseUserJobType(s string) (JobType, error) {
|
|
|
|
switch s {
|
|
|
|
case "pull":
|
|
|
|
return JobTypePull, nil
|
|
|
|
case "source":
|
|
|
|
return JobTypeSource, nil
|
|
|
|
case "local":
|
|
|
|
return JobTypeLocal, nil
|
2018-04-13 23:37:49 +02:00
|
|
|
case "prometheus":
|
|
|
|
return JobTypePrometheus, nil
|
2018-04-05 22:22:55 +02:00
|
|
|
}
|
|
|
|
return "", fmt.Errorf("unknown job type '%s'", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j JobType) String() string {
|
|
|
|
return string(j)
|
|
|
|
}
|
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
type daemonJobAdaptor struct {
|
|
|
|
j Job
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a daemonJobAdaptor) Name() string {
|
|
|
|
return a.j.JobName()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a daemonJobAdaptor) Run(ctx context.Context) {
|
|
|
|
a.j.JobStart(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a daemonJobAdaptor) Status() interface{} { return nil }
|
|
|
|
|
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 {
|
2018-02-17 17:43:55 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "error parsing config: %s\n", 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)
|
2018-08-26 14:58:57 +02:00
|
|
|
ctx := WithLogger(context.Background(), log)
|
2017-09-22 14:02:07 +02:00
|
|
|
|
2018-08-26 21:58:58 +02:00
|
|
|
daemonJobs := make([]job.Job, 0, len(conf.Jobs))
|
|
|
|
for i := range conf.Jobs {
|
|
|
|
daemonJobs = append(daemonJobs, daemonJobAdaptor{conf.Jobs[i]})
|
|
|
|
}
|
|
|
|
daemon.Run(ctx, conf.Global.Control.Sockpath, conf.Global.logging.Outlets, daemonJobs)
|
2017-09-17 18:20:05 +02:00
|
|
|
|
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 (
|
2017-12-24 15:35:12 +01:00
|
|
|
contextKeyLog contextKey = contextKey("log")
|
|
|
|
contextKeyDaemon contextKey = contextKey("daemon")
|
2017-09-13 23:27:18 +02:00
|
|
|
)
|
2017-09-10 16:13:05 +02:00
|
|
|
|
2018-08-10 17:06:00 +02:00
|
|
|
func getLogger(ctx context.Context) Logger {
|
|
|
|
return ctx.Value(contextKeyLog).(Logger)
|
|
|
|
}
|
|
|
|
|
2018-08-26 14:58:57 +02:00
|
|
|
func WithLogger(ctx context.Context, l Logger) context.Context {
|
|
|
|
return context.WithValue(ctx, contextKeyLog, l)
|
|
|
|
}
|
|
|
|
|
2017-09-13 23:27:18 +02:00
|
|
|
type Daemon struct {
|
2017-12-24 15:34:41 +01:00
|
|
|
conf *Config
|
|
|
|
startedAt time.Time
|
2017-09-17 18:20:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewDaemon(initialConf *Config) *Daemon {
|
2017-12-24 15:34:41 +01:00
|
|
|
return &Daemon{conf: initialConf}
|
2017-09-13 23:27:18 +02:00
|
|
|
}
|
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
func (d *Daemon) Loop(ctx context.Context) {
|
|
|
|
|
2017-12-24 15:34:41 +01:00
|
|
|
d.startedAt = time.Now()
|
|
|
|
|
2018-08-26 14:58:57 +02:00
|
|
|
log := getLogger(ctx)
|
2017-09-13 23:27:18 +02:00
|
|
|
|
2017-09-17 18:20:05 +02:00
|
|
|
ctx, cancel := context.WithCancel(ctx)
|
2017-12-24 15:35:12 +01:00
|
|
|
ctx = context.WithValue(ctx, contextKeyDaemon, d)
|
2017-09-17 18:20:05 +02:00
|
|
|
|
|
|
|
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++
|
2018-08-26 14:58:57 +02:00
|
|
|
jobCtx := WithLogger(ctx, 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-30 16:39:52 +02:00
|
|
|
cancel() // make go vet happy
|
2017-09-13 23:27:18 +02:00
|
|
|
|
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
|
|
|
}
|