mirror of
https://github.com/dagucloud/dagu.git
synced 2026-12-05 05:32:51 +00:00
288 lines
9.6 KiB
Go
288 lines
9.6 KiB
Go
// Copyright (C) 2026 Yota Hamada
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package executor
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"io"
|
|
"os"
|
|
"sync"
|
|
|
|
"github.com/dagucloud/dagu/v2/internal/cmn/cmdutil"
|
|
"github.com/dagucloud/dagu/v2/internal/cmn/logger"
|
|
"github.com/dagucloud/dagu/v2/internal/cmn/logger/tag"
|
|
"github.com/dagucloud/dagu/v2/internal/executor/registry"
|
|
"github.com/dagucloud/dagu/v2/internal/ir"
|
|
)
|
|
|
|
// CloseExecutor safely closes an executor if it implements io.Closer.
|
|
// Returns nil if executor doesn't implement io.Closer or is nil.
|
|
// This should be called after executor.Run() completes to release resources.
|
|
func CloseExecutor(exec Executor) error {
|
|
if exec == nil {
|
|
return nil
|
|
}
|
|
if closer, ok := exec.(io.Closer); ok {
|
|
return closer.Close()
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// Executor is an interface for executing steps in a DAG.
|
|
type Executor interface {
|
|
SetStdout(out io.Writer)
|
|
SetStderr(out io.Writer)
|
|
Kill(sig os.Signal) error
|
|
Run(ctx context.Context) error
|
|
}
|
|
|
|
// Stopper is implemented by executors that can handle lifecycle stop intent
|
|
// directly instead of receiving only a legacy OS signal.
|
|
type Stopper interface {
|
|
Stop(cmdutil.TerminationIntent) error
|
|
}
|
|
|
|
// ExecutorFactory is a function type that creates an Executor based on the step configuration.
|
|
type ExecutorFactory func(ctx context.Context, step ir.Step) (Executor, error)
|
|
|
|
// NewExecutor creates a new Executor based on the step's executor type.
|
|
func NewExecutor(ctx context.Context, step ir.Step) (Executor, error) {
|
|
executorRegistryMu.RLock()
|
|
factory, ok := executorRegistry[step.ExecutorConfig.Type]
|
|
executorRegistryMu.RUnlock()
|
|
if ok {
|
|
return factory(ctx, step)
|
|
}
|
|
|
|
logger.Error(ctx, "Action is not registered",
|
|
tag.Type(step.ExecutorConfig.Type),
|
|
tag.Step(step.Name),
|
|
)
|
|
return nil, fmt.Errorf("action %q is not registered", step.ExecutorConfig.Type)
|
|
}
|
|
|
|
// RegisterExecutor registers a new executor type with its factory, validator, and capabilities.
|
|
func RegisterExecutor(executorType string, factory ExecutorFactory, validator registry.StepValidator, caps registry.ExecutorCapabilities) {
|
|
executorRegistryMu.Lock()
|
|
executorRegistry[executorType] = factory
|
|
executorRegistryMu.Unlock()
|
|
if validator != nil {
|
|
registry.RegisterStepValidator(executorType, validator)
|
|
}
|
|
registry.RegisterExecutorCapabilities(executorType, caps)
|
|
}
|
|
|
|
// UnregisterExecutor removes a registered executor type.
|
|
func UnregisterExecutor(executorType string) {
|
|
executorRegistryMu.Lock()
|
|
delete(executorRegistry, executorType)
|
|
executorRegistryMu.Unlock()
|
|
registry.UnregisterStepValidator(executorType)
|
|
registry.UnregisterExecutorCapabilities(executorType)
|
|
}
|
|
|
|
var executorRegistry = make(map[string]ExecutorFactory)
|
|
|
|
var executorRegistryMu sync.RWMutex
|
|
|
|
// ExitCoder is an interface for executors that can return an exit code.
|
|
type ExitCoder interface {
|
|
ExitCode() int
|
|
}
|
|
|
|
// NodeStatusDeterminer is an interface for reporting the status of a node execution.
|
|
type NodeStatusDeterminer interface {
|
|
DetermineNodeStatus() (ir.NodeStatus, error)
|
|
}
|
|
|
|
// DAGExecutor is an interface for sub DAG executors.
|
|
type DAGExecutor interface {
|
|
Executor
|
|
|
|
// SetParams sets the parameters for running a sub DAG.
|
|
SetParams(RunParams)
|
|
}
|
|
|
|
// ParallelExecutor is an interface for parallel step executors.
|
|
type ParallelExecutor interface {
|
|
Executor
|
|
|
|
// SetParamsList sets the parameters for running multiple sub DAGs in parallel.
|
|
SetParamsList([]RunParams)
|
|
}
|
|
|
|
// NodesExecutor runs a step with nodes as one child run per node (RFC 013).
|
|
type NodesExecutor interface {
|
|
Executor
|
|
|
|
// SetNodes sets the nodes the step matched, in name order.
|
|
SetNodes([]NodeRun)
|
|
}
|
|
|
|
// NodeRun is one node a step with nodes matched.
|
|
type NodeRun struct {
|
|
// Node is the node's name.
|
|
Node string
|
|
// Disabled reports a node that does not run. It has a Run only when an
|
|
// earlier attempt of the step gave it a child run.
|
|
Disabled bool
|
|
// Removed reports a node of a retried step that has left the inventory.
|
|
// It does not run.
|
|
Removed bool
|
|
// Reuse reports a node of a retried step whose child run succeeded. The
|
|
// step keeps that run's result without running it again.
|
|
Reuse bool
|
|
// Worker reports a worker node, whose child run runs on that worker.
|
|
Worker bool
|
|
// Run starts the child run bound to the node.
|
|
Run RunParams
|
|
}
|
|
|
|
// Runs reports whether the node has a child run in the step: one that runs,
|
|
// or one whose earlier result the step keeps.
|
|
func (n NodeRun) Runs() bool {
|
|
return !n.Disabled && !n.Removed
|
|
}
|
|
|
|
// RunParams holds the parameters for running a sub DAG.
|
|
type RunParams struct {
|
|
RunID string
|
|
Params string
|
|
ParallelItem string
|
|
DAGName string
|
|
WorkerSelector map[string]string
|
|
// PassedEnv holds resolved "KEY=value" pairs the parent opted to share
|
|
// with the child run via the step's pass_env field.
|
|
PassedEnv []string
|
|
// Node names the node the child run is bound to (RFC 013).
|
|
Node string
|
|
// TargetWorkerID names the worker the child run runs on: the worker node
|
|
// it is bound to.
|
|
TargetWorkerID string
|
|
}
|
|
|
|
// ChatMessageHandler is an interface for executors that handle chat session messages.
|
|
type ChatMessageHandler interface {
|
|
SetContext([]ir.LLMMessage)
|
|
GetMessages() []ir.LLMMessage
|
|
}
|
|
|
|
// AgentSessionHandler exchanges durable managed-agent state with the runtime.
|
|
type AgentSessionHandler interface {
|
|
SetAgentSession(*ir.AgentSession)
|
|
GetAgentSession() *ir.AgentSession
|
|
}
|
|
|
|
// ProgressCallbackAware reports executor side-channel changes during execution.
|
|
type ProgressCallbackAware interface {
|
|
SetProgressCallback(func())
|
|
}
|
|
|
|
// PushBackAware is implemented by executors that can incorporate
|
|
// push-back feedback into their conversation flow. It is called only for a step
|
|
// that ran before the push-back.
|
|
type PushBackAware interface {
|
|
SetPushBackContext(inputs map[string]string, iteration int)
|
|
}
|
|
|
|
// PushBackPreviousStdoutAware is implemented by executors that can consume the
|
|
// previous stdout log path for push-back re-execution.
|
|
type PushBackPreviousStdoutAware interface {
|
|
SetPushBackPreviousStdout(path string)
|
|
}
|
|
|
|
// RepeatCheck describes the failed until check that made a step repeat.
|
|
type RepeatCheck struct {
|
|
Attempt int // the execution about to start, counted from 1
|
|
Limit int // the step's repeat limit, or 0 for none
|
|
Command string // the check as written in the repeat policy
|
|
OutputFile string // the output of the failed check
|
|
}
|
|
|
|
// RepeatCheckAware is implemented by executors that can tell their agent why
|
|
// the step repeats. It is called only for an execution that follows a failed
|
|
// check.
|
|
type RepeatCheckAware interface {
|
|
SetRepeatCheck(check RepeatCheck)
|
|
}
|
|
|
|
// SubRunProvider is an interface for executors that spawn sub-DAG runs.
|
|
// This is used by executors like chat (with tools) to report sub-runs
|
|
// for UI drill-down functionality.
|
|
type SubRunProvider interface {
|
|
GetSubRuns() []ir.SubDAGRun
|
|
}
|
|
|
|
// ConnectionCauseProvider is implemented by executors that run sub-DAG runs.
|
|
// It reports the connections that caused the failure of those runs, when
|
|
// each failed run had one, or nil.
|
|
type ConnectionCauseProvider interface {
|
|
ConnectionCause() []string
|
|
}
|
|
|
|
// StatusDetailsProvider reports independently tracked executions within a node.
|
|
type StatusDetailsProvider interface {
|
|
GetStatusDetails() []ir.NodeStatusDetail
|
|
}
|
|
|
|
// ToolDefinitionProvider is an interface for executors that provide tool definitions.
|
|
// This is used by chat executors to report what tools were available to the LLM
|
|
// for debugging and visibility purposes.
|
|
type ToolDefinitionProvider interface {
|
|
GetToolDefinitions() []ir.ToolDefinition
|
|
}
|
|
|
|
// SkillDefinitionProvider is implemented by executors that report the skills
|
|
// that the effective catalog offered to the LLM.
|
|
type SkillDefinitionProvider interface {
|
|
GetSkillDefinitions() []ir.SkillDefinition
|
|
}
|
|
|
|
// UntrustedRunner is implemented by executors that can run an untrusted step
|
|
// (Spec 103). Such an executor starts each process of the step through
|
|
// untrusted.StartManagedProcess, which applies the rules of an untrusted step.
|
|
// The runtime refuses an untrusted step whose executor does not implement it.
|
|
type UntrustedRunner interface {
|
|
RunsUntrusted()
|
|
}
|
|
|
|
// OutputsProvider is implemented by executors that publish DAG/action outputs.
|
|
type OutputsProvider interface {
|
|
GetOutputs() map[string]any
|
|
}
|
|
|
|
// OutputsValueProvider is implemented by executors that publish a raw JSON
|
|
// value through the step outputs channel. Unlike OutputsProvider, the payload
|
|
// is not required to be a JSON object; a parallel executor, for example,
|
|
// publishes an array of per-child output maps.
|
|
type OutputsValueProvider interface {
|
|
GetOutputsValue() any
|
|
}
|
|
|
|
// DeclaredOutputsProvider marks executor outputs as available to strict step output references.
|
|
type DeclaredOutputsProvider interface {
|
|
OutputsProvider
|
|
PublishesDeclaredOutputs() bool
|
|
}
|
|
|
|
// NamedOutputsPublisher is implemented by executors that publish every named
|
|
// output of their step, such as a mocked action. Their outputs replace what
|
|
// the step's output file, output_schema, object-form output, stdout.outputs,
|
|
// and action would publish, and reach the references and run outputs those
|
|
// sources reach. An output declared under Spec 012 publishes as text, and a
|
|
// json one as its JSON text. String-form output still captures stdout.
|
|
type NamedOutputsPublisher interface {
|
|
// NamedOutputs returns the value of each named output, by name, that a
|
|
// successful execution publishes.
|
|
NamedOutputs() map[string]any
|
|
}
|
|
|
|
// SecretMaskerAware is implemented by executors that learn secret values
|
|
// while they run, such as a token that a forced refresh mints. The step's
|
|
// logs and outputs mask each value passed to mask from then on.
|
|
type SecretMaskerAware interface {
|
|
SetSecretMasker(mask func(values ...string))
|
|
}
|