mirror of
https://github.com/rcourtman/Pulse.git
synced 2026-11-30 17:42:32 +01:00
Read the active queues instead of publishing heap scans from poll workers. A quiet queue still crosses the due-soon boundary, and unrelated provider results cannot reset the process depth. Change-source: pulse-maintainer
106 lines
3.0 KiB
Go
106 lines
3.0 KiB
Go
package monitoring
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/rcourtman/pulse-go-rewrite/internal/logging"
|
|
"github.com/rs/zerolog"
|
|
"github.com/rs/zerolog/log"
|
|
)
|
|
|
|
func (m *Monitor) pollWorkProgress() *pollWorkProgress {
|
|
if m.pollMetrics == nil {
|
|
return nil
|
|
}
|
|
return m.pollMetrics.workProgress
|
|
}
|
|
|
|
func (m *Monitor) dispatchTaskWorkers(ctx context.Context, limiter *pollTaskWorkerLimiter) {
|
|
defer recoverFromPanic("taskWorkerDispatcher")
|
|
defer m.pollMetrics.trackTaskQueue(m.taskQueue)()
|
|
progress := m.pollWorkProgress()
|
|
|
|
for {
|
|
task, ok := m.taskQueue.WaitNext(ctx)
|
|
if !ok {
|
|
return
|
|
}
|
|
// The task is no longer in the heap, but may not get a process slot.
|
|
budget := m.taskExecutionTimeout(task.InstanceType)
|
|
due := task.NextRun
|
|
if due.IsZero() {
|
|
due = time.Now()
|
|
}
|
|
work := progress.begin(pollWorkWaitingForSlot, due.Add(budget))
|
|
if !limiter.acquire(ctx) {
|
|
m.taskQueue.completeClaim(task, ScheduledTask{}, noPollReschedule)
|
|
progress.finish(work)
|
|
return
|
|
}
|
|
|
|
progress.start(work, time.Now().Add(budget))
|
|
go func() {
|
|
defer limiter.release()
|
|
m.executeAndRescheduleTask(ctx, task, progress, work)
|
|
}()
|
|
}
|
|
}
|
|
|
|
func (m *Monitor) taskWorker(ctx context.Context, id int) {
|
|
defer recoverFromPanic(fmt.Sprintf("taskWorker-%d", id))
|
|
defer m.pollMetrics.trackTaskQueue(m.taskQueue)()
|
|
progress := m.pollWorkProgress()
|
|
|
|
if logging.IsLevelEnabled(zerolog.DebugLevel) {
|
|
log.Debug().Int("worker", id).Msg("task worker started")
|
|
}
|
|
for {
|
|
task, ok := m.taskQueue.WaitNext(ctx)
|
|
if !ok {
|
|
if logging.IsLevelEnabled(zerolog.DebugLevel) {
|
|
log.Debug().Int("worker", id).Msg("task worker stopping")
|
|
}
|
|
return
|
|
}
|
|
|
|
work := progress.begin(pollWorkRunning, time.Now().Add(m.taskExecutionTimeout(task.InstanceType)))
|
|
m.executeAndRescheduleTask(ctx, task, progress, work)
|
|
}
|
|
}
|
|
|
|
func (m *Monitor) executeAndRescheduleTask(ctx context.Context, task ScheduledTask, progress *pollWorkProgress, work uint64) {
|
|
// Recover at the task boundary so one panic cannot end a reusable worker.
|
|
// The claim and work marker still unwind before this handler runs.
|
|
defer func() {
|
|
if recovered := recover(); recovered != nil {
|
|
log.Error().
|
|
Str("type", string(task.InstanceType)).
|
|
Str("instance", task.InstanceName).
|
|
Interface("panic", recovered).
|
|
Stack().
|
|
Msg("Recovered from panic in scheduled poll task")
|
|
}
|
|
}()
|
|
// Do not clear this at context expiry. An unreturned worker is still busy.
|
|
defer progress.finish(work)
|
|
var next ScheduledTask
|
|
var reschedule pollReschedule
|
|
defer func() { m.taskQueue.completeClaim(task, next, reschedule) }()
|
|
|
|
if ctx.Err() != nil {
|
|
return
|
|
}
|
|
if retryAt := m.scheduledBreakerRetryAt(task); retryAt.After(time.Now()) {
|
|
// A plan or explicit request can be due before the breaker permits a
|
|
// probe. Nothing ran: don't reschedule from the previous result or
|
|
// record a second dead letter for the same failed read.
|
|
next, reschedule = task, pollAfterBackoff
|
|
next.NextRun = retryAt
|
|
return
|
|
}
|
|
m.executeScheduledTask(ctx, task)
|
|
next, reschedule = m.nextScheduledPoll(task)
|
|
}
|