From 0dd2c445d6ee73d7cf9d49df2d2cfda0f21a8df7 Mon Sep 17 00:00:00 2001 From: Saltaferis Dimitrios Date: Tue, 13 Oct 2026 17:33:04 +0200 Subject: [PATCH 1/2] fix(taskmanager): flush flow runs on Infrahub 1.13, which takes no number of days Infrahub 1.13 removes --days-to-keep and --batch-size from `infrahub tasks flush flow-runs`: the task manager deletes up to its own task history retention, and --rewrite returns the disk space (opsmill/infrahub#10869). The tool passed both options, and an unknown option is not the "no such command" that triggers its fallback, so `infrahub-taskmanager flush flow-runs` failed on 1.13. The tool now reads the command's help in the task worker. When it offers --rewrite and no --days-to-keep, the tool runs the command without days, warns when days_to_keep or batch_size were given, and passes the new --rewrite flag. Earlier releases keep the previous behaviour. Co-Authored-By: Claude Opus 5.5 --- ...low-runs-task-manager-retention.changed.md | 1 + src/cmd/infrahub-taskmanager/main.go | 18 ++- src/internal/app/taskmanager.go | 69 ++++++++++- src/internal/app/taskmanager_test.go | 113 ++++++++++++++++++ 4 files changed, 196 insertions(+), 5 deletions(-) create mode 100644 changelog/+flow-runs-task-manager-retention.changed.md create mode 100644 src/internal/app/taskmanager_test.go diff --git a/changelog/+flow-runs-task-manager-retention.changed.md b/changelog/+flow-runs-task-manager-retention.changed.md new file mode 100644 index 00000000..95899017 --- /dev/null +++ b/changelog/+flow-runs-task-manager-retention.changed.md @@ -0,0 +1 @@ +`infrahub-taskmanager flush flow-runs` works with Infrahub 1.13 and later, where `infrahub tasks flush flow-runs` takes no number of days. With those releases it deletes the runs older than the task history retention set on the task manager (`INFRAHUB_TASK_MANAGER_RETENTION_TASK_HISTORY`), warns when `days_to_keep` or `batch_size` is given because they are ignored, and with the new `--rewrite` flag rewrites the tables that are mostly free space to return their disk space. With earlier releases the command behaves as before. diff --git a/src/cmd/infrahub-taskmanager/main.go b/src/cmd/infrahub-taskmanager/main.go index 31115165..1ca60fbe 100644 --- a/src/cmd/infrahub-taskmanager/main.go +++ b/src/cmd/infrahub-taskmanager/main.go @@ -35,9 +35,14 @@ func main() { Long: "Cleanup operations for Prefect resources.", } + var rewrite bool flowRunsCmd := &cobra.Command{ - Use: "flow-runs [days_to_keep] [batch_size]", - Short: "Delete completed/failed/cancelled flow runs older than the retention period", + Use: "flow-runs [days_to_keep] [batch_size]", + Short: "Delete completed/failed/cancelled flow runs older than the retention period", + Long: "Delete completed/failed/cancelled flow runs older than the retention period.\n\n" + + "From Infrahub 1.13, the task manager deletes the runs older than its task history retention " + + "(INFRAHUB_TASK_MANAGER_RETENTION_TASK_HISTORY), so days_to_keep and batch_size are ignored: " + + "set the retention instead. --rewrite then returns the disk space of the deleted runs.", Args: cobra.RangeArgs(0, 2), SilenceUsage: true, RunE: func(cmd *cobra.Command, args []string) error { @@ -56,9 +61,16 @@ func main() { return err } } - return iops.FlushFlowRuns(days, batch) + return iops.FlushFlowRuns(app.FlowRunsRequest{ + DaysToKeep: days, + BatchSize: batch, + ArgumentsGiven: len(args) > 0, + Rewrite: rewrite, + }) }, } + flowRunsCmd.Flags().BoolVar(&rewrite, "rewrite", false, + "After the deletes, rewrite the tables that are mostly free space to return it to the disk (Infrahub 1.13 or later)") staleRunsCmd := &cobra.Command{ Use: "stale-runs [days_to_keep] [batch_size]", diff --git a/src/internal/app/taskmanager.go b/src/internal/app/taskmanager.go index 93b29899..a48ed0c1 100644 --- a/src/internal/app/taskmanager.go +++ b/src/internal/app/taskmanager.go @@ -36,9 +36,74 @@ var ( } ) +// FlowRunsRequest is what the user asked `flush flow-runs` for. +type FlowRunsRequest struct { + DaysToKeep int + BatchSize int + // ArgumentsGiven is true when the user passed days_to_keep or batch_size. + ArgumentsGiven bool + Rewrite bool +} + // FlushFlowRuns removes completed Prefect runs beyond the retention window. -func (iops *InfrahubOps) FlushFlowRuns(daysToKeep, batchSize int) error { - return iops.flushTaskRuns(flowRunsConfig, daysToKeep, batchSize) +// +// From Infrahub 1.13, `infrahub tasks flush flow-runs` takes no number of days: the task manager deletes up to its +// own task history retention, and `--rewrite` returns the disk space. Earlier releases take the number of days. +func (iops *InfrahubOps) FlushFlowRuns(request FlowRunsRequest) error { + if err := iops.checkPrerequisites(); err != nil { + return err + } + if err := iops.DetectEnvironment(); err != nil { + return err + } + + // Wide enough that the Infrahub CLI prints its help and summary lines without wrapping them. + execOpts := iops.buildTaskWorkerExecOpts(&ExecOptions{Env: map[string]string{"COLUMNS": "200"}}) + help, helpErr := iops.Exec("task-worker", []string{"infrahub", "tasks", "flush", "flow-runs", "--help"}, execOpts) + if helpErr != nil || !taskManagerOwnsRetention(help) { + if request.Rewrite { + logrus.Warn("This Infrahub release cannot rewrite the task history tables; --rewrite is ignored") + } + return iops.flushTaskRuns(flowRunsConfig, request.DaysToKeep, request.BatchSize) + } + + if warning := ignoredArgumentsWarning(request); warning != "" { + logrus.Warn(warning) + } + command := flowRunsCommand(request) + logrus.Infof("Running %s in the task worker...", strings.Join(command, " ")) + if _, err := iops.ExecStream("task-worker", command, execOpts); err != nil { + return fmt.Errorf("failed to execute %s: %w", strings.Join(command, " "), err) + } + logrus.Info("Flow runs cleanup completed") + return nil +} + +// taskManagerOwnsRetention reports whether the help of `infrahub tasks flush flow-runs` is the one of Infrahub 1.13 +// and later, which takes no number of days and offers --rewrite. +func taskManagerOwnsRetention(help string) bool { + return strings.Contains(help, "--rewrite") && !strings.Contains(help, "--days-to-keep") +} + +// flowRunsCommand is the `infrahub tasks flush flow-runs` command of Infrahub 1.13 and later for the request. +func flowRunsCommand(request FlowRunsRequest) []string { + command := []string{"infrahub", "tasks", "flush", "flow-runs"} + if request.Rewrite { + command = append(command, "--rewrite") + } + return command +} + +// ignoredArgumentsWarning explains that Infrahub 1.13 and later ignore days_to_keep and batch_size, when they were given. +func ignoredArgumentsWarning(request FlowRunsRequest) string { + if !request.ArgumentsGiven { + return "" + } + return fmt.Sprintf( + "This Infrahub release deletes the flow runs older than the task history retention set on the task manager, "+ + "not the %d days given; to keep %d days, set INFRAHUB_TASK_MANAGER_RETENTION_TASK_HISTORY=%dd on the task manager", + request.DaysToKeep, request.DaysToKeep, request.DaysToKeep, + ) } // FlushStaleRuns cancels running Prefect flow runs that exceeded retention. diff --git a/src/internal/app/taskmanager_test.go b/src/internal/app/taskmanager_test.go new file mode 100644 index 00000000..a8e88966 --- /dev/null +++ b/src/internal/app/taskmanager_test.go @@ -0,0 +1,113 @@ +package app + +import ( + "reflect" + "testing" +) + +// Help texts of `infrahub tasks flush flow-runs`, printed with COLUMNS=100 by the Infrahub 1.11.4 image and by a +// build of the change that ships in Infrahub 1.13. +const ( + flowRunsHelpBefore113 = ` Usage: infrahub tasks flush flow-runs [OPTIONS] [CONFIG_FILE] + + Flush old task runs. + +╭─ Arguments ──────────────────────────────────────────────────────────────────────────────────────╮ +│ config_file [CONFIG_FILE] [env var: INFRAHUB_CONFIG] [default: infrahub.toml] │ +╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ +╭─ Options ────────────────────────────────────────────────────────────────────────────────────────╮ +│ --days-to-keep INTEGER [default: 30] │ +│ --batch-size INTEGER [default: 100] │ +│ --help Show this message and exit. │ +╰──────────────────────────────────────────────────────────────────────────────────────────────────╯` + + flowRunsHelpFrom113 = ` Usage: infrahub tasks flush flow-runs [OPTIONS] [CONFIG_FILE] + + Delete the finished task runs older than the task history retention, with their logs and + artifacts. + + Raises: typer.Exit: With code 1 when the cleanup fails. + +╭─ Arguments ──────────────────────────────────────────────────────────────────────────────────────╮ +│ config_file [CONFIG_FILE] [env var: INFRAHUB_CONFIG] [default: infrahub.toml] │ +╰──────────────────────────────────────────────────────────────────────────────────────────────────╯ +╭─ Options ────────────────────────────────────────────────────────────────────────────────────────╮ +│ --rewrite After the deletes, rewrite each task history and activity log table whose │ +│ live rows hold less than half of its disk space, to return that space to the │ +│ disk. Tasks that write to a table wait while it is rewritten. │ +│ --help Show this message and exit. │ +╰──────────────────────────────────────────────────────────────────────────────────────────────────╯` +) + +func TestTaskManagerOwnsRetention(t *testing.T) { + cases := []struct { + name string + help string + want bool + }{ + {name: "before 1.13", help: flowRunsHelpBefore113, want: false}, + {name: "from 1.13", help: flowRunsHelpFrom113, want: true}, + {name: "no help", help: "", want: false}, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := taskManagerOwnsRetention(tc.help); got != tc.want { + t.Fatalf("taskManagerOwnsRetention() = %v, want %v", got, tc.want) + } + }) + } +} + +func TestFlowRunsCommand(t *testing.T) { + cases := []struct { + name string + request FlowRunsRequest + want []string + }{ + { + name: "without rewrite", + request: FlowRunsRequest{DaysToKeep: 7, BatchSize: 200, ArgumentsGiven: true}, + want: []string{"infrahub", "tasks", "flush", "flow-runs"}, + }, + { + name: "with rewrite", + request: FlowRunsRequest{DaysToKeep: 30, BatchSize: 200, Rewrite: true}, + want: []string{"infrahub", "tasks", "flush", "flow-runs", "--rewrite"}, + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := flowRunsCommand(tc.request); !reflect.DeepEqual(got, tc.want) { + t.Fatalf("flowRunsCommand() = %v, want %v", got, tc.want) + } + }) + } +} + +func TestIgnoredArgumentsWarning(t *testing.T) { + cases := []struct { + name string + request FlowRunsRequest + want string + }{ + { + name: "days given", + request: FlowRunsRequest{DaysToKeep: 7, BatchSize: 200, ArgumentsGiven: true}, + want: "This Infrahub release deletes the flow runs older than the task history retention set on the task " + + "manager, not the 7 days given; to keep 7 days, set INFRAHUB_TASK_MANAGER_RETENTION_TASK_HISTORY=7d " + + "on the task manager", + }, + { + name: "nothing given", + request: FlowRunsRequest{DaysToKeep: 30, BatchSize: 200}, + want: "", + }, + } + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + if got := ignoredArgumentsWarning(tc.request); got != tc.want { + t.Fatalf("ignoredArgumentsWarning() = %q, want %q", got, tc.want) + } + }) + } +} From fee078014dde2e735a9d46f164d3f21439e18b2d Mon Sep 17 00:00:00 2001 From: Dimitrios Saltaferis Date: Thu, 29 Oct 2026 18:22:56 +0000 Subject: [PATCH 2/2] fix(taskmanager): normalize colored help before detecting retention --- src/internal/app/collect_extras.go | 3 +-- src/internal/app/taskmanager.go | 1 + src/internal/app/taskmanager_test.go | 10 ++++++++++ 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/internal/app/collect_extras.go b/src/internal/app/collect_extras.go index d0ca203f..a7c32fdf 100644 --- a/src/internal/app/collect_extras.go +++ b/src/internal/app/collect_extras.go @@ -235,8 +235,7 @@ func commandErrorLine(output string) string { // ported from the Python collect_benchmark parser. var benchmarkResultRe = regexp.MustCompile(`(\w+(?: \w+)*): (\d+)(?: MB)? - Required: (\d+)(?: MB)? : (\w+)`) -// ansiEscapeRe matches the ANSI SGR color sequences the benchmark entrypoint -// wraps its result lines in. +// ansiEscapeRe matches ANSI SGR color sequences in command output. var ansiEscapeRe = regexp.MustCompile(`\x1b\[[0-9;]*m`) // benchmarkMeasure is one parsed benchmark result; the field names mirror the diff --git a/src/internal/app/taskmanager.go b/src/internal/app/taskmanager.go index a48ed0c1..3962fad1 100644 --- a/src/internal/app/taskmanager.go +++ b/src/internal/app/taskmanager.go @@ -82,6 +82,7 @@ func (iops *InfrahubOps) FlushFlowRuns(request FlowRunsRequest) error { // taskManagerOwnsRetention reports whether the help of `infrahub tasks flush flow-runs` is the one of Infrahub 1.13 // and later, which takes no number of days and offers --rewrite. func taskManagerOwnsRetention(help string) bool { + help = ansiEscapeRe.ReplaceAllString(help, "") return strings.Contains(help, "--rewrite") && !strings.Contains(help, "--days-to-keep") } diff --git a/src/internal/app/taskmanager_test.go b/src/internal/app/taskmanager_test.go index a8e88966..9ab0d5f0 100644 --- a/src/internal/app/taskmanager_test.go +++ b/src/internal/app/taskmanager_test.go @@ -47,6 +47,16 @@ func TestTaskManagerOwnsRetention(t *testing.T) { }{ {name: "before 1.13", help: flowRunsHelpBefore113, want: false}, {name: "from 1.13", help: flowRunsHelpFrom113, want: true}, + { + name: "colored help from 1.13", + help: "│ \x1b[1;36m-\x1b[0m\x1b[1;36m-rewrite\x1b[0m Return unused disk space. │", + want: true, + }, + { + name: "colored legacy option still owns retention", + help: "--rewrite \x1b[1;36m-\x1b[0m\x1b[1;36m-days-to-keep\x1b[0m INTEGER", + want: false, + }, {name: "no help", help: "", want: false}, } for _, tc := range cases {