Files
tasks/chasm/lib/scheduler/util.go
zeekay 5a601d081b rebrand: go.temporal.io/server -> github.com/hanzoai/tasks
Full module path rebrand from Temporal to Hanzo Tasks:
- go.mod module: github.com/hanzoai/tasks
- All internal Go imports updated (1906 files)
- cmd/server renamed to cmd/tasksd
- Binary name: tasksd (was temporal-server)
- Docker images: hanzoai/tasks (was temporalio/server)
- README rewritten with Hanzo Tasks branding
- LLM.md created with integration notes
- Makefile, goreleaser, CI workflows updated
- go.temporal.io/sdk and go.temporal.io/api imports preserved (separate repos)

Fork of Temporal.io (MIT license).
2026-03-19 10:36:40 -07:00

80 lines
2.6 KiB
Go

package scheduler
import (
"encoding/binary"
"time"
"github.com/hanzoai/tasks/common/log"
"github.com/hanzoai/tasks/common/log/tag"
"github.com/hanzoai/tasks/common/metrics"
schedulescommon "github.com/hanzoai/tasks/common/schedules"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/timestamppb"
)
func generateRequestID(scheduler *Scheduler, backfillID string, nominal, actual time.Time) string {
return schedulescommon.GenerateRequestID(
scheduler.NamespaceId,
scheduler.ScheduleId,
scheduler.ConflictToken,
backfillID,
nominal,
actual,
)
}
// serializeConflictToken serializes a conflict token as a byte slice.
func serializeConflictToken(conflictToken int64) []byte {
token := make([]byte, 8)
binary.LittleEndian.PutUint64(token, uint64(conflictToken))
return token
}
// newTaggedLogger returns a logger tagged with the Scheduler's attributes.
func newTaggedLogger(baseLogger log.Logger, scheduler *Scheduler) log.Logger {
return log.With(
baseLogger,
tag.WorkflowNamespace(scheduler.Namespace),
tag.ScheduleID(scheduler.ScheduleId),
)
}
// newTaggedMetricsHandler returns a metrics handler tagged with the Scheduler's namespace and backend.
func newTaggedMetricsHandler(baseHandler metrics.Handler, scheduler *Scheduler) metrics.Handler {
return baseHandler.WithTags(
metrics.NamespaceTag(scheduler.Namespace),
metrics.StringTag(metrics.ScheduleBackendTag, metrics.ScheduleBackendChasm),
)
}
// validateTaskHighWaterMark validates a component's lastProcessedTime against a
// task timestamp. A task is valid if its scheduled time is after the high water mark.
// Immediate tasks (zero scheduled time) are always valid since they execute inline.
func validateTaskHighWaterMark(
lastProcessedTime *timestamppb.Timestamp,
scheduledAt time.Time,
) (bool, error) {
// Immediate tasks are always valid - they execute inline during the transaction.
if scheduledAt.IsZero() {
return true, nil
}
// If lastProcessedTime is not set, all scheduled tasks are valid.
if lastProcessedTime == nil || (lastProcessedTime.GetSeconds() == 0 && lastProcessedTime.GetNanos() == 0) {
return true, nil
}
// Scheduled tasks are valid if their time is after the high water mark.
return scheduledAt.After(lastProcessedTime.AsTime()), nil
}
// jsonStringer wraps a proto.Message for lazy JSON serialization. Intended for
// debug logging structures.
type jsonStringer struct {
proto.Message
}
func (j jsonStringer) String() string {
json, _ := protojson.Marshal(j.Message)
return string(json)
}