|
| 1 | +defmodule Mix.Tasks.Durable.Cleanup do |
| 2 | + @shortdoc "Deletes old workflow executions" |
| 3 | + |
| 4 | + @moduledoc """ |
| 5 | + Deletes old workflow executions from the database. |
| 6 | +
|
| 7 | + Cascade deletes handle associated step executions, pending inputs, and events. |
| 8 | +
|
| 9 | + ## Usage |
| 10 | +
|
| 11 | + mix durable.cleanup --older-than DURATION [options] |
| 12 | +
|
| 13 | + ## Options |
| 14 | +
|
| 15 | + * `--older-than DURATION` - Required. Delete executions older than this duration. |
| 16 | + Supports: `30d` (days), `24h` (hours), `60m` (minutes) |
| 17 | + * `--status STATUS` - Only delete executions with this status (default: completed, failed). |
| 18 | + Can be specified multiple times. |
| 19 | + * `--dry-run` - Show how many records would be deleted without deleting |
| 20 | + * `--batch-size N` - Number of records to delete per batch (default: 1000) |
| 21 | + * `--name NAME` - The Durable instance name (default: Durable) |
| 22 | +
|
| 23 | + ## Examples |
| 24 | +
|
| 25 | + mix durable.cleanup --older-than 30d |
| 26 | + mix durable.cleanup --older-than 24h --status completed --dry-run |
| 27 | + mix durable.cleanup --older-than 7d --batch-size 500 |
| 28 | + """ |
| 29 | + |
| 30 | + use Mix.Task |
| 31 | + |
| 32 | + import Ecto.Query |
| 33 | + |
| 34 | + alias Durable.Config |
| 35 | + alias Durable.Mix.Helpers |
| 36 | + alias Durable.Repo |
| 37 | + alias Durable.Storage.Schemas.WorkflowExecution |
| 38 | + |
| 39 | + @default_statuses [:completed, :failed] |
| 40 | + @default_batch_size 1000 |
| 41 | + |
| 42 | + @impl Mix.Task |
| 43 | + def run(args) do |
| 44 | + Helpers.ensure_started() |
| 45 | + |
| 46 | + {opts, _, _} = |
| 47 | + OptionParser.parse(args, |
| 48 | + strict: [ |
| 49 | + older_than: :string, |
| 50 | + status: [:string, :keep], |
| 51 | + dry_run: :boolean, |
| 52 | + batch_size: :integer, |
| 53 | + name: :string |
| 54 | + ] |
| 55 | + ) |
| 56 | + |
| 57 | + with {:ok, cutoff} <- parse_older_than(opts), |
| 58 | + {:ok, statuses} <- parse_statuses(opts) do |
| 59 | + durable_name = Helpers.get_durable_name(opts) |
| 60 | + config = Config.get(durable_name) |
| 61 | + dry_run = Keyword.get(opts, :dry_run, false) |
| 62 | + batch_size = Keyword.get(opts, :batch_size, @default_batch_size) |
| 63 | + |
| 64 | + if dry_run do |
| 65 | + run_dry(config, cutoff, statuses) |
| 66 | + else |
| 67 | + run_cleanup(config, cutoff, statuses, batch_size) |
| 68 | + end |
| 69 | + end |
| 70 | + end |
| 71 | + |
| 72 | + defp parse_older_than(opts) do |
| 73 | + case Keyword.get(opts, :older_than) do |
| 74 | + nil -> |
| 75 | + Mix.shell().error("--older-than is required. Example: --older-than 30d") |
| 76 | + :error |
| 77 | + |
| 78 | + duration_str -> |
| 79 | + parse_duration(duration_str) |
| 80 | + end |
| 81 | + end |
| 82 | + |
| 83 | + defp parse_duration(str) do |
| 84 | + case Regex.run(~r/^(\d+)([dhm])$/, str) do |
| 85 | + [_, num_str, unit] -> |
| 86 | + num = String.to_integer(num_str) |
| 87 | + seconds = duration_to_seconds(num, unit) |
| 88 | + cutoff = DateTime.add(DateTime.utc_now(), -seconds, :second) |
| 89 | + {:ok, cutoff} |
| 90 | + |
| 91 | + nil -> |
| 92 | + Mix.shell().error( |
| 93 | + "Invalid duration: #{str}. Use format like 30d (days), 24h (hours), or 60m (minutes)." |
| 94 | + ) |
| 95 | + |
| 96 | + :error |
| 97 | + end |
| 98 | + end |
| 99 | + |
| 100 | + defp duration_to_seconds(num, "d"), do: num * 86_400 |
| 101 | + defp duration_to_seconds(num, "h"), do: num * 3_600 |
| 102 | + defp duration_to_seconds(num, "m"), do: num * 60 |
| 103 | + |
| 104 | + defp parse_statuses(opts) do |
| 105 | + case Keyword.get_values(opts, :status) do |
| 106 | + [] -> |
| 107 | + {:ok, @default_statuses} |
| 108 | + |
| 109 | + status_strings -> |
| 110 | + statuses = |
| 111 | + Enum.map(status_strings, fn s -> |
| 112 | + String.to_existing_atom(s) |
| 113 | + end) |
| 114 | + |
| 115 | + {:ok, statuses} |
| 116 | + end |
| 117 | + rescue |
| 118 | + ArgumentError -> |
| 119 | + Mix.shell().error("Invalid status provided.") |
| 120 | + :error |
| 121 | + end |
| 122 | + |
| 123 | + defp run_dry(config, cutoff, statuses) do |
| 124 | + count = count_matching(config, cutoff, statuses) |
| 125 | + status_str = Enum.map_join(statuses, ", ", &to_string/1) |
| 126 | + |
| 127 | + Mix.shell().info( |
| 128 | + "Dry run: #{Helpers.format_number(count)} executions would be deleted " <> |
| 129 | + "(status: #{status_str}, older than #{Helpers.format_datetime(cutoff)})." |
| 130 | + ) |
| 131 | + end |
| 132 | + |
| 133 | + defp run_cleanup(config, cutoff, statuses, batch_size) do |
| 134 | + total = do_batch_delete(config, cutoff, statuses, batch_size, 0) |
| 135 | + Mix.shell().info("Deleted #{Helpers.format_number(total)} workflow executions.") |
| 136 | + end |
| 137 | + |
| 138 | + defp do_batch_delete(config, cutoff, statuses, batch_size, acc) do |
| 139 | + ids_query = |
| 140 | + from(w in WorkflowExecution, |
| 141 | + where: w.status in ^statuses and w.inserted_at < ^cutoff, |
| 142 | + select: w.id, |
| 143 | + limit: ^batch_size |
| 144 | + ) |
| 145 | + |
| 146 | + delete_query = from(w in WorkflowExecution, where: w.id in subquery(ids_query)) |
| 147 | + {deleted, _} = Repo.delete_all(config, delete_query) |
| 148 | + |
| 149 | + if deleted > 0 do |
| 150 | + do_batch_delete(config, cutoff, statuses, batch_size, acc + deleted) |
| 151 | + else |
| 152 | + acc |
| 153 | + end |
| 154 | + end |
| 155 | + |
| 156 | + defp count_matching(config, cutoff, statuses) do |
| 157 | + query = |
| 158 | + from(w in WorkflowExecution, |
| 159 | + where: w.status in ^statuses and w.inserted_at < ^cutoff, |
| 160 | + select: count(w.id) |
| 161 | + ) |
| 162 | + |
| 163 | + Repo.one(config, query) |
| 164 | + end |
| 165 | +end |
0 commit comments