-
-
Notifications
You must be signed in to change notification settings - Fork 309
/
postgres.ex
190 lines (146 loc) · 4.72 KB
/
postgres.ex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
defmodule Oban.Peers.Postgres do
@moduledoc """
A Postgres based peer that coordinates centrally through a database table.
Postgres peers don't require clustering through distributed Erlang or any other
interconnectivity between nodes. Leadership is coordinated through the `oban_peers` table in
your database. With a standard Oban config the `oban_peers` table will only have one row, and
that node is the leader.
Applications that run multiple Oban instances will have one row per instance. For example, an
umbrella application that runs `Oban.A` and `Oban.B` will have two rows in `oban_peers`.
## Usage
Specify the `Postgres` peer in your Oban configuration.
config :my_app, Oban,
peer: Oban.Peers.Postgres,
...
"""
@behaviour Oban.Peer
use GenServer
import Ecto.Query, only: [where: 2, where: 3]
alias Oban.{Backoff, Notifier, Repo}
require Logger
defmodule State do
@moduledoc false
defstruct [
:conf,
:name,
:timer,
interval: :timer.seconds(30),
leader?: false,
leader_boost: 2
]
end
@impl Oban.Peer
def start_link(opts) do
GenServer.start_link(__MODULE__, opts, name: opts[:name])
end
@impl Oban.Peer
def leader?(pid, timeout \\ 5_000) do
GenServer.call(pid, :leader?, timeout)
end
@impl GenServer
def init(opts) do
Process.flag(:trap_exit, true)
{:ok, struct!(State, opts), {:continue, :start}}
end
@impl GenServer
def terminate(_reason, %State{timer: timer} = state) do
if is_reference(timer), do: Process.cancel_timer(timer)
if state.leader? do
try do
Repo.transaction(state.conf, fn ->
delete_self(state)
notify_down(state)
end)
catch
:exit, _reason -> :ok
end
end
:ok
end
@impl GenServer
def handle_continue(:start, %State{} = state) do
Notifier.listen(state.conf.name, [:leader])
handle_info(:election, state)
end
@impl GenServer
def handle_info(:election, %State{} = state) do
meta = %{conf: state.conf, leader: state.leader?, peer: __MODULE__}
state =
:telemetry.span([:oban, :peer, :election], meta, fn ->
{:ok, state} =
Repo.transaction(state.conf, fn ->
state
|> delete_expired_peers()
|> upsert_peer()
end)
{state, %{meta | leader: state.leader?}}
end)
{:noreply, schedule_election(state)}
rescue
error in [Postgrex.Error] ->
if error.postgres.code == :undefined_table do
Logger.warn("""
The `oban_peers` table is undefined and leadership is disabled.
Run migrations up to v11 to restore peer leadership. In the meantime, distributed plugins
(e.g. Cron, Pruner, Stager) will not run on any nodes.
""")
end
{:noreply, schedule_election(%{state | leader?: false})}
end
def handle_info({:notification, :leader, %{"down" => name}}, %State{conf: conf} = state) do
if name == inspect(conf.name) do
handle_info(:election, state)
else
{:noreply, state}
end
end
def handle_info(message, state) do
Logger.warn("Received unexpected message: #{inspect(message)}")
{:noreply, state}
end
@impl GenServer
def handle_call(:leader?, _from, %State{} = state) do
{:reply, state.leader?, state}
end
# Helpers
defp schedule_election(%State{interval: interval} = state) do
base = if state.leader?, do: div(interval, state.leader_boost), else: interval
time = Backoff.jitter(base, mode: :dec)
%{state | timer: Process.send_after(self(), :election, time)}
end
defp delete_expired_peers(%State{conf: conf} = state) do
query =
"oban_peers"
|> where([p], p.name == ^inspect(conf.name))
|> where([p], p.expires_at < ^DateTime.utc_now())
Repo.delete_all(conf, query)
state
end
defp delete_self(%State{conf: conf}) do
query = where("oban_peers", name: ^inspect(conf.name), node: ^conf.node)
Repo.delete_all(conf, query)
end
defp notify_down(%State{conf: conf}) do
Notifier.notify(conf, :leader, %{down: inspect(conf.name)})
end
defp upsert_peer(%State{conf: conf} = state) do
started_at = DateTime.utc_now()
expires_at = DateTime.add(started_at, state.interval, :millisecond)
peer_data = %{
name: inspect(conf.name),
node: conf.node,
started_at: started_at,
expires_at: expires_at
}
repo_opts =
if state.leader? do
[conflict_target: :name, on_conflict: [set: [expires_at: expires_at]]]
else
[on_conflict: :nothing]
end
case Repo.insert_all(conf, "oban_peers", [peer_data], repo_opts) do
{1, _} -> %{state | leader?: true}
{0, _} -> %{state | leader?: false}
end
end
end