mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-04 02:04:03 +00:00
I'm sending the review for the mechanism (and an example callsite), and I'll send out a review that makes use of it throughout the scheduler code afterwards. ## What changed? - CHASM scheduler components each parent a new EventLog component, which we'll use to record decision logic details that are normally too spammy for logs. - No plan to expose this through any APIs just yet; it isn't meant to be customer-facing (though, I am thinking about how we can make it easy for dev success to get at) ## Why? - V1 debugging was often done through use of the workflow's event history; without an analogue for V2, it's difficult to get a clear picture of how the logic arrived at a certain output state. - Info logging in the same capacity would likely be too spammy for our observability system. - It's difficult to trace a complete picture for an individual schedule *over time* using just Loki. Schedules are often long-lived, with state transitions taking place in short blips over periods of days or weeks. Being able to get a complete list on demand for a relevant scheduler will greatly improve debugging time. ## How did you test it? - [ ] built - [ ] run locally and tested manually - [ ] covered by existing tests - [x] added new unit test(s) - [ ] added new functional test(s) ## Potential risks - This could increase scheduler state size, however: - V2 already measures notably lighter on storage than V1 (not to mention, no event history data whatsoever) - Log is bounded to max 30 messages per component (usually scheduler, generator, invoker) of max 1000 characters each (e.g., ~100KB), as a safety mechanism - Most messages logged will be small and informational, not large state dumps (e.g., "scheduler unpaused", "generator scheduled for ..."). - Backfillers each get their own log, which shares a Backfiller's lifecycle.
93 lines
2.7 KiB
Go
93 lines
2.7 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"time"
|
|
|
|
"go.temporal.io/server/chasm"
|
|
"go.temporal.io/server/chasm/lib/scheduler/gen/schedulerpb/v1"
|
|
"go.temporal.io/server/service/worker/scheduler"
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
)
|
|
|
|
// The Generator component is responsible for buffering actions according
|
|
// to the schedule's specification. Manually requested actions (from an immediate
|
|
// request or backfill) are separately handled in the Backfiller component.
|
|
type Generator struct {
|
|
chasm.UnimplementedComponent
|
|
|
|
*schedulerpb.GeneratorState
|
|
|
|
Scheduler chasm.ParentPtr[*Scheduler]
|
|
|
|
EventLog chasm.Field[*EventLog]
|
|
}
|
|
|
|
// NewGenerator returns an initialized Generator component, which should
|
|
// be parented under a Scheduler root node.
|
|
func NewGenerator(ctx chasm.MutableContext) *Generator {
|
|
generator := newGeneratorWithState(ctx, &schedulerpb.GeneratorState{
|
|
LastProcessedTime: nil,
|
|
})
|
|
// Kick off initial generator run as an immediate task.
|
|
generator.Generate(ctx)
|
|
return generator
|
|
}
|
|
|
|
func newGeneratorWithState(ctx chasm.MutableContext, state *schedulerpb.GeneratorState) *Generator {
|
|
generator := &Generator{
|
|
GeneratorState: state,
|
|
EventLog: chasm.NewComponentField(ctx, NewEventLog(ctx)),
|
|
}
|
|
return generator
|
|
}
|
|
|
|
// Generate immediately kicks off a new GeneratorTask. Used after updating the
|
|
// schedule specification.
|
|
func (g *Generator) Generate(ctx chasm.MutableContext) {
|
|
g.scheduleTask(ctx, chasm.TaskScheduledTimeImmediate)
|
|
}
|
|
|
|
// scheduleTask schedules a GeneratorTask at the given time.
|
|
func (g *Generator) scheduleTask(ctx chasm.MutableContext, scheduledTime time.Time) {
|
|
ctx.AddTask(g, chasm.TaskAttributes{
|
|
ScheduledTime: scheduledTime,
|
|
}, &schedulerpb.GeneratorTask{})
|
|
}
|
|
|
|
func (g *Generator) LifecycleState(ctx chasm.Context) chasm.LifecycleState {
|
|
return chasm.LifecycleStateRunning
|
|
}
|
|
|
|
// UpdateFutureActionTimes computes and stores the next scheduled action times.
|
|
func (g *Generator) UpdateFutureActionTimes(
|
|
ctx chasm.Context,
|
|
specBuilder *scheduler.SpecBuilder,
|
|
) {
|
|
sched := g.Scheduler.Get(ctx)
|
|
spec, err := sched.getCompiledSpec(specBuilder)
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
count := recentActionCount
|
|
if sched.Schedule.State.LimitedActions {
|
|
count = min(int(sched.Schedule.State.RemainingActions), recentActionCount)
|
|
}
|
|
|
|
futureTimes := make([]*timestamppb.Timestamp, 0, count)
|
|
// Start from max(now, updateTime) to ensure we skip times before the last update.
|
|
t := ctx.Now(g)
|
|
if updateTime := sched.Info.GetUpdateTime().AsTime(); updateTime.After(t) {
|
|
t = updateTime
|
|
}
|
|
for len(futureTimes) < count {
|
|
t = spec.GetNextTime(sched.jitterSeed(), t).Next
|
|
if t.IsZero() {
|
|
break
|
|
}
|
|
futureTimes = append(futureTimes, timestamppb.New(t))
|
|
}
|
|
|
|
g.FutureActionTimes = futureTimes
|
|
}
|