Skip to content

Commit d366d1b

Browse files
committed
consistent naming
1 parent 7ee4c8b commit d366d1b

File tree

4 files changed

+54
-98
lines changed

4 files changed

+54
-98
lines changed

03_global_background_job/lib/background_job/application.ex

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,7 @@ defmodule GlobalBackgroundJob.Application do
99
def start(_type, _args) do
1010
children = [
1111
{Cluster.Supervisor, [topologies(), [name: GlobalBackgroundJob.ClusterSupervisor]]},
12-
{GlobalBackgroundJob.Singleton,
13-
[
14-
mod: GlobalBackgroundJob.DatabaseCleaner,
15-
args: [timeout: :timer.seconds(5)],
16-
name: DatabaseCleaner
17-
]}
12+
{GlobalBackgroundJob.DatabaseCleaner.Starter, [timeout: :timer.seconds(2)]}
1813
]
1914

2015
opts = [strategy: :one_for_one, name: GlobalBackgroundJob.Supervisor]
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
defmodule GlobalBackgroundJob.DatabaseCleaner.Starter do
2+
@moduledoc """
3+
GenServer which starts and monitors another process, restarting
4+
it when it goes down.
5+
"""
6+
7+
use GenServer
8+
require Logger
9+
10+
alias GlobalBackgroundJob.DatabaseCleaner
11+
12+
def start_link(opts) do
13+
GenServer.start_link(__MODULE__, opts, name: __MODULE__)
14+
end
15+
16+
@impl GenServer
17+
def init(opts) do
18+
pid = start_and_monitor(opts)
19+
20+
{:ok, {pid, opts}}
21+
end
22+
23+
@impl GenServer
24+
def handle_info({:DOWN, _, :process, pid, _reason}, {pid, opts} = state) do
25+
log("process down, restarting... #{inspect(state)}")
26+
27+
{:noreply, {start_and_monitor(opts), opts}}
28+
end
29+
30+
defp start_and_monitor(opts) do
31+
log("restarting #{inspect(opts)}...")
32+
33+
pid =
34+
case GenServer.start_link(DatabaseCleaner, opts, name: {:global, DatabaseCleaner}) do
35+
{:ok, pid} ->
36+
pid
37+
38+
{:error, {:already_started, pid}} ->
39+
log("...already started!")
40+
pid
41+
end
42+
43+
Process.monitor(pid)
44+
45+
log("monitoring #{inspect(pid)}...")
46+
47+
pid
48+
end
49+
50+
defp log(text) do
51+
Logger.info("----[#{node()}] #{__MODULE__} #{text}")
52+
end
53+
end

03_global_background_job/lib/background_job/singleton.ex

Lines changed: 0 additions & 67 deletions
This file was deleted.

03_global_background_job/lib/background_job/singleton/state.ex

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)