-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathjob_queue_server.ex
More file actions
59 lines (46 loc) · 1.27 KB
/
Copy pathjob_queue_server.ex
File metadata and controls
59 lines (46 loc) · 1.27 KB
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
defmodule Csv2sql.JobQueueServer do
use GenServer
def start_link(_) do
GenServer.start_link(__MODULE__, :no_args, name: __MODULE__)
end
def init(_) do
{:ok, []}
end
def add_data_chunk(file, data_chunk) do
GenServer.cast(__MODULE__, {:add_new_data_chunk, file, data_chunk})
end
def get_work() do
GenServer.call(__MODULE__, :get_work, :infinity)
end
def get_job_count() do
GenServer.call(__MODULE__, :get_job_count, :infinity)
end
def job_for_file_present(file) do
GenServer.call(__MODULE__, {:job_for_file_present, file}, :infinity)
end
def handle_cast({:add_new_data_chunk, file, data_chunk}, state) do
new_state = state ++ [{file, data_chunk}]
{:noreply, new_state}
end
def handle_call(:get_work, _from, state) do
state
|> case do
[data | new_state] ->
{:reply, data, new_state}
[] ->
{:reply, :no_work, state}
nil ->
{:reply, :no_work, state}
end
end
def handle_call(:get_job_count, _from, state) do
{:reply, Enum.count(state), state}
end
def handle_call({:job_for_file_present, file}, _from, state) do
file_present =
Enum.any?(state, fn {file_job, _data_chunk} ->
file == file_job
end)
{:reply, file_present, state}
end
end