Skip to content

Commit

Permalink
Only log hearbeat if status changed
Browse files Browse the repository at this point in the history
Right now it will log this every five seconds, which really clobbers my
terminal/logs, mostly with fairly useless debug messages:

	 15:27 INFO  producer: Heartbeat
		     num_completed_jobs = 0
		     num_jobs_running   = 0
		     queue              = default
	 15:27 INFO  producer: Heartbeat
		     num_completed_jobs = 0
		     num_jobs_running   = 0
		     queue              = default
	 15:27 INFO  producer: Heartbeat
		     num_completed_jobs = 0
		     num_jobs_running   = 0
		     queue              = default
	 15:27 INFO  producer: Heartbeat
		     num_completed_jobs = 0
		     num_jobs_running   = 0
		     queue              = default

I originally considered changing the InfoContext() to DebugContext(),
but doing an Info log only if the status changed seemed like a good
middle ground.
  • Loading branch information
arp242 committed Aug 18, 2024
1 parent e97f2a3 commit 96118cd
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions producer.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,25 @@ func (p *producer) dispatchWork(workCtx context.Context, count int, fetchResultC
func (p *producer) heartbeatLogLoop(ctx context.Context) {
ticker := time.NewTicker(5 * time.Second)
defer ticker.Stop()
type jobState struct {
ran uint64
active int
}
var prevState jobState
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
p.Logger.InfoContext(ctx, p.Name+": Heartbeat",
slog.Uint64("num_completed_jobs", p.numJobsRan.Load()),
slog.Int("num_jobs_running", int(p.numJobsActive.Load())),
slog.String("queue", p.config.Queue),
)
curState := jobState{ran: p.numJobsRan.Load(), active: int(p.numJobsActive.Load())}
if curState != prevState {
p.Logger.InfoContext(ctx, p.Name+": producer job state changed",
slog.Uint64("num_completed_jobs", curState.ran),
slog.Int("num_jobs_running", curState.active),
slog.String("queue", p.config.Queue),
)
}
prevState = curState
}
}
}
Expand Down

0 comments on commit 96118cd

Please sign in to comment.