Skip to content

Commit

Permalink
Implement Oban worker removing expired user sessions with grace period
Browse files Browse the repository at this point in the history
  • Loading branch information
zoldar committed Aug 23, 2024
1 parent 9f6b657 commit 45361dd
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions config/runtime.exs
Original file line number Diff line number Diff line change
Expand Up @@ -589,6 +589,8 @@ base_cron = [
# Every day at 1am
{"0 1 * * *", Plausible.Workers.CleanInvitations},
# Every 2 hours
{"30 */2 * * *", Plausible.Workers.CleanUserSessions},
# Every 2 hours
{"0 */2 * * *", Plausible.Workers.ExpireDomainChangeTransitions},
# Daily at midnight
{"0 0 * * *", Plausible.Workers.LocationsSync}
Expand Down Expand Up @@ -619,6 +621,7 @@ base_queues = [
check_stats_emails: 1,
site_setup_emails: 1,
clean_invitations: 1,
clean_user_sessions: 1,
analytics_imports: 1,
analytics_exports: 1,
notify_exported_analytics: 1,
Expand Down
24 changes: 24 additions & 0 deletions lib/workers/clean_user_sessions.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
defmodule Plausible.Workers.CleanUserSessions do
@moduledoc """
Job removing expired user sessions. A grace period is applied.
"""

use Plausible.Repo
use Oban.Worker, queue: :clean_user_sessions

@grace_period Duration.new!(day: -7)

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

Repo.delete_all(
from us in Plausible.Auth.UserSession,
where: us.timeout_at < ^grace_cutoff
)

:ok
end
end

0 comments on commit 45361dd

Please sign in to comment.