Skip to content

Commit

Permalink
Make invitations cleanup worker rely on UTC time (plausible#4610)
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldar authored Sep 25, 2024
1 parent 3251b5b commit 6981972
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
8 changes: 7 additions & 1 deletion lib/workers/clean_invitations.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@ defmodule Plausible.Workers.CleanInvitations do
use Plausible.Repo
use Oban.Worker, queue: :clean_invitations

@cutoff Duration.new!(hour: -48)

@impl Oban.Worker
def perform(_job) do
cutoff_time =
NaiveDateTime.utc_now(:second)
|> NaiveDateTime.shift(@cutoff)

Repo.delete_all(
from i in Plausible.Auth.Invitation,
where: i.inserted_at < fragment("now() - INTERVAL '48 hours'")
where: i.inserted_at < ^cutoff_time
)

:ok
Expand Down
8 changes: 6 additions & 2 deletions test/workers/clean_invitations_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ defmodule Plausible.Workers.CleanInvitationsTest do
alias Plausible.Workers.CleanInvitations

test "cleans invitation that is more than 48h old" do
now = NaiveDateTime.utc_now(:second)

insert(:invitation,
inserted_at: Timex.shift(Timex.now(), hours: -49),
inserted_at: NaiveDateTime.shift(now, hour: -49),
site: build(:site),
inviter: build(:user)
)
Expand All @@ -15,8 +17,10 @@ defmodule Plausible.Workers.CleanInvitationsTest do
end

test "does not clean invitation that is less than 48h old" do
now = NaiveDateTime.utc_now(:second)

insert(:invitation,
inserted_at: Timex.shift(Timex.now(), hours: -47),
inserted_at: NaiveDateTime.shift(now, hour: -47),
site: build(:site),
inviter: build(:user)
)
Expand Down

0 comments on commit 6981972

Please sign in to comment.