mirror of
https://github.com/hanzoai/tasks.git
synced 2026-11-03 03:56:04 +00:00
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).
117 lines
3.6 KiB
Go
117 lines
3.6 KiB
Go
package worker
|
|
|
|
import (
|
|
"context"
|
|
"sync/atomic"
|
|
|
|
sdkworker "go.temporal.io/sdk/worker"
|
|
"github.com/hanzoai/tasks/common"
|
|
"github.com/hanzoai/tasks/common/headers"
|
|
"github.com/hanzoai/tasks/common/log"
|
|
"github.com/hanzoai/tasks/common/log/tag"
|
|
"github.com/hanzoai/tasks/common/membership"
|
|
"github.com/hanzoai/tasks/common/primitives"
|
|
"github.com/hanzoai/tasks/common/sdk"
|
|
workercommon "github.com/hanzoai/tasks/service/worker/common"
|
|
)
|
|
|
|
type (
|
|
// workerManager maintains list of SDK workers.
|
|
workerManager struct {
|
|
status int32
|
|
hostInfo membership.HostInfo
|
|
logger log.Logger
|
|
sdkClientFactory sdk.ClientFactory
|
|
workers []sdkworker.Worker
|
|
workerComponents []workercommon.WorkerComponent
|
|
}
|
|
)
|
|
|
|
// NewWorkerManager creates a new worker manager. The workerComponents argument must be first in order for the fx param
|
|
// tag to work correctly.
|
|
func NewWorkerManager(
|
|
workerComponents []workercommon.WorkerComponent,
|
|
logger log.Logger,
|
|
sdkClientFactory sdk.ClientFactory,
|
|
hostInfo membership.HostInfo,
|
|
) *workerManager {
|
|
return &workerManager{
|
|
hostInfo: hostInfo,
|
|
logger: logger,
|
|
sdkClientFactory: sdkClientFactory,
|
|
workerComponents: workerComponents,
|
|
}
|
|
}
|
|
|
|
func (wm *workerManager) Start() {
|
|
if !atomic.CompareAndSwapInt32(
|
|
&wm.status,
|
|
common.DaemonStatusInitialized,
|
|
common.DaemonStatusStarted,
|
|
) {
|
|
return
|
|
}
|
|
|
|
defaultWorkerOptions := sdkworker.Options{
|
|
Identity: "temporal-system@" + wm.hostInfo.Identity(),
|
|
// TODO: add dynamic config for worker options
|
|
BackgroundActivityContext: headers.SetCallerType(context.Background(), headers.CallerTypeBackgroundHigh),
|
|
}
|
|
sdkClient := wm.sdkClientFactory.GetSystemClient()
|
|
defaultWorker := wm.sdkClientFactory.NewWorker(sdkClient, primitives.DefaultWorkerTaskQueue, defaultWorkerOptions)
|
|
wm.workers = []sdkworker.Worker{defaultWorker}
|
|
|
|
for _, wc := range wm.workerComponents {
|
|
wfWorkerOptions := wc.DedicatedWorkflowWorkerOptions()
|
|
if wfWorkerOptions == nil {
|
|
// use default worker
|
|
wc.RegisterWorkflow(defaultWorker)
|
|
} else {
|
|
wfWorkerOptions.Options.Identity = "temporal-system@" + wm.hostInfo.Identity()
|
|
// this worker component requires a dedicated worker
|
|
dedicatedWorker := wm.sdkClientFactory.NewWorker(sdkClient, wfWorkerOptions.TaskQueue, wfWorkerOptions.Options)
|
|
wc.RegisterWorkflow(dedicatedWorker)
|
|
wm.workers = append(wm.workers, dedicatedWorker)
|
|
}
|
|
|
|
activityWorkerOptions := wc.DedicatedActivityWorkerOptions()
|
|
if activityWorkerOptions == nil {
|
|
// use default worker
|
|
wc.RegisterActivities(defaultWorker)
|
|
} else {
|
|
// TODO: This is to prevent issues during upgrade/downgrade. Remove in 1.24 release.
|
|
wc.RegisterActivities(defaultWorker)
|
|
|
|
// this worker component requires a dedicated worker for activities
|
|
activityWorkerOptions.Options.DisableWorkflowWorker = true
|
|
activityWorkerOptions.Options.Identity = "temporal-system@" + wm.hostInfo.Identity()
|
|
activityWorker := wm.sdkClientFactory.NewWorker(sdkClient, activityWorkerOptions.TaskQueue, activityWorkerOptions.Options)
|
|
wc.RegisterActivities(activityWorker)
|
|
wm.workers = append(wm.workers, activityWorker)
|
|
}
|
|
}
|
|
|
|
for _, w := range wm.workers {
|
|
if err := w.Start(); err != nil {
|
|
wm.logger.Fatal("Unable to start worker", tag.Error(err))
|
|
}
|
|
}
|
|
|
|
wm.logger.Info("", tag.ComponentWorkerManager, tag.LifeCycleStarted)
|
|
}
|
|
|
|
func (wm *workerManager) Stop() {
|
|
if !atomic.CompareAndSwapInt32(
|
|
&wm.status,
|
|
common.DaemonStatusStarted,
|
|
common.DaemonStatusStopped,
|
|
) {
|
|
return
|
|
}
|
|
|
|
for _, w := range wm.workers {
|
|
w.Stop()
|
|
}
|
|
wm.logger.Info("", tag.ComponentWorkerManager, tag.LifeCycleStopped)
|
|
}
|