mirror of
https://github.com/zrepl/zrepl.git
synced 2025-02-22 05:11:06 +01:00
cmd: clean up usage of contextKeyLog through getter and setter functions
This commit is contained in:
parent
666ead2646
commit
f6be5b776b
@ -46,7 +46,7 @@ const (
|
||||
|
||||
func (j *ControlJob) JobStart(ctx context.Context) {
|
||||
|
||||
log := ctx.Value(contextKeyLog).(Logger)
|
||||
log := getLogger(ctx)
|
||||
defer log.Info("control job finished")
|
||||
|
||||
daemon := ctx.Value(contextKeyDaemon).(*Daemon)
|
||||
|
@ -84,7 +84,7 @@ func (j *LocalJob) JobType() JobType { return JobTypeLocal }
|
||||
|
||||
func (j *LocalJob) JobStart(ctx context.Context) {
|
||||
|
||||
rootLog := ctx.Value(contextKeyLog).(Logger)
|
||||
rootLog := getLogger(ctx)
|
||||
|
||||
j.snapperTask = NewTask("snapshot", j, rootLog)
|
||||
j.mainTask = NewTask("main", j, rootLog)
|
||||
|
@ -69,7 +69,7 @@ func (j *PrometheusJob) JobStart(ctx context.Context) {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
log := ctx.Value(contextKeyLog).(Logger)
|
||||
log := getLogger(ctx)
|
||||
task := NewTask("main", j, log)
|
||||
log = task.Log()
|
||||
|
||||
|
@ -105,7 +105,7 @@ func (j *PullJob) JobType() JobType { return JobTypePull }
|
||||
|
||||
func (j *PullJob) JobStart(ctx context.Context) {
|
||||
|
||||
log := ctx.Value(contextKeyLog).(Logger)
|
||||
log := getLogger(ctx)
|
||||
defer log.Info("exiting")
|
||||
j.task = NewTask("main", j, log)
|
||||
|
||||
|
@ -89,7 +89,7 @@ func (j *SourceJob) JobType() JobType { return JobTypeSource }
|
||||
|
||||
func (j *SourceJob) JobStart(ctx context.Context) {
|
||||
|
||||
log := ctx.Value(contextKeyLog).(Logger)
|
||||
log := getLogger(ctx)
|
||||
defer log.Info("exiting")
|
||||
|
||||
j.autosnapTask = NewTask("autosnap", j, log)
|
||||
@ -196,7 +196,6 @@ outer:
|
||||
}
|
||||
|
||||
return
|
||||
|
||||
}
|
||||
|
||||
func (j *SourceJob) handleConnection(conn net.Conn, task *Task) {
|
||||
@ -209,7 +208,7 @@ func (j *SourceJob) handleConnection(conn net.Conn, task *Task) {
|
||||
senderEP := endpoint.NewSender(j.Filesystems, NewPrefixFilter(j.SnapshotPrefix))
|
||||
|
||||
ctx := context.Background()
|
||||
ctx = context.WithValue(ctx, contextKeyLog, task.Log().WithField(logSubsysField, "rpc.endpoint"))
|
||||
ctx = endpoint.WithLogger(ctx, task.Log().WithField(logSubsysField, "rpc.endpoint"))
|
||||
ctx = streamrpc.ContextWithLogger(ctx, streamrpcLogAdaptor{task.Log().WithField(logSubsysField, "rpc.protocol")})
|
||||
handler := endpoint.NewHandler(senderEP)
|
||||
if err := streamrpc.ServeConn(ctx, conn, STREAMRPC_CONFIG, handler.Handle); err != nil {
|
||||
|
@ -73,8 +73,7 @@ func doDaemon(cmd *cobra.Command, args []string) {
|
||||
|
||||
log.Info(NewZreplVersionInformation().String())
|
||||
log.Debug("starting daemon")
|
||||
ctx := context.WithValue(context.Background(), contextKeyLog, log)
|
||||
ctx = context.WithValue(ctx, contextKeyLog, log)
|
||||
ctx := WithLogger(context.Background(), log)
|
||||
|
||||
d := NewDaemon(conf)
|
||||
d.Loop(ctx)
|
||||
@ -92,6 +91,10 @@ func getLogger(ctx context.Context) Logger {
|
||||
return ctx.Value(contextKeyLog).(Logger)
|
||||
}
|
||||
|
||||
func WithLogger(ctx context.Context, l Logger) context.Context {
|
||||
return context.WithValue(ctx, contextKeyLog, l)
|
||||
}
|
||||
|
||||
type Daemon struct {
|
||||
conf *Config
|
||||
startedAt time.Time
|
||||
@ -105,7 +108,7 @@ func (d *Daemon) Loop(ctx context.Context) {
|
||||
|
||||
d.startedAt = time.Now()
|
||||
|
||||
log := ctx.Value(contextKeyLog).(Logger)
|
||||
log := getLogger(ctx)
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
ctx = context.WithValue(ctx, contextKeyDaemon, d)
|
||||
@ -121,7 +124,7 @@ func (d *Daemon) Loop(ctx context.Context) {
|
||||
logger := log.WithField(logJobField, job.JobName())
|
||||
logger.Info("starting")
|
||||
i++
|
||||
jobCtx := context.WithValue(ctx, contextKeyLog, logger)
|
||||
jobCtx := WithLogger(ctx, logger)
|
||||
go func(j Job) {
|
||||
j.JobStart(jobCtx)
|
||||
finishs <- j
|
||||
|
@ -179,7 +179,7 @@ func doTestPrunePolicy(cmd *cobra.Command, args []string) {
|
||||
|
||||
log.Printf("start pruning")
|
||||
|
||||
ctx := context.WithValue(context.Background(), contextKeyLog, log)
|
||||
ctx := WithLogger(context.Background(), log)
|
||||
result, err := pruner.Run(ctx)
|
||||
if err != nil {
|
||||
log.Printf("error running pruner: %s", err)
|
||||
|
Loading…
Reference in New Issue
Block a user