Skip to content

Commit

Permalink
fix observer cron (#1351)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljguarino authored Aug 30, 2024
1 parent 311cda6 commit cc1839f
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/console/deployments/cron.ex
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,7 @@ defmodule Console.Deployments.Cron do
def run_observers() do
Observer.runnable()
|> Observer.ordered(asc: :id)
|> Repo.stream(method: :keyset)
|> Console.throttle()
|> Flow.from_enumerable()
|> Flow.map(&Console.Deployments.Observer.Runner.run/1)
Expand Down
10 changes: 8 additions & 2 deletions lib/console/schema/app_notification.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ defmodule Console.Schema.AppNotification do
use Piazza.Ecto.Schema
alias Console.Schema.{User}

@expiry [days: -7]
@too_old [days: -30]

defenum Priority, low: 0, medium: 1, high: 2

schema "app_notifications" do
Expand All @@ -27,9 +30,12 @@ defmodule Console.Schema.AppNotification do
end

def expired(query \\ __MODULE__) do
expiry = Timex.now() |> Timex.shift(days: -7)
expiry = Timex.now() |> Timex.shift(@expiry)
too_old = Timex.now() |> Timex.shift(@too_old)

from(n in query, where: not is_nil(n.read_at) and n.read_at < ^expiry)
from(n in query,
where: (not is_nil(n.read_at) and n.read_at < ^expiry) or n.inserted_at < ^too_old
)
end

def changeset(model, attrs \\ %{}) do
Expand Down
36 changes: 36 additions & 0 deletions test/console/deployments/cron_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ defmodule Console.Deployments.CronTest do
alias Kazan.Apis.Core.V1, as: Core
alias Console.Deployments.{Cron, Clusters, Services}


describe "#prune_services/0" do
test "it will wipe stale drained services" do
svcs = insert_list(3, :service, deleted_at: Timex.now())
Expand Down Expand Up @@ -266,4 +267,39 @@ defmodule Console.Deployments.CronTest do
end
end
end

describe "#prune_notifications/0" do
test "it will wipe old read or really old unread notifications" do
read = insert_list(2, :app_notification, read_at: Timex.now() |> Timex.shift(days: -8))
unread = insert_list(2, :app_notification, inserted_at: Timex.now() |> Timex.shift(days: -40))
ignore = insert_list(3, :app_notification)

Cron.prune_notifications()

for n <- read ++ unread,
do: refute refetch(n)

for n <- ignore,
do: assert refetch(n)
end
end
end

defmodule Console.Deployments.AsyncCronTest do
use Console.DataCase, async: false
use Mimic
alias Console.Deployments.{Cron}

setup :set_mimic_global

describe "#run_observers/0" do
test "it can execute pending observers" do
insert_list(2, :observer, next_run_at: Timex.now() |> Timex.shift(minutes: -1))
insert(:observer, next_run_at: Timex.now() |> Timex.shift(minutes: 5))

expect(Console.Deployments.Observer.Runner, :run, 2, fn _ -> :ok end)

:ok = Cron.run_observers()
end
end
end
1 change: 1 addition & 0 deletions test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Mimic.copy(Tentacat.App.Installations)
Mimic.copy(Tentacat.Organizations.Hooks)
Mimic.copy(Console.Deployments.Pr.Git)
Mimic.copy(Console.Deployments.Pr.Dispatcher)
Mimic.copy(Console.Deployments.Observer.Runner)

ExUnit.start()
Ecto.Adapters.SQL.Sandbox.mode(Console.Repo, :manual)
Expand Down

0 comments on commit cc1839f

Please sign in to comment.