-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ex
More file actions
170 lines (140 loc) · 3.95 KB
/
agent.ex
File metadata and controls
170 lines (140 loc) · 3.95 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
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
defmodule Vector.Agent do
@moduledoc false
use GenServer
alias Vector.Consumer.Logger
defstruct [:config, :pid, :os_pid]
################################
# Public API
################################
@doc false
@spec start_link(Vector.Config.t()) :: GenServer.on_start()
def start_link(%Vector.Config{} = config) do
GenServer.start_link(__MODULE__, config)
end
@doc false
@spec stop(GenServer.server()) :: :ok
def stop(agent) do
GenServer.stop(agent)
end
@doc false
@spec send(GenServer.server(), data :: iodata()) :: :ok
def send(agent, data) do
data = to_string(data)
GenServer.call(agent, {:send, data})
end
################################
# GenServer Callbacks
################################
@impl GenServer
def init(config) do
Process.flag(:trap_exit, true)
if config.start_async do
{:ok, config, {:continue, :start}}
else
agent = do_start(config)
{:ok, agent}
end
end
@impl GenServer
def handle_continue(:start, config) do
agent = do_start(config)
{:noreply, agent}
end
@impl GenServer
def handle_call({:send, data}, _from, agent) do
:ok = :exec.send(agent.os_pid, data)
{:reply, :ok, agent}
end
@impl GenServer
def handle_info({:stdout, _, stdout}, agent) do
handle_data(agent, :stdout, stdout)
{:noreply, agent}
end
def handle_info({:stderr, _, stderr}, agent) do
handle_data(agent, :stderr, stderr)
{:noreply, agent}
end
def handle_info({:EXIT, _, {:exit_status, _} = status}, agent) do
{:stop, {[:vector, :error], status}, agent}
end
def handle_info({:EXIT, _, :normal}, agent) do
{:stop, :normal, agent}
end
def handle_info(:shutdown, agent) do
{:stop, :normal, agent}
end
@impl GenServer
def terminate({[:vector, :error], {:exit_status, status}}, agent) do
:ok = Logger.log(agent, :error, "vector: Vector is exiting with error status #{status}.")
agent
end
def terminate(message, agent) when message in [:normal, :shutdown] do
:ok = Logger.log(agent, :info, "vector: Vector is stopping.")
:exec.stop(agent.os_pid)
do_confirm_exit(agent)
do_flush_messages(agent)
:ok = Logger.log(agent, :info, "vector: Vector has stopped.")
agent
end
################################
# Private API
################################
defp do_start(config) do
options = build_options(config)
command = Vector.start_command(config)
{:ok, pid, os_pid} = :exec.run_link(command, options)
agent = %__MODULE__{config: config, pid: pid, os_pid: os_pid}
:ok = Logger.log(agent, :info, "vector: Vector is starting.")
maybe_schedule_shutdown(agent)
agent
end
defp build_options(config) do
[:stdin]
|> with_option(config, :stdout)
|> with_option(config, :stderr)
end
defp with_option(options, config, option) when option in [:stderr, :stdout] do
case Map.get(config, option) do
{_, _} -> options ++ [option]
_ -> options
end
end
defp handle_data(agent, type, data) do
{consumer, opts} = Map.get(agent.config, type)
consumer.handle_data(agent, type, data, opts)
end
defp maybe_schedule_shutdown(agent) do
if is_integer(agent.config.shutdown_ms) do
:ok =
Logger.log(
agent,
:info,
"vector: Vector shutdown scheduled in #{agent.config.shutdown_ms}ms."
)
Process.send_after(self(), :shutdown, agent.config.shutdown_ms)
end
end
defp do_confirm_exit(agent) do
case System.cmd("kill", ["-0", to_string(agent.os_pid)], stderr_to_stdout: true) do
{_, 0} ->
:timer.sleep(100)
do_confirm_exit(agent)
{_, 1} ->
:ok
end
end
defp do_flush_messages(agent) do
receive do
msg ->
handle_info(msg, agent)
do_flush_messages(agent)
after
0 -> :ok
end
end
defimpl Inspect do
def inspect(%{os_pid: os_pid}, _) do
"#Vector.Agent<pid: #{inspect(os_pid)}>"
end
end
end