Skip to content

Commit 27bf9b8

Browse files
committed
Use GenServer.cast for updates
1 parent a884602 commit 27bf9b8

File tree

1 file changed

+52
-75
lines changed

1 file changed

+52
-75
lines changed

lib/quantum_storage_ets.ex

Lines changed: 52 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -47,16 +47,16 @@ defmodule QuantumStoragePersistentEts do
4747

4848
@doc false
4949
@impl Quantum.Storage
50-
def add_job(storage_pid, job), do: GenServer.call(storage_pid, {:add_job, job})
50+
def add_job(storage_pid, job), do: GenServer.cast(storage_pid, {:add_job, job})
5151

5252
@doc false
5353
@impl Quantum.Storage
54-
def delete_job(storage_pid, job_name), do: GenServer.call(storage_pid, {:delete_job, job_name})
54+
def delete_job(storage_pid, job_name), do: GenServer.cast(storage_pid, {:delete_job, job_name})
5555

5656
@doc false
5757
@impl Quantum.Storage
5858
def update_job_state(storage_pid, job_name, state),
59-
do: GenServer.call(storage_pid, {:update_job_state, job_name, state})
59+
do: GenServer.cast(storage_pid, {:update_job_state, job_name, state})
6060

6161
@doc false
6262
@impl Quantum.Storage
@@ -65,51 +65,15 @@ defmodule QuantumStoragePersistentEts do
6565
@doc false
6666
@impl Quantum.Storage
6767
def update_last_execution_date(storage_pid, last_execution_date),
68-
do: GenServer.call(storage_pid, {:update_last_execution_date, last_execution_date})
68+
do: GenServer.cast(storage_pid, {:update_last_execution_date, last_execution_date})
6969

7070
@doc false
7171
@impl Quantum.Storage
72-
def purge(storage_pid), do: GenServer.call(storage_pid, :purge)
72+
def purge(storage_pid), do: GenServer.cast(storage_pid, :purge)
7373

7474
@doc false
7575
@impl GenServer
76-
def handle_call({:add_job, job}, _from, %State{table: table} = state) do
77-
{:reply, do_add_job(table, job), state}
78-
end
79-
80-
def handle_call(:jobs, _from, %State{table: table} = state) do
81-
{:reply, do_get_jobs(table), state}
82-
end
83-
84-
def handle_call({:delete_job, job}, _from, %State{table: table} = state) do
85-
{:reply, do_delete_job(table, job), state}
86-
end
87-
88-
def handle_call({:update_job_state, job_name, job_state}, _from, %State{table: table} = state) do
89-
{:reply, do_update_job_state(table, job_name, job_state), state}
90-
end
91-
92-
def handle_call(:last_execution_date, _from, %State{table: table} = state) do
93-
{:reply, do_get_last_execution_date(table), state}
94-
end
95-
96-
def handle_call(
97-
{:update_last_execution_date, last_execution_date},
98-
_from,
99-
%State{table: table} = state
100-
) do
101-
{:reply, do_update_last_execution_date(table, last_execution_date), state}
102-
end
103-
104-
def handle_call(:purge, _from, %State{table: table} = state) do
105-
{:reply, do_purge(table), state}
106-
end
107-
108-
defp job_key(job_name) do
109-
{:job, job_name}
110-
end
111-
112-
defp do_add_job(table, job) do
76+
def handle_cast({:add_job, job}, %State{table: table} = state) do
11377
:ets.insert(table, entry = {job_key(job.name), job})
11478
:ets.insert(table, {:init_jobs})
11579

@@ -119,54 +83,67 @@ defmodule QuantumStoragePersistentEts do
11983
}]"
12084
end)
12185

122-
:ok
86+
{:noreply, state}
12387
end
12488

125-
defp do_get_jobs(table) do
126-
table
127-
|> :ets.lookup(:init_jobs)
128-
|> case do
129-
[{:init_jobs}] ->
130-
table
131-
|> :ets.match({{:job, :_}, :"$1"})
132-
|> List.flatten()
133-
134-
[] ->
135-
:not_applicable
136-
end
137-
end
138-
139-
defp do_delete_job(table, job_name) do
89+
def handle_cast({:delete_job, job_name}, %State{table: table} = state) do
14090
:ets.delete(table, job_key(job_name))
14191

142-
:ok
92+
{:noreply, state}
14393
end
14494

145-
defp do_update_job_state(table, job_name, state) do
95+
def handle_cast({:update_job_state, job_name, job_state}, %State{table: table} = state) do
14696
table
14797
|> :ets.lookup(job_key(job_name))
148-
|> Enum.map(&{elem(&1, 0), %{elem(&1, 1) | state: state}})
98+
|> Enum.map(&{elem(&1, 0), %{elem(&1, 1) | state: job_state}})
14999
|> Enum.each(&:ets.update_element(table, elem(&1, 0), {2, elem(&1, 1)}))
150100

151-
:ok
152-
end
153-
154-
defp do_get_last_execution_date(table) do
155-
table
156-
|> :ets.lookup(:last_execution_date)
157-
|> case do
158-
[] -> :unknown
159-
[{:last_execution_date, date} | _t] -> date
160-
end
101+
{:noreply, state}
161102
end
162103

163-
defp do_update_last_execution_date(table, last_execution_date) do
104+
def handle_cast(
105+
{:update_last_execution_date, last_execution_date},
106+
%State{table: table} = state
107+
) do
164108
:ets.insert(table, {:last_execution_date, last_execution_date})
165-
:ok
109+
110+
{:noreply, state}
166111
end
167112

168-
defp do_purge(table) do
113+
def handle_cast(:purge, %State{table: table} = state) do
169114
:ets.delete_all_objects(table)
170-
:ok
115+
116+
{:noreply, state}
117+
end
118+
119+
@doc false
120+
@impl GenServer
121+
def handle_call(:jobs, _from, %State{table: table} = state) do
122+
{:reply,
123+
table
124+
|> :ets.lookup(:init_jobs)
125+
|> case do
126+
[{:init_jobs}] ->
127+
table
128+
|> :ets.match({{:job, :_}, :"$1"})
129+
|> List.flatten()
130+
131+
[] ->
132+
:not_applicable
133+
end, state}
134+
end
135+
136+
def handle_call(:last_execution_date, _from, %State{table: table} = state) do
137+
{:reply,
138+
table
139+
|> :ets.lookup(:last_execution_date)
140+
|> case do
141+
[] -> :unknown
142+
[{:last_execution_date, date} | _t] -> date
143+
end, state}
144+
end
145+
146+
defp job_key(job_name) do
147+
{:job, job_name}
171148
end
172149
end

0 commit comments

Comments
 (0)