-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from tofran/expired-session-pruner
Added ExpiredSessionPruner GenServer to clear expired session from the DB
- Loading branch information
Showing
2 changed files
with
69 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
defmodule EctoSessions.ExpiredSessionPruner do | ||
@moduledoc """ | ||
`GenServer` implementation to delete expired sessions periodically. Given an `EctoSessions` module | ||
and periodicity, in milliseconds. | ||
## Usage | ||
- In your project's `application.ex`: | ||
``` | ||
def start(_type, _args) do | ||
children = [ | ||
# ... | ||
{EctoSessions.ExpiredSessionPruner, {YourSessionsModule, :timer.hours(24)}} | ||
] | ||
opts = [strategy: :one_for_one, name: EctoSessionsDemo.Supervisor] | ||
Supervisor.start_link(children, opts) | ||
end | ||
``` | ||
- Low level usage with `start_link`: | ||
``` | ||
GenServer.start_link( | ||
EctoSessions.ExpiredSessionPruner, | ||
{YourSessionsModule, 10_000} | ||
) | ||
``` | ||
Where `YourSessionsModule` is any module that uses `EctoSessions` and the second argument the | ||
number of milliseconds to 'sleep' between cycles. Ex: `12 * 60 * 60 * 1000` for 12h. Use `:timer` | ||
for readability. | ||
""" | ||
|
||
use GenServer | ||
|
||
require Logger | ||
|
||
def start_link(args = {module, _sleep_time}) when is_atom(module) do | ||
GenServer.start_link( | ||
__MODULE__, | ||
args, | ||
name: {:global, Module.concat(__MODULE__, module)} | ||
) | ||
end | ||
|
||
def init(state = {module, sleep_time}) when is_atom(module) do | ||
Logger.debug("Starting session pruner for #{module} (every #{sleep_time}ms)") | ||
|
||
{:ok, prune(state)} | ||
end | ||
|
||
def handle_info(:prune, state) do | ||
{:noreply, prune(state)} | ||
end | ||
|
||
defp prune(state = {module, sleep_time}) do | ||
delete_count = apply(module, :delete_expired, []) | ||
|
||
Logger.info("Deleted #{delete_count} expired sessions with #{module}") | ||
|
||
Process.send_after(self(), :prune, sleep_time) | ||
|
||
state | ||
end | ||
end |