Skip to content

Commit

Permalink
add cron based version pruning
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Jul 4, 2024
1 parent c932de3 commit 712bc88
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 1 deletion.
8 changes: 7 additions & 1 deletion config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ config :polar,

config :polar, Polar.Vault, json_library: Jason

config :polar, Oban, engine: Oban.Engines.Basic, queues: [default: 3], repo: Polar.Repo
config :polar, Oban,
engine: Oban.Engines.Basic,
queues: [default: 3],
repo: Polar.Repo,
plugins: [
{Oban.Plugins.Cron, crontab: [{"@daily", Polar.Streams.Version.Pruning}]}
]

# Configures the endpoint
config :polar, PolarWeb.Endpoint,
Expand Down
31 changes: 31 additions & 0 deletions lib/polar/streams/version/pruning.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
defmodule Polar.Streams.Version.Pruning do
use Oban.Worker, queue: :default

alias Polar.Repo
alias Polar.Accounts.Automation
alias Polar.Streams.Version

import Ecto.Query, only: [from: 2]

@age_days 2

def perform(%Job{}) do
age =
DateTime.utc_now()
|> DateTime.add(-@age_days, :day)

versions_to_deactivate =
from(v in Version,
where: v.current_state == "testing" and v.inserted_at < ^age
)
|> Repo.all()

bot = Automation.get_bot!()

Enum.each(versions_to_deactivate, fn version ->
Eventful.Transit.perform(version, bot, "deactivate", comment: "testing period expired")
end)

:ok
end
end
111 changes: 111 additions & 0 deletions test/polar/streams/version/pruning_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
defmodule Polar.Streams.Version.PruningTest do
use Polar.DataCase, async: true
use Oban.Testing, repo: Polar.Repo

import Polar.AccountsFixtures

alias Polar.Repo
alias Polar.Accounts

alias Polar.Streams
alias Polar.Streams.Product
alias Polar.Streams.Version
alias Polar.Streams.Version.Pruning

setup do
password = Accounts.generate_automation_password()

bot = bot_fixture(%{password: password})

{:ok, bot: bot}
end

setup %{bot: bot} do
{:ok, %Product{} = with_active_versions} =
Streams.create_product(%{
aliases: ["alpine/3.18", "alpine/3.18/default"],
arch: "amd64",
os: "Alpine",
release: "3.18",
release_title: "3.18",
variant: "default",
requirements: %{
secureboot: "false"
}
})

{:ok, version} =
Streams.create_version(with_active_versions, %{
serial: "20240209-36",
items: [
%{
name: "lxd.tar.gz",
file_type: "lxd.tar.gz",
hash: "35363f3d086271ed5402d61ab18ec03987bed51758c00079b8c9d372ff6d62dd",
size: 876,
path: "images/alpine/edge/amd64/default/20240209_13:00/incus.tar.xz"
},
%{
name: "root.squashfs",
file_type: "squashfs",
hash: "47cc4070da1bf17d8364c390…3603f4ed7e9e46582e690d2",
size: 2_982_800,
path: "images/alpine/edge/amd64/default/20240209_13:00/rootfs.tar.xz"
}
]
})

{:ok, another_version} =
Streams.create_version(with_active_versions, %{
serial: "20240209-37",
items: [
%{
name: "lxd.tar.gz",
file_type: "lxd.tar.gz",
hash: "35363f3d086271ed5402d61ab18ec03987bed51758c00079b8c9d372ff6d62aa",
size: 876,
path: "images/alpine/edge/amd64/default/20240209_13:00/incus.tar.xz"
},
%{
name: "root.squashfs",
file_type: "squashfs",
hash: "47cc4070da1bf17d8364c390…3603f4ed7e9e46582e690aa",
size: 2_982_800,
path: "images/alpine/edge/amd64/default/20240209_13:00/rootfs.tar.xz"
}
]
})

{:ok, %{resource: testing_version}} = Eventful.Transit.perform(version, bot, "test")

{:ok, %{resource: another_testing_version}} =
Eventful.Transit.perform(another_version, bot, "test")

expired =
DateTime.utc_now()
|> DateTime.add(-4, :day)

query = from(v in Version, where: v.id == ^version.id)

Repo.update_all(query, set: [inserted_at: expired])

{:ok, version: testing_version, another_version: another_testing_version}
end

describe "perform" do
test "successfully deactivate expired version", %{
version: version,
another_version: another_version
} do
assert version.current_state == "testing"
assert another_version.current_state == "testing"

assert :ok = perform_job(Pruning, %{})

version = Repo.reload(version)

assert version.current_state == "inactive"
assert another_version.current_state == "testing"
end
end
end

0 comments on commit 712bc88

Please sign in to comment.