mirror of
https://github.com/hanzoai/tasks.git
synced 2026-10-30 14:56:19 +00:00
- Make task executor return values a struct to comply with golang convention
97 lines
3.1 KiB
Go
97 lines
3.1 KiB
Go
// The MIT License
|
|
//
|
|
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
|
|
//
|
|
// Copyright (c) 2020 Uber Technologies, Inc.
|
|
//
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
// in the Software without restriction, including without limitation the rights
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
// furnished to do so, subject to the following conditions:
|
|
//
|
|
// The above copyright notice and this permission notice shall be included in
|
|
// all copies or substantial portions of the Software.
|
|
//
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
// THE SOFTWARE.
|
|
|
|
package queues
|
|
|
|
import (
|
|
"context"
|
|
|
|
"go.temporal.io/server/common/headers"
|
|
"go.temporal.io/server/common/log"
|
|
"go.temporal.io/server/common/log/tag"
|
|
"go.temporal.io/server/common/namespace"
|
|
)
|
|
|
|
type (
|
|
activeStandbyExecutor struct {
|
|
currentClusterName string
|
|
registry namespace.Registry
|
|
activeExecutor Executor
|
|
standbyExecutor Executor
|
|
logger log.Logger
|
|
}
|
|
)
|
|
|
|
func NewActiveStandbyExecutor(
|
|
currentClusterName string,
|
|
registry namespace.Registry,
|
|
activeExecutor Executor,
|
|
standbyExecutor Executor,
|
|
logger log.Logger,
|
|
) Executor {
|
|
return &activeStandbyExecutor{
|
|
currentClusterName: currentClusterName,
|
|
registry: registry,
|
|
activeExecutor: activeExecutor,
|
|
standbyExecutor: standbyExecutor,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (e *activeStandbyExecutor) Execute(
|
|
ctx context.Context,
|
|
executable Executable,
|
|
) ExecuteResponse {
|
|
if e.isActiveTask(executable) {
|
|
return e.activeExecutor.Execute(ctx, executable)
|
|
}
|
|
|
|
// for standby tasks, use preemptable callerType to avoid impacting active traffic
|
|
return e.standbyExecutor.Execute(
|
|
headers.SetCallerType(ctx, headers.CallerTypePreemptable),
|
|
executable,
|
|
)
|
|
}
|
|
|
|
func (e *activeStandbyExecutor) isActiveTask(
|
|
executable Executable,
|
|
) bool {
|
|
// Following is the existing task allocator logic for verifying active task
|
|
|
|
namespaceID := executable.GetNamespaceID()
|
|
entry, err := e.registry.GetNamespaceByID(namespace.ID(namespaceID))
|
|
if err != nil {
|
|
e.logger.Warn("Unable to find namespace, process task as active.", tag.WorkflowNamespaceID(namespaceID), tag.Value(executable.GetTask()))
|
|
return true
|
|
}
|
|
|
|
if !entry.ActiveInCluster(e.currentClusterName) {
|
|
e.logger.Debug("Process task as standby.", tag.WorkflowNamespaceID(namespaceID), tag.Value(executable.GetTask()))
|
|
return false
|
|
}
|
|
|
|
e.logger.Debug("Process task as active.", tag.WorkflowNamespaceID(namespaceID), tag.Value(executable.GetTask()))
|
|
return true
|
|
}
|