neiam
/vigil
public · Issues · Pulls · Labels · Forks · Compare · Actions success · Badges · Packages
⭐
Log in to mark this repository.
Retention: per-org limits, daily sweep of stale issues/releases/bundles, purge of deleted projects and orgs
76dee78 · gmorell · 2026-12-02 03:42
Files changed
modified
config/config.exs
+3
−1
@@ -57,7 +57,9 @@ config :vigil, Oban,
| 57 | 57 | # Uptime checks whose self-rescheduling chain stopped (Vigil.Uptime). |
| 58 | 58 | {"* * * * *", Vigil.Uptime.ReconcileWorker}, |
| 59 | 59 | # Chunk blobs and assemble statuses past a day (Vigil.Artifacts). |
| 60 | − {"41 3 * * *", Vigil.Artifacts.CleanupWorker} | |
| 60 | + {"41 3 * * *", Vigil.Artifacts.CleanupWorker}, | |
| 61 | + # Retention: per-org limits, stale issues and releases, purges. | |
| 62 | + {"53 3 * * *", Vigil.Retention.SweepWorker} | |
| 61 | 63 | ]} |
| 62 | 64 | ] |
| 63 | 65 |
modified
lib/vigil/organizations/organization.ex
+27
−1
@@ -15,6 +15,11 @@ defmodule Vigil.Organizations.Organization do
| 15 | 15 | field :group_removal, Ecto.Enum, values: [:remove, :demote, :keep], default: :remove |
| 16 | 16 | field :group_rules_promote_manual, :boolean, default: false |
| 17 | 17 | field :deleted_at, :utc_datetime |
| 18 | + # Shorter than the instance's retention, or nil for its default | |
| 19 | + # (Vigil.Retention). | |
| 20 | + field :event_retention_days, :integer | |
| 21 | + field :log_retention_days, :integer | |
| 22 | + field :release_retention_days, :integer | |
| 18 | 23 | |
| 19 | 24 | has_many :members, Vigil.Organizations.Member |
| 20 | 25 | has_many :teams, Vigil.Organizations.Team |
@@ -36,10 +41,31 @@ defmodule Vigil.Organizations.Organization do
| 36 | 41 | @doc "The settings page." |
| 37 | 42 | def settings_changeset(org, attrs) do |
| 38 | 43 | org |
| 39 | − |> cast(attrs, [:name, :slug, :accepting_events, :scrub_ip, :open_membership]) | |
| 44 | + |> cast(attrs, [ | |
| 45 | + :name, | |
| 46 | + :slug, | |
| 47 | + :accepting_events, | |
| 48 | + :scrub_ip, | |
| 49 | + :open_membership, | |
| 50 | + :event_retention_days, | |
| 51 | + :log_retention_days, | |
| 52 | + :release_retention_days | |
| 53 | + ]) | |
| 40 | 54 | |> validate_required([:name]) |
| 55 | + |> validate_retention(:event_retention_days, :events) | |
| 56 | + |> validate_retention(:log_retention_days, :logs) | |
| 57 | + |> validate_retention(:release_retention_days, :releases) | |
| 41 | 58 | |> validate_length(:name, max: 64) |
| 42 | 59 | |> Vigil.Slug.changeset(:name, reserved: true) |
| 43 | 60 | |> unique_constraint(:slug, message: "is taken") |
| 44 | 61 | end |
| 62 | + | |
| 63 | + # At least a day, at most what the instance keeps. | |
| 64 | + defp validate_retention(changeset, field, kind) do | |
| 65 | + validate_number(changeset, field, | |
| 66 | + greater_than_or_equal_to: 1, | |
| 67 | + less_than_or_equal_to: Vigil.Retention.default_days(kind), | |
| 68 | + message: "must be between 1 and #{Vigil.Retention.default_days(kind)} days" | |
| 69 | + ) | |
| 70 | + end | |
| 45 | 71 | end |
added
lib/vigil/retention.ex
+281
−0
@@ -0,0 +1,281 @@
| 1 | +defmodule Vigil.Retention do | |
| 2 | + @moduledoc """ | |
| 3 | + How long data is kept, and the daily sweep that enforces it | |
| 4 | + (`Vigil.Retention.SweepWorker`). | |
| 5 | + | |
| 6 | + The instance's limits are the partitioned tables' `keep_days` | |
| 7 | + (`config :vigil, Vigil.Partitions`): whole partitions past them are | |
| 8 | + dropped by `Vigil.Partitions.maintain/1`. Releases are kept | |
| 9 | + `config :vigil, Vigil.Retention, release_days:` (365). An organization | |
| 10 | + may keep less (`event_retention_days`, `log_retention_days`, | |
| 11 | + `release_retention_days`); `sweep/1` deletes its rows past that. | |
| 12 | + | |
| 13 | + The sweep also removes what's left behind: | |
| 14 | + | |
| 15 | + * issues whose last event is past the event retention, and issues | |
| 16 | + deleted (soft) more than a day ago; | |
| 17 | + * releases created before the release retention that no issue was | |
| 18 | + seen in since, with their artifact bundles; | |
| 19 | + * artifact bundles linked to no project any more, with their stored | |
| 20 | + files; | |
| 21 | + * projects and organizations deleted (soft) more than a day ago: | |
| 22 | + each is purged by its own `Vigil.Retention.PurgeWorker` job. | |
| 23 | + | |
| 24 | + Chunk blobs are cleaned by `Vigil.Artifacts.CleanupWorker`. | |
| 25 | + """ | |
| 26 | + | |
| 27 | + import Ecto.Query | |
| 28 | + | |
| 29 | + alias Vigil.Artifacts.{ArtifactBundle, ArtifactFile} | |
| 30 | + alias Vigil.Issues.Issue | |
| 31 | + alias Vigil.Organizations.Organization | |
| 32 | + alias Vigil.Projects.Project | |
| 33 | + alias Vigil.Releases.Release | |
| 34 | + alias Vigil.{Partitions, Repo, Storage} | |
| 35 | + | |
| 36 | + require Logger | |
| 37 | + | |
| 38 | + @grace_days 1 | |
| 39 | + | |
| 40 | + @doc "The instance's retention in days: `:events`, `:logs` or `:releases`." | |
| 41 | + def default_days(:events), do: keep_days("issue_events", 90) | |
| 42 | + def default_days(:logs), do: keep_days("log_events", 30) | |
| 43 | + | |
| 44 | + def default_days(:releases), | |
| 45 | + do: Application.get_env(:vigil, __MODULE__, []) |> Keyword.get(:release_days, 365) | |
| 46 | + | |
| 47 | + defp keep_days(table, default) do | |
| 48 | + Enum.find_value(Partitions.tables(), default, &(&1.table == table && &1.keep_days)) | |
| 49 | + end | |
| 50 | + | |
| 51 | + @doc "An organization's retention in days for `kind`, its own or the instance's." | |
| 52 | + def days(%Organization{} = org, kind) do | |
| 53 | + own = | |
| 54 | + case kind do | |
| 55 | + :events -> org.event_retention_days | |
| 56 | + :logs -> org.log_retention_days | |
| 57 | + :releases -> org.release_retention_days | |
| 58 | + end | |
| 59 | + | |
| 60 | + min(own || default_days(kind), default_days(kind)) | |
| 61 | + end | |
| 62 | + | |
| 63 | + @doc """ | |
| 64 | + The daily sweep (see the moduledoc), as of `now`. Returns what it did, | |
| 65 | + counts by kind. | |
| 66 | + """ | |
| 67 | + def sweep(now \\ DateTime.utc_now()) do | |
| 68 | + orgs = Repo.all(from o in Organization, where: is_nil(o.deleted_at)) | |
| 69 | + | |
| 70 | + org_counts = | |
| 71 | + Enum.reduce(orgs, %{events: 0, logs: 0, issues: 0, releases: 0}, fn org, acc -> | |
| 72 | + acc | |
| 73 | + |> add(:events, shorter_events(org, now)) | |
| 74 | + |> add(:logs, shorter_logs(org, now)) | |
| 75 | + |> add(:issues, stale_issues(org, now)) | |
| 76 | + |> add(:releases, stale_releases(org, now)) | |
| 77 | + end) | |
| 78 | + | |
| 79 | + org_counts | |
| 80 | + |> Map.put(:deleted_issues, purge_deleted_issues(now)) | |
| 81 | + |> Map.put(:bundles, orphan_bundles()) | |
| 82 | + |> Map.put(:purges_queued, queue_purges(now)) | |
| 83 | + end | |
| 84 | + | |
| 85 | + defp add(acc, k, n), do: Map.update!(acc, k, &(&1 + n)) | |
| 86 | + | |
| 87 | + defp cutoff(now, days), do: DateTime.add(now, -days * 86_400) | |
| 88 | + | |
| 89 | + # Rows of an organization that keeps less than the instance. (Past the | |
| 90 | + # instance's limit the partitions themselves are dropped.) | |
| 91 | + defp shorter_events(org, now) do | |
| 92 | + if days(org, :events) < default_days(:events) do | |
| 93 | + floor = Partitions.uuid7_floor(cutoff(now, days(org, :events))) | |
| 94 | + | |
| 95 | + {n, _} = | |
| 96 | + Repo.delete_all( | |
| 97 | + from e in "issue_events", | |
| 98 | + where: | |
| 99 | + e.organization_id == type(^org.id, :binary_id) and e.id < type(^floor, :binary_id) | |
| 100 | + ) | |
| 101 | + | |
| 102 | + n | |
| 103 | + else | |
| 104 | + 0 | |
| 105 | + end | |
| 106 | + end | |
| 107 | + | |
| 108 | + defp shorter_logs(org, now) do | |
| 109 | + if days(org, :logs) < default_days(:logs) do | |
| 110 | + floor = Partitions.uuid7_floor(cutoff(now, days(org, :logs))) | |
| 111 | + | |
| 112 | + {n, _} = | |
| 113 | + Repo.delete_all( | |
| 114 | + from e in "log_events", | |
| 115 | + where: | |
| 116 | + e.organization_id == type(^org.id, :binary_id) and e.id < type(^floor, :binary_id) | |
| 117 | + ) | |
| 118 | + | |
| 119 | + n | |
| 120 | + else | |
| 121 | + 0 | |
| 122 | + end | |
| 123 | + end | |
| 124 | + | |
| 125 | + # Issues last seen before the event retention have no events left. | |
| 126 | + defp stale_issues(org, now) do | |
| 127 | + {n, _} = | |
| 128 | + Repo.delete_all( | |
| 129 | + from i in Issue, | |
| 130 | + where: i.organization_id == ^org.id and i.last_seen < ^cutoff(now, days(org, :events)) | |
| 131 | + ) | |
| 132 | + | |
| 133 | + n | |
| 134 | + end | |
| 135 | + | |
| 136 | + # Old releases nobody has seen an issue in since the cutoff. | |
| 137 | + defp stale_releases(org, now) do | |
| 138 | + cut = cutoff(now, days(org, :releases)) | |
| 139 | + | |
| 140 | + stale = | |
| 141 | + Repo.all( | |
| 142 | + from r in Release, | |
| 143 | + as: :r, | |
| 144 | + where: | |
| 145 | + r.organization_id == ^org.id and r.inserted_at < ^cut and | |
| 146 | + not exists( | |
| 147 | + from i in Issue, | |
| 148 | + where: i.last_release_id == parent_as(:r).id and i.last_seen >= ^cut, | |
| 149 | + select: 1 | |
| 150 | + ), | |
| 151 | + select: {r.id, r.version} | |
| 152 | + ) | |
| 153 | + | |
| 154 | + Enum.each(stale, fn {id, version} -> | |
| 155 | + bundles = | |
| 156 | + Repo.all( | |
| 157 | + from b in ArtifactBundle, | |
| 158 | + where: b.organization_id == ^org.id and b.release == ^version, | |
| 159 | + select: b.id | |
| 160 | + ) | |
| 161 | + | |
| 162 | + delete_bundles(bundles) | |
| 163 | + Repo.delete_all(from r in Release, where: r.id == ^id) | |
| 164 | + end) | |
| 165 | + | |
| 166 | + length(stale) | |
| 167 | + end | |
| 168 | + | |
| 169 | + defp purge_deleted_issues(now) do | |
| 170 | + {n, _} = | |
| 171 | + Repo.delete_all( | |
| 172 | + from i in Issue, | |
| 173 | + where: not is_nil(i.deleted_at) and i.deleted_at < ^cutoff(now, @grace_days) | |
| 174 | + ) | |
| 175 | + | |
| 176 | + n | |
| 177 | + end | |
| 178 | + | |
| 179 | + # Bundles whose projects are all gone. | |
| 180 | + defp orphan_bundles do | |
| 181 | + ids = | |
| 182 | + Repo.all( | |
| 183 | + from b in ArtifactBundle, | |
| 184 | + as: :b, | |
| 185 | + where: | |
| 186 | + not exists( | |
| 187 | + from bp in "artifact_bundle_projects", | |
| 188 | + where: bp.artifact_bundle_id == parent_as(:b).id, | |
| 189 | + select: 1 | |
| 190 | + ), | |
| 191 | + select: b.id | |
| 192 | + ) | |
| 193 | + | |
| 194 | + delete_bundles(ids) | |
| 195 | + length(ids) | |
| 196 | + end | |
| 197 | + | |
| 198 | + @doc false | |
| 199 | + def delete_bundles([]), do: :ok | |
| 200 | + | |
| 201 | + def delete_bundles(ids) do | |
| 202 | + ids | |
| 203 | + |> Enum.chunk_every(100) | |
| 204 | + |> Enum.each(fn chunk -> | |
| 205 | + Repo.all( | |
| 206 | + from f in ArtifactFile, where: f.artifact_bundle_id in ^chunk, select: f.storage_key | |
| 207 | + ) | |
| 208 | + |> Enum.each(&Storage.delete/1) | |
| 209 | + | |
| 210 | + Repo.delete_all(from b in ArtifactBundle, where: b.id in ^chunk) | |
| 211 | + end) | |
| 212 | + end | |
| 213 | + | |
| 214 | + defp queue_purges(now) do | |
| 215 | + cut = cutoff(now, @grace_days) | |
| 216 | + | |
| 217 | + projects = | |
| 218 | + Repo.all( | |
| 219 | + from p in Project, where: not is_nil(p.deleted_at) and p.deleted_at < ^cut, select: p.id | |
| 220 | + ) | |
| 221 | + | |
| 222 | + orgs = | |
| 223 | + Repo.all( | |
| 224 | + from o in Organization, | |
| 225 | + where: not is_nil(o.deleted_at) and o.deleted_at < ^cut, | |
| 226 | + select: o.id | |
| 227 | + ) | |
| 228 | + | |
| 229 | + jobs = | |
| 230 | + Enum.map(projects, &Vigil.Retention.PurgeWorker.new(%{project_id: &1})) ++ | |
| 231 | + Enum.map(orgs, &Vigil.Retention.PurgeWorker.new(%{organization_id: &1})) | |
| 232 | + | |
| 233 | + Oban.insert_all(jobs) | |
| 234 | + length(jobs) | |
| 235 | + end | |
| 236 | + | |
| 237 | + @doc """ | |
| 238 | + Removes a soft-deleted project for good: its artifact bundles' files | |
| 239 | + from storage, then the row (everything else cascades). No-op if it was | |
| 240 | + restored meanwhile. | |
| 241 | + """ | |
| 242 | + def purge_project(project_id) do | |
| 243 | + case Repo.get(Project, project_id) do | |
| 244 | + %Project{deleted_at: %DateTime{}} = project -> | |
| 245 | + Repo.delete!(project, timeout: :infinity) | |
| 246 | + # Its bundles lost their link; those shared with another project | |
| 247 | + # keep that one and survive. | |
| 248 | + orphan_bundles() | |
| 249 | + Logger.info("purged project #{project.id}") | |
| 250 | + :ok | |
| 251 | + | |
| 252 | + _ -> | |
| 253 | + :ok | |
| 254 | + end | |
| 255 | + end | |
| 256 | + | |
| 257 | + @doc "Removes a soft-deleted organization for good, its projects first." | |
| 258 | + def purge_organization(org_id) do | |
| 259 | + case Repo.get(Organization, org_id) do | |
| 260 | + %Organization{deleted_at: %DateTime{}} = org -> | |
| 261 | + Repo.all(from p in Project, where: p.organization_id == ^org.id, select: p.id) | |
| 262 | + |> Enum.each(fn id -> | |
| 263 | + Repo.update_all(from(p in Project, where: p.id == ^id and is_nil(p.deleted_at)), | |
| 264 | + set: [deleted_at: DateTime.utc_now(:second)] | |
| 265 | + ) | |
| 266 | + | |
| 267 | + purge_project(id) | |
| 268 | + end) | |
| 269 | + | |
| 270 | + Repo.all(from b in ArtifactBundle, where: b.organization_id == ^org.id, select: b.id) | |
| 271 | + |> delete_bundles() | |
| 272 | + | |
| 273 | + Repo.delete!(org, timeout: :infinity) | |
| 274 | + Logger.info("purged organization #{org.id}") | |
| 275 | + :ok | |
| 276 | + | |
| 277 | + _ -> | |
| 278 | + :ok | |
| 279 | + end | |
| 280 | + end | |
| 281 | +end |
added
lib/vigil/retention/purge_worker.ex
+20
−0
@@ -0,0 +1,20 @@
| 1 | +defmodule Vigil.Retention.PurgeWorker do | |
| 2 | + @moduledoc """ | |
| 3 | + Removes one soft-deleted project or organization for good | |
| 4 | + (`Vigil.Retention.purge_project/1`, `purge_organization/1`), queued by | |
| 5 | + the daily sweep a day after the deletion. | |
| 6 | + """ | |
| 7 | + use Oban.Worker, | |
| 8 | + queue: :maintenance, | |
| 9 | + max_attempts: 3, | |
| 10 | + unique: [period: :infinity, states: [:available, :scheduled, :executing, :retryable]] | |
| 11 | + | |
| 12 | + @impl Oban.Worker | |
| 13 | + def perform(%Oban.Job{args: %{"project_id" => id}}), do: Vigil.Retention.purge_project(id) | |
| 14 | + | |
| 15 | + def perform(%Oban.Job{args: %{"organization_id" => id}}), | |
| 16 | + do: Vigil.Retention.purge_organization(id) | |
| 17 | + | |
| 18 | + @impl Oban.Worker | |
| 19 | + def timeout(_job), do: :timer.hours(2) | |
| 20 | +end |
added
lib/vigil/retention/sweep_worker.ex
+16
−0
@@ -0,0 +1,16 @@
| 1 | +defmodule Vigil.Retention.SweepWorker do | |
| 2 | + @moduledoc "Daily: `Vigil.Retention.sweep/1`." | |
| 3 | + use Oban.Worker, queue: :maintenance, max_attempts: 3 | |
| 4 | + | |
| 5 | + require Logger | |
| 6 | + | |
| 7 | + @impl Oban.Worker | |
| 8 | + def perform(_job) do | |
| 9 | + counts = Vigil.Retention.sweep() | |
| 10 | + Logger.info("retention sweep: #{inspect(counts)}") | |
| 11 | + :ok | |
| 12 | + end | |
| 13 | + | |
| 14 | + @impl Oban.Worker | |
| 15 | + def timeout(_job), do: :timer.hours(2) | |
| 16 | +end |
modified
lib/vigil_web/live/org_settings_live/general.ex
+32
−0
@@ -45,6 +45,38 @@ defmodule VigilWeb.OrgSettingsLive.General do
| 45 | 45 | type="checkbox" |
| 46 | 46 | label="Open membership: every member sees every project" |
| 47 | 47 | /> |
| 48 | + <div id="org-retention" class="space-y-1 pt-2"> | |
| 49 | + <h3 class="text-sm font-semibold">Retention</h3> | |
| 50 | + <p class="text-xs text-base-content/60"> | |
| 51 | + Keep data for fewer days than this instance does. Empty: the instance's | |
| 52 | + limit (events {Vigil.Retention.default_days(:events)}, logs {Vigil.Retention.default_days( | |
| 53 | + :logs | |
| 54 | + )}, releases {Vigil.Retention.default_days(:releases)} days). | |
| 55 | + </p> | |
| 56 | + <div class="grid gap-x-3 sm:grid-cols-3"> | |
| 57 | + <.input | |
| 58 | + field={@form[:event_retention_days]} | |
| 59 | + type="number" | |
| 60 | + label="Events (days)" | |
| 61 | + min="1" | |
| 62 | + max={Vigil.Retention.default_days(:events)} | |
| 63 | + /> | |
| 64 | + <.input | |
| 65 | + field={@form[:log_retention_days]} | |
| 66 | + type="number" | |
| 67 | + label="Logs (days)" | |
| 68 | + min="1" | |
| 69 | + max={Vigil.Retention.default_days(:logs)} | |
| 70 | + /> | |
| 71 | + <.input | |
| 72 | + field={@form[:release_retention_days]} | |
| 73 | + type="number" | |
| 74 | + label="Releases (days)" | |
| 75 | + min="1" | |
| 76 | + max={Vigil.Retention.default_days(:releases)} | |
| 77 | + /> | |
| 78 | + </div> | |
| 79 | + </div> | |
| 48 | 80 | <.button |
| 49 | 81 | :if={Scope.can?(@current_scope, "org:write")} |
| 50 | 82 | class="btn btn-primary btn-sm" |
added
priv/repo/migrations/20261202034037_add_org_retention.exs
+13
−0
@@ -0,0 +1,13 @@
| 1 | +defmodule Vigil.Repo.Migrations.AddOrgRetention do | |
| 2 | + use Ecto.Migration | |
| 3 | + | |
| 4 | + # Slice 11 (ROADMAP.md): an organization may keep its data for less than | |
| 5 | + # the instance does (nil: the instance's default, Vigil.Retention). | |
| 6 | + def change do | |
| 7 | + alter table(:organizations) do | |
| 8 | + add :event_retention_days, :integer | |
| 9 | + add :log_retention_days, :integer | |
| 10 | + add :release_retention_days, :integer | |
| 11 | + end | |
| 12 | + end | |
| 13 | +end |
added
test/vigil/retention_test.exs
+181
−0
@@ -0,0 +1,181 @@
| 1 | +defmodule Vigil.RetentionTest do | |
| 2 | + use Vigil.DataCase, async: true | |
| 3 | + use Oban.Testing, repo: Vigil.Repo | |
| 4 | + | |
| 5 | + import Vigil.AccountsFixtures | |
| 6 | + import Vigil.OrganizationsFixtures | |
| 7 | + | |
| 8 | + alias Vigil.{Ingest, Issues, Organizations, Retention} | |
| 9 | + alias Vigil.Artifacts.{ArtifactBundle, ArtifactFile} | |
| 10 | + alias Vigil.Ingest.{Drain, Envelope} | |
| 11 | + alias Vigil.Issues.{Issue, IssueEvent} | |
| 12 | + alias Vigil.Organizations.Organization | |
| 13 | + alias Vigil.Projects.Project | |
| 14 | + alias Vigil.Releases.Release | |
| 15 | + alias Vigil.Retention.PurgeWorker | |
| 16 | + | |
| 17 | + setup do | |
| 18 | + owner = user_fixture() | |
| 19 | + org = organization_fixture(owner) | |
| 20 | + scope = org_scope(owner, org) | |
| 21 | + project = project_fixture(scope) | |
| 22 | + | |
| 23 | + target = %Ingest.Target{ | |
| 24 | + project_id: project.id, | |
| 25 | + dsn_id: project.dsn_id, | |
| 26 | + organization_id: org.id, | |
| 27 | + key_id: Ecto.UUID.generate() | |
| 28 | + } | |
| 29 | + | |
| 30 | + %{org: org, scope: scope, project: project, target: target} | |
| 31 | + end | |
| 32 | + | |
| 33 | + defp send_error(target, type, release \\ nil) do | |
| 34 | + event = | |
| 35 | + %{ | |
| 36 | + "exception" => [%{"type" => type, "value" => "v"}], | |
| 37 | + "event_id" => String.replace(Ecto.UUID.generate(), "-", "") | |
| 38 | + } | |
| 39 | + |> then(&if(release, do: Map.put(&1, "release", release), else: &1)) | |
| 40 | + | |
| 41 | + item = %Envelope.Item{ | |
| 42 | + type: "event", | |
| 43 | + headers: %{"type" => "event"}, | |
| 44 | + payload: Jason.encode!(event) | |
| 45 | + } | |
| 46 | + | |
| 47 | + Ingest.accept(target, %{headers: %{}, items: [item]}, %{client_ip: nil, user_agent: nil}) | |
| 48 | + Drain.drain(target.project_id) | |
| 49 | + end | |
| 50 | + | |
| 51 | + defp bundle(org, project, release) do | |
| 52 | + id = Vigil.Schema.uuid7() | |
| 53 | + key = "artifacts/#{org.id}/#{id}/f" | |
| 54 | + :ok = Vigil.Storage.put(key, "data") | |
| 55 | + | |
| 56 | + Repo.insert!(%ArtifactBundle{ | |
| 57 | + id: id, | |
| 58 | + organization_id: org.id, | |
| 59 | + checksum: Base.encode16(:crypto.strong_rand_bytes(20), case: :lower), | |
| 60 | + release: release, | |
| 61 | + artifact_count: 1 | |
| 62 | + }) | |
| 63 | + | |
| 64 | + Repo.insert!(%ArtifactFile{ | |
| 65 | + artifact_bundle_id: id, | |
| 66 | + organization_id: org.id, | |
| 67 | + kind: "minified_source", | |
| 68 | + url: "~/a.js", | |
| 69 | + size: 4, | |
| 70 | + storage_key: key | |
| 71 | + }) | |
| 72 | + | |
| 73 | + Repo.insert_all("artifact_bundle_projects", [ | |
| 74 | + %{artifact_bundle_id: Ecto.UUID.dump!(id), project_id: Ecto.UUID.dump!(project.id)} | |
| 75 | + ]) | |
| 76 | + | |
| 77 | + {id, key} | |
| 78 | + end | |
| 79 | + | |
| 80 | + test "an organization's settings keep at most what the instance keeps", ctx do | |
| 81 | + assert {:error, cs} = | |
| 82 | + Organizations.update_organization(ctx.scope, ctx.org, %{ | |
| 83 | + "event_retention_days" => 9999 | |
| 84 | + }) | |
| 85 | + | |
| 86 | + assert %{event_retention_days: [_]} = errors_on(cs) | |
| 87 | + | |
| 88 | + assert {:ok, org} = | |
| 89 | + Organizations.update_organization(ctx.scope, ctx.org, %{"event_retention_days" => 7}) | |
| 90 | + | |
| 91 | + assert Retention.days(org, :events) == 7 | |
| 92 | + assert Retention.days(org, :logs) == Retention.default_days(:logs) | |
| 93 | + end | |
| 94 | + | |
| 95 | + test "a shorter event retention deletes the org's old events and their issues", ctx do | |
| 96 | + send_error(ctx.target, "KeyError") | |
| 97 | + send_error(ctx.target, "ValueError") | |
| 98 | + [old, recent] = Repo.all(from i in Issue, order_by: i.title) | |
| 99 | + | |
| 100 | + {:ok, org} = | |
| 101 | + Organizations.update_organization(ctx.scope, ctx.org, %{"event_retention_days" => 7}) | |
| 102 | + | |
| 103 | + _ = org | |
| 104 | + | |
| 105 | + # Ten days from now, KeyError's last event is past 7 days; ValueError | |
| 106 | + # was seen again yesterday (relative). | |
| 107 | + future = DateTime.add(DateTime.utc_now(), 10, :day) | |
| 108 | + | |
| 109 | + Repo.update_all(from(i in Issue, where: i.id == ^recent.id), | |
| 110 | + set: [last_seen: DateTime.add(future, -1, :day)] | |
| 111 | + ) | |
| 112 | + | |
| 113 | + counts = Retention.sweep(future) | |
| 114 | + assert counts.events == 2 | |
| 115 | + assert counts.issues == 1 | |
| 116 | + | |
| 117 | + refute Repo.get(Issue, old.id) | |
| 118 | + assert Repo.get(Issue, recent.id) | |
| 119 | + assert Repo.aggregate(IssueEvent, :count) == 0 | |
| 120 | + end | |
| 121 | + | |
| 122 | + test "soft-deleted issues go after a day", ctx do | |
| 123 | + send_error(ctx.target, "KeyError") | |
| 124 | + [issue] = Repo.all(Issue) | |
| 125 | + {:ok, _} = Issues.delete(ctx.scope, issue) | |
| 126 | + | |
| 127 | + assert %{deleted_issues: 0} = Retention.sweep() | |
| 128 | + assert %{deleted_issues: 1} = Retention.sweep(DateTime.add(DateTime.utc_now(), 2, :day)) | |
| 129 | + refute Repo.get(Issue, issue.id) | |
| 130 | + end | |
| 131 | + | |
| 132 | + test "stale releases go with their bundles; active ones stay", ctx do | |
| 133 | + send_error(ctx.target, "KeyError", "web@1.0") | |
| 134 | + Vigil.Releases.ensure_release(ctx.org.id, ctx.project.id, "web@0.1") | |
| 135 | + {_, old_key} = bundle(ctx.org, ctx.project, "web@0.1") | |
| 136 | + {kept, _} = bundle(ctx.org, ctx.project, "web@1.0") | |
| 137 | + | |
| 138 | + # 400 days on: web@0.1 has no issue since; web@1.0's issue is "seen" then. | |
| 139 | + future = DateTime.add(DateTime.utc_now(), 400, :day) | |
| 140 | + Repo.update_all(Issue, set: [last_seen: future]) | |
| 141 | + | |
| 142 | + assert %{releases: 1} = Retention.sweep(future) | |
| 143 | + assert [%{version: "web@1.0"}] = Repo.all(Release) | |
| 144 | + assert [%{id: ^kept}] = Repo.all(ArtifactBundle) | |
| 145 | + assert {:error, :not_found} = Vigil.Storage.get(old_key) | |
| 146 | + end | |
| 147 | + | |
| 148 | + test "deleted projects and organizations are purged a day later, files and all", ctx do | |
| 149 | + send_error(ctx.target, "KeyError") | |
| 150 | + {bundle_id, key} = bundle(ctx.org, ctx.project, nil) | |
| 151 | + {:ok, _} = Vigil.Projects.delete_project(ctx.scope, ctx.project) | |
| 152 | + | |
| 153 | + assert %{purges_queued: 0} = Retention.sweep() | |
| 154 | + later = DateTime.add(DateTime.utc_now(), 2, :day) | |
| 155 | + assert %{purges_queued: 1} = Retention.sweep(later) | |
| 156 | + | |
| 157 | + project_id = ctx.project.id | |
| 158 | + assert_enqueued(worker: PurgeWorker, args: %{project_id: project_id}) | |
| 159 | + assert :ok = perform_job(PurgeWorker, %{project_id: project_id}) | |
| 160 | + | |
| 161 | + refute Repo.get(Project, project_id) | |
| 162 | + refute Repo.get(ArtifactBundle, bundle_id) | |
| 163 | + assert {:error, :not_found} = Vigil.Storage.get(key) | |
| 164 | + assert Repo.aggregate(Issue, :count) == 0 | |
| 165 | + | |
| 166 | + other = project_fixture(ctx.scope, %{"name" => "other"}) | |
| 167 | + | |
| 168 | + Repo.update_all(from(o in Organization, where: o.id == ^ctx.org.id), | |
| 169 | + set: [deleted_at: ~U[2026-01-01 00:00:00Z]] | |
| 170 | + ) | |
| 171 | + | |
| 172 | + assert :ok = perform_job(PurgeWorker, %{organization_id: ctx.org.id}) | |
| 173 | + refute Repo.get(Organization, ctx.org.id) | |
| 174 | + refute Repo.get(Project, other.id) | |
| 175 | + end | |
| 176 | + | |
| 177 | + test "a restored project isn't purged", ctx do | |
| 178 | + assert :ok = perform_job(PurgeWorker, %{project_id: ctx.project.id}) | |
| 179 | + assert Repo.get(Project, ctx.project.id) | |
| 180 | + end | |
| 181 | +end |
modified
test/vigil_web/live/org_settings_live_test.exs
+17
−0
@@ -83,4 +83,21 @@ defmodule VigilWeb.OrgSettingsLiveTest do
| 83 | 83 | {:ok, view, _} = live(conn, ~p"/orgs/#{other.slug}/settings/groups") |
| 84 | 84 | refute has_element?(view, "#rule-form") |
| 85 | 85 | end |
| 86 | + | |
| 87 | + test "the retention settings are saved, and capped", %{conn: conn, org: org} do | |
| 88 | + {:ok, view, _} = live(conn, ~p"/orgs/#{org.slug}/settings") | |
| 89 | + assert has_element?(view, "#org-retention") | |
| 90 | + | |
| 91 | + view | |
| 92 | + |> form("#org-settings-form", organization: %{event_retention_days: "100000"}) | |
| 93 | + |> render_submit() | |
| 94 | + | |
| 95 | + assert is_nil(Vigil.Repo.reload!(org).event_retention_days) | |
| 96 | + | |
| 97 | + view | |
| 98 | + |> form("#org-settings-form", organization: %{event_retention_days: "14"}) | |
| 99 | + |> render_submit() | |
| 100 | + | |
| 101 | + assert Vigil.Repo.reload!(org).event_retention_days == 14 | |
| 102 | + end | |
| 86 | 103 | end |
Parents: e7312de