This repository has been archived by the owner on Jun 6, 2021. It is now read-only.
/
worker.ex
61 lines (52 loc) · 1.93 KB
/
worker.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
defmodule Flect.Worker do
@moduledoc """
Encapsulates a worker process that invokes a Flect tool and collects
its exit code. Can be supervised by an OTP supervisor.
"""
use GenServer.Behaviour
@doc """
Starts a worker process linked to the parent process. Returns `{:ok, pid}`
on success.
"""
@spec start_link() :: {:ok, pid()}
def start_link() do
tup = {:ok, pid} = :gen_server.start_link(__MODULE__, nil, [])
Process.register(pid, :flect_worker)
tup
end
@doc """
Instructs the given worker process to execute a Flect tool as specified
by the given configuration. Returns the exit code of the tool.
`pid` must be the PID of a `Flect.Worker` process. `cfg` must be a valid
`Flect.Config` instance. `timeout` must be `:infinity` or a millisecond
value specifying how much time to wait for the tool to complete.
"""
@spec work(pid(), Flect.Config.t()) :: non_neg_integer()
def work(pid, cfg, timeout // :infinity) do
code = :gen_server.call(pid, {:work, cfg}, timeout)
_ = case :application.get_env(:flect, :flect_event_pid) do
{:ok, pid} -> pid <- {:flect_shutdown, code}
:undefined -> :ok
end
code
end
@doc false
@spec handle_call({:work, Flect.Config.t()}, {pid(), term()}, nil) :: {:reply, non_neg_integer(), nil}
def handle_call({:work, cfg}, _, nil) do
code = try do
case cfg.tool() do
:a -> Flect.Analyzer.Tool.run(cfg)
:c -> Flect.Compiler.Tool.run(cfg)
:d -> Flect.Documentor.Tool.run(cfg)
:f -> Flect.Formatter.Tool.run(cfg)
:i -> Flect.Interactive.Tool.run(cfg)
:p -> Flect.Packager.Tool.run(cfg)
:s -> Flect.Server.Tool.run(cfg)
end
0
catch
code -> code
end
{:reply, code, nil}
end
end