-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathjob_worker.ex
More file actions
66 lines (55 loc) · 1.18 KB
/
Copy pathjob_worker.ex
File metadata and controls
66 lines (55 loc) · 1.18 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
60
61
62
63
64
65
66
defmodule Survey.JobWorker do
use GenServer
alias Survey.Job
require Logger
@delay_proc 120 * 1000
@delay_clean 300 * 1000
def start_link do
{:ok, pid} = GenServer.start_link(__MODULE__, [], name: :job_worker)
clean
work
{:ok, pid}
end
def work do
GenServer.cast(:job_worker, :work)
end
def clean do
GenServer.cast(:job_worker, :clean)
end
#----------------------------------------
def init([]) do
clean
work
{:ok, []}
end
def handle_cast(:work, []) do
job = Job.checkout(self)
if job do
{m, f, a} = job.mfa
case apply(m, f, a) do
:ok -> Job.completed(job.id)
{:ok, _} -> Job.completed(job.id)
x ->
Logger.warn("Job #{job.id}: #{inspect(x)}")
Job.failed(job.id)
end
work
else
:erlang.send_after(@delay_proc, self, :timer)
end
{:noreply, []}
end
def handle_cast(:clean, []) do
send self, :clean
{:noreply, []}
end
def handle_info(:timer, []) do
work
{:noreply, []}
end
def handle_info(:clean, []) do
Job.clean
:erlang.send_after(@delay_clean, self, :clean)
{:noreply, []}
end
end