Files
tasks/service/worker/scheduler/fx.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

138 lines
4.9 KiB
Go

package scheduler
import (
"fmt"
"math"
"time"
"go.temporal.io/api/workflowservice/v1"
sdkworker "go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
schedulespb "github.com/hanzoai/tasks/api/schedule/v1"
"github.com/hanzoai/tasks/chasm"
schedulerpb "github.com/hanzoai/tasks/chasm/lib/scheduler/gen/schedulerpb/v1"
"github.com/hanzoai/tasks/common/dynamicconfig"
"github.com/hanzoai/tasks/common/log"
"github.com/hanzoai/tasks/common/metrics"
"github.com/hanzoai/tasks/common/namespace"
"github.com/hanzoai/tasks/common/quotas"
"github.com/hanzoai/tasks/common/resource"
"github.com/hanzoai/tasks/common/searchattribute/sadefs"
workercommon "github.com/hanzoai/tasks/service/worker/common"
"go.uber.org/fx"
)
const (
WorkflowType = "temporal-sys-scheduler-workflow"
NamespaceDivision = "TemporalScheduler"
)
// VisibilityListQueryV1 selects only V1 scheduler workflows.
// Used by listSchedulesWorkflow which calls ListWorkflowExecutions without archetype ID.
var VisibilityListQueryV1 = fmt.Sprintf(
"%s = '%s' AND %s = 'Running'",
sadefs.TemporalNamespaceDivision,
NamespaceDivision,
sadefs.ExecutionStatus,
)
// VisibilityListQueryChasm selects both V1 scheduler and CHASM scheduler.
// Used by listSchedulesChasm which calls chasm.ListExecutions with archetype ID set,
// allowing TemporalSystemExecutionStatus to be translated to ExecutionStatus.
var VisibilityListQueryChasm = fmt.Sprintf(
"((%s = '%s' AND TemporalSystemExecutionStatus = 'Running') OR (%s = '%d' AND ExecutionStatus = 'Running'))",
sadefs.TemporalNamespaceDivision,
NamespaceDivision,
sadefs.TemporalNamespaceDivision,
chasm.SchedulerArchetypeID,
)
type (
workerComponent struct {
specBuilder *SpecBuilder // workflow dep
activityDeps activityDeps
enabledForNs dynamicconfig.BoolPropertyFnWithNamespaceFilter
enableCHASMMigration dynamicconfig.BoolPropertyFnWithNamespaceFilter
globalNSStartWorkflowRPS dynamicconfig.TypedSubscribableWithNamespaceFilter[float64]
maxBlobSize dynamicconfig.IntPropertyFnWithNamespaceFilter
localActivitySleepLimit dynamicconfig.DurationPropertyFnWithNamespaceFilter
}
activityDeps struct {
fx.In
MetricsHandler metrics.Handler
Logger log.Logger
HistoryClient resource.HistoryClient
FrontendClient workflowservice.WorkflowServiceClient
SchedulerClient schedulerpb.SchedulerServiceClient
}
fxResult struct {
fx.Out
Component workercommon.PerNSWorkerComponent `group:"perNamespaceWorkerComponent"`
}
)
var Module = fx.Options(
fx.Provide(NewResult),
// SpecBuilder is provided as part of chasm Scheduler module at top level.
)
func NewResult(
dc *dynamicconfig.Collection,
specBuilder *SpecBuilder,
params activityDeps,
) fxResult {
return fxResult{
Component: &workerComponent{
specBuilder: specBuilder,
activityDeps: params,
enabledForNs: dynamicconfig.WorkerEnableScheduler.Get(dc),
enableCHASMMigration: dynamicconfig.EnableCHASMSchedulerMigration.Get(dc),
globalNSStartWorkflowRPS: dynamicconfig.SchedulerNamespaceStartWorkflowRPS.Subscribe(dc),
maxBlobSize: dynamicconfig.BlobSizeLimitError.Get(dc),
localActivitySleepLimit: dynamicconfig.SchedulerLocalActivitySleepLimit.Get(dc),
},
}
}
func (s *workerComponent) DedicatedWorkerOptions(ns *namespace.Namespace) *workercommon.PerNSDedicatedWorkerOptions {
return &workercommon.PerNSDedicatedWorkerOptions{
Enabled: s.enabledForNs(ns.Name().String()),
}
}
func (s *workerComponent) Register(registry sdkworker.Registry, ns *namespace.Namespace, details workercommon.RegistrationDetails) func() {
enableMigration := s.enableCHASMMigration(ns.Name().String())
wfFunc := func(ctx workflow.Context, args *schedulespb.StartScheduleArgs) error {
return schedulerWorkflowWithSpecBuilder(ctx, args, s.specBuilder, enableMigration)
}
registry.RegisterWorkflowWithOptions(wfFunc, workflow.RegisterOptions{Name: WorkflowType})
activities, cleanup := s.newActivities(ns.Name(), ns.ID(), details)
registry.RegisterActivity(activities)
return cleanup
}
func (s *workerComponent) newActivities(name namespace.Name, id namespace.ID, details workercommon.RegistrationDetails) (*activities, func()) {
const burstRatio = 1.0
lim := quotas.NewRateLimiter(1, 1)
cb := func(rps float64) {
localRPS := rps * float64(details.Multiplicity) / float64(details.TotalWorkers)
burst := max(1, int(math.Ceil(localRPS*burstRatio)))
lim.SetRateBurst(localRPS, burst)
}
initialRPS, cancel := s.globalNSStartWorkflowRPS(name.String(), cb)
cb(initialRPS)
return &activities{
activityDeps: s.activityDeps,
namespace: name,
namespaceID: id,
startWorkflowRateLimiter: lim,
maxBlobSize: func() int { return s.maxBlobSize(name.String()) },
localActivitySleepLimit: func() time.Duration { return s.localActivitySleepLimit(name.String()) },
}, cancel
}