mirror of
https://github.com/dagucloud/dagu.git
synced 2026-12-05 21:34:48 +00:00
211 lines
7.4 KiB
Go
211 lines
7.4 KiB
Go
// Copyright (C) 2026 Yota Hamada
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/dagucloud/dagu/v2/internal/backfill"
|
|
"github.com/dagucloud/dagu/v2/internal/dagrun"
|
|
"github.com/dagucloud/dagu/v2/internal/ir"
|
|
"github.com/dagucloud/dagu/v2/internal/output"
|
|
"github.com/dagucloud/dagu/v2/internal/persis"
|
|
"github.com/spf13/cobra"
|
|
"golang.org/x/term"
|
|
)
|
|
|
|
func Status() *cobra.Command {
|
|
return NewCommand(
|
|
&cobra.Command{
|
|
Use: "status [flags] <DAG name>",
|
|
Short: "Display the current status of a DAG-run",
|
|
Long: `Show real-time status information for a specified DAG-run instance.
|
|
|
|
This command retrieves and displays the current execution status of a DAG-run,
|
|
including its state (running, completed, failed), process ID, and other relevant details.
|
|
|
|
Flags:
|
|
--run-id string (optional) Unique identifier of the DAG-run to check.
|
|
If not provided, it will show the status of the
|
|
most recent DAG-run for the given name.
|
|
--sub-run-id string (optional) Unique identifier of a sub DAG-run.
|
|
Requires --run-id to be provided.
|
|
Use this to check the status of nested DAG executions.
|
|
|
|
Example:
|
|
dagu status --run-id=abc123 my_dag
|
|
dagu status my_dag # Shows status of the most recent DAG-run
|
|
dagu status --run-id=abc123 --sub-run-id=def456 my_dag # Shows status of a sub DAG-run
|
|
`,
|
|
Args: cobra.ExactArgs(1),
|
|
}, statusFlags, runStatus,
|
|
)
|
|
}
|
|
|
|
var statusFlags = []commandLineFlag{
|
|
dagRunIDFlagStatus,
|
|
subDAGRunIDFlagStatus,
|
|
recursiveOutputFlag,
|
|
}
|
|
|
|
func runStatus(ctx *Context, args []string) error {
|
|
recursive, err := ctx.Command.Flags().GetBool(recursiveOutputFlag.name)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read %s: %w", recursiveOutputFlag.name, err)
|
|
}
|
|
if ctx.IsRemote() {
|
|
return remoteRunStatus(ctx, args, recursive)
|
|
}
|
|
dagRunID, err := ctx.StringParam("run-id")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get dag-run ID: %w", err)
|
|
}
|
|
|
|
subDAGRunID, err := ctx.StringParam("sub-run-id")
|
|
if err != nil {
|
|
return fmt.Errorf("failed to get sub-dag-run ID: %w", err)
|
|
}
|
|
|
|
if subDAGRunID != "" && dagRunID == "" {
|
|
return fmt.Errorf("--sub-run-id requires --run-id to be provided (root DAG run context is needed)")
|
|
}
|
|
|
|
name, err := extractDAGName(ctx, args[0])
|
|
if err != nil {
|
|
return fmt.Errorf("failed to extract DAG name: %w", err)
|
|
}
|
|
|
|
attempt, err := extractAttemptForStatus(ctx, name, dagRunID, subDAGRunID)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to extract attempt: %w", err)
|
|
}
|
|
|
|
dag, err := attempt.ReadDAG(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read DAG from run data: %w", err)
|
|
}
|
|
|
|
dagStatus, err := attempt.ReadStatus(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to read status from attempt: %w", err)
|
|
}
|
|
|
|
// For running root DAGs, fetch real-time status via socket connection.
|
|
// Sub DAGs use stored status since they may run on different workers.
|
|
if dagStatus.Status == ir.Running && subDAGRunID == "" {
|
|
// Use dagStatus.DAGRunID as fallback when --run-id flag is omitted
|
|
runIDForLookup := dagRunID
|
|
if runIDForLookup == "" {
|
|
runIDForLookup = dagStatus.DAGRunID
|
|
}
|
|
realtimeStatus, err := ctx.DAGRunMgr.GetCurrentStatus(ctx, dag, runIDForLookup)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to retrieve current status: %w", err)
|
|
}
|
|
if realtimeStatus.DAGRunID == dagStatus.DAGRunID {
|
|
dagStatus = realtimeStatus
|
|
}
|
|
}
|
|
|
|
var subRuns output.SubRunResolver
|
|
if recursive {
|
|
subRuns = subRunStatusResolver(ctx, dagStatus, name, dagRunID)
|
|
}
|
|
displayTreeStatus(dag, dagStatus, subRuns)
|
|
if dagRunID == "" {
|
|
return showLatestBackfill(ctx, dag)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// showLatestBackfill prints the DAG's backfill an operator should see, as
|
|
// backfill.Latest picks it.
|
|
func showLatestBackfill(ctx *Context, dag *ir.DAG) error {
|
|
if ctx.Persistence.BackfillStore == nil {
|
|
return nil
|
|
}
|
|
sched := backfill.SlotSchedule{Schedules: boundStartSchedules(ctx, dag), Location: ctx.Config.Core.Location}
|
|
report, err := backfill.Latest(ctx, ctx.Persistence.BackfillStore, latestRunStatus(ctx.Persistence.DAGRunRepository), ctx.Persistence.DAGRunRepository, dag, sched)
|
|
if err != nil || report == nil {
|
|
return err
|
|
}
|
|
_, err = fmt.Fprintf(ctx.Command.OutOrStdout(), "\nLatest backfill %s: %s; %s (dagu backfill status %s)\n",
|
|
report.Record.ID, backfillStatusLabel(ctx, report.Record), backfillSlotCounts(*report), report.Record.ID)
|
|
return err
|
|
}
|
|
|
|
// displayTreeStatus prints the status tree of a run. A non-nil subRuns
|
|
// resolver expands the run's sub-runs inline.
|
|
func displayTreeStatus(dag *ir.DAG, dagStatus *ir.DAGRunStatus, subRuns output.SubRunResolver) {
|
|
config := output.DefaultConfig()
|
|
config.ColorEnabled = term.IsTerminal(int(os.Stdout.Fd()))
|
|
config.SubRunResolver = subRuns
|
|
|
|
renderer := output.NewRenderer(config)
|
|
fmt.Print(renderer.RenderDAGStatus(dag, dagStatus))
|
|
}
|
|
|
|
// subRunStatusResolver resolves sub-run status under the displayed run's root,
|
|
// locally via the run store or remotely via the server's sub-run endpoint.
|
|
func subRunStatusResolver(ctx *Context, dagStatus *ir.DAGRunStatus, dagName, dagRunID string) output.SubRunResolver {
|
|
if ctx.IsRemote() {
|
|
// The sub-run endpoint treats a literal "latest" or empty run ID as a
|
|
// root ID, so resolve it to the displayed run's recorded ID first.
|
|
rootName, rootID := dagName, dagRunID
|
|
if rootID == "" || rootID == "latest" {
|
|
rootName, rootID = dagStatus.Name, dagStatus.DAGRunID
|
|
}
|
|
return withSubRunLookupBudget(subRunStatusLookupBudget, func(sub ir.SubDAGRun) (*ir.DAGRunStatus, error) {
|
|
lookupCtx, cancel := context.WithTimeout(ctx, subRunStatusLookupTimeout)
|
|
defer cancel()
|
|
detail, err := ctx.Remote.getSubDAGRunDetails(lookupCtx, rootName, rootID, sub.DAGRunID)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return toExecStatus(detail)
|
|
})
|
|
}
|
|
// The displayed run may be the latest; its recorded ID is the root under
|
|
// which all descendants are stored.
|
|
root := ir.NewDAGRunRef(dagName, dagRunID)
|
|
if dagRunID == "" || dagRunID == "latest" {
|
|
root = ir.NewDAGRunRef(dagStatus.Name, dagStatus.DAGRunID)
|
|
}
|
|
return func(sub ir.SubDAGRun) (*ir.DAGRunStatus, error) {
|
|
return ctx.DAGRunMgr.FindSubDAGRunStatus(ctx, root, sub.DAGRunID)
|
|
}
|
|
}
|
|
|
|
// extractAttemptForStatus returns the appropriate Attempt based on the provided IDs.
|
|
// For sub DAG runs, it finds the nested attempt under the root run.
|
|
// For root runs, it finds either the specified run or the latest run.
|
|
func extractAttemptForStatus(ctx *Context, name, dagRunID, subDAGRunID string) (dagrun.Attempt, error) {
|
|
if subDAGRunID != "" {
|
|
dagRunRef := ir.NewDAGRunRef(name, dagRunID)
|
|
attempt, err := ctx.Persistence.DAGRunRepository.FindSubAttempt(ctx, dagRunRef, subDAGRunID)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to find sub dag-run with ID %s under root %s: %w",
|
|
subDAGRunID, dagRunID, err)
|
|
}
|
|
return attempt, nil
|
|
}
|
|
|
|
if dagRunID != "" {
|
|
dagRunRef := ir.NewDAGRunRef(name, dagRunID)
|
|
attempt, err := ctx.Persistence.DAGRunRepository.FindAttempt(ctx, dagRunRef)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to find run data for dag-run ID %s: %w", dagRunID, err)
|
|
}
|
|
return attempt, nil
|
|
}
|
|
|
|
attempt, err := ctx.Persistence.DAGRunRepository.LatestAttempt(ctx, name, persis.DAGRunLatestAttemptOptions{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("failed to find the latest run data for DAG %s: %w", name, err)
|
|
}
|
|
return attempt, nil
|
|
}
|