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