|
| 1 | +/* |
| 2 | +Copyright 2025 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package workflow |
| 15 | + |
| 16 | +import ( |
| 17 | + "errors" |
| 18 | + |
| 19 | + "github.com/dapr/cli/pkg/workflow" |
| 20 | + "github.com/dapr/kit/signals" |
| 21 | + "github.com/spf13/cobra" |
| 22 | +) |
| 23 | + |
| 24 | +var ( |
| 25 | + flagPurgeOlderThan string |
| 26 | + flagPurgeAll bool |
| 27 | + flagPurgeConn *connFlag |
| 28 | + schedulerNamespace string |
| 29 | +) |
| 30 | + |
| 31 | +var PurgeCmd = &cobra.Command{ |
| 32 | + Use: "purge", |
| 33 | + Short: "Purge one or more workflow instances with a terminal state. Accepts a workflow instance ID argument or flags to purge multiple/all terminal instances. Also deletes all associated scheduler jobs.", |
| 34 | + Args: func(cmd *cobra.Command, args []string) error { |
| 35 | + switch { |
| 36 | + case cmd.Flags().Changed("all-older-than"), |
| 37 | + cmd.Flags().Changed("all"): |
| 38 | + if len(args) > 0 { |
| 39 | + return errors.New("no arguments are accepted when using purge all flags") |
| 40 | + } |
| 41 | + default: |
| 42 | + if len(args) == 0 { |
| 43 | + return errors.New("one or more workflow instance ID arguments are required when not using purge all flags") |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + return nil |
| 48 | + }, |
| 49 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 50 | + ctx := signals.Context() |
| 51 | + |
| 52 | + appID, err := getWorkflowAppID(cmd) |
| 53 | + if err != nil { |
| 54 | + return err |
| 55 | + } |
| 56 | + |
| 57 | + opts := workflow.PurgeOptions{ |
| 58 | + KubernetesMode: flagKubernetesMode, |
| 59 | + Namespace: flagDaprNamespace, |
| 60 | + SchedulerNamespace: schedulerNamespace, |
| 61 | + AppID: appID, |
| 62 | + InstanceIDs: args, |
| 63 | + All: flagPurgeAll, |
| 64 | + ConnectionString: flagPurgeConn.connectionString, |
| 65 | + TableName: flagPurgeConn.tableName, |
| 66 | + } |
| 67 | + |
| 68 | + if cmd.Flags().Changed("all-older-than") { |
| 69 | + opts.AllOlderThan, err = parseWorkflowDurationTimestamp(flagPurgeOlderThan, true) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return workflow.Purge(ctx, opts) |
| 76 | + }, |
| 77 | +} |
| 78 | + |
| 79 | +func init() { |
| 80 | + PurgeCmd.Flags().StringVar(&flagPurgeOlderThan, "all-older-than", "", "Purge workflow instances older than the specified Go duration or timestamp, e.g., '24h' or '2023-01-02T15:04:05Z'.") |
| 81 | + PurgeCmd.Flags().BoolVar(&flagPurgeAll, "all", false, "Purge all workflow instances in a terminal state. Use with caution.") |
| 82 | + PurgeCmd.MarkFlagsMutuallyExclusive("all-older-than", "all") |
| 83 | + |
| 84 | + PurgeCmd.Flags().StringVar(&schedulerNamespace, "scheduler-namespace", "dapr-system", "Kubernetes namespace where the scheduler is deployed, only relevant if --kubernetes is set") |
| 85 | + |
| 86 | + flagPurgeConn = connectionCmd(PurgeCmd) |
| 87 | + |
| 88 | + WorkflowCmd.AddCommand(PurgeCmd) |
| 89 | +} |
0 commit comments