Skip to content

Commit bad643f

Browse files
isaacsandersmaennchen
authored andcommitted
fix: Log errors rather than return them as values
1 parent f81daa2 commit bad643f

File tree

2 files changed

+117
-14
lines changed

2 files changed

+117
-14
lines changed

lib/quantum/executor.ex

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -84,20 +84,25 @@ defmodule Quantum.Executor do
8484
"[#{inspect(Node.self())}][#{__MODULE__}] Execute started for job #{inspect(job_name)}"
8585
end)
8686

87-
result =
88-
try do
89-
execute_task(task)
90-
catch
91-
type, value ->
92-
{type, value}
93-
end
94-
95-
debug_logging &&
96-
Logger.debug(fn ->
97-
"[#{inspect(Node.self())}][#{__MODULE__}] Execution ended for job #{inspect(job_name)}, which yielded result: #{
98-
inspect(result)
99-
}"
100-
end)
87+
try do
88+
execute_task(task)
89+
catch
90+
type, value ->
91+
debug_logging &&
92+
Logger.debug(fn ->
93+
"[#{inspect(Node.self())}][#{__MODULE__}] Execution ended for job #{
94+
inspect(job_name)
95+
}, which failed due to: #{Exception.format(type, value, __STACKTRACE__)}"
96+
end)
97+
else
98+
result ->
99+
debug_logging &&
100+
Logger.debug(fn ->
101+
"[#{inspect(Node.self())}][#{__MODULE__}] Execution ended for job #{
102+
inspect(job_name)
103+
}, which yielded result: #{inspect(result)}"
104+
end)
105+
end
101106

102107
:ok
103108
end)

test/quantum/executor_test.exs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,9 +189,107 @@ defmodule Quantum.ExecutorTest do
189189

190190
assert :marked_running = TaskRegistry.mark_running(task_registry, job.name, Node.self())
191191
end
192+
193+
test "logs error", %{
194+
task_supervisor: task_supervisor,
195+
task_registry: task_registry,
196+
debug_logging: debug_logging
197+
} do
198+
job =
199+
TestScheduler.new_job()
200+
|> Job.set_task(fn -> raise "failed" end)
201+
|> Job.set_overlap(false)
202+
203+
logs =
204+
capture_log(fn ->
205+
{:ok, task} =
206+
Executor.start_link(
207+
%StartOpts{
208+
task_supervisor_reference: task_supervisor,
209+
task_registry_reference: task_registry,
210+
debug_logging: debug_logging
211+
},
212+
%Event{job: job, node: Node.self()}
213+
)
214+
215+
assert :ok == wait_for_termination(task)
216+
end)
217+
218+
assert logs =~ ~r/\(RuntimeError\) failed/
219+
end
220+
221+
test "logs exit", %{
222+
task_supervisor: task_supervisor,
223+
task_registry: task_registry,
224+
debug_logging: debug_logging
225+
} do
226+
job =
227+
TestScheduler.new_job()
228+
|> Job.set_task(fn -> exit(:failure) end)
229+
|> Job.set_overlap(false)
230+
231+
logs =
232+
capture_log(fn ->
233+
{:ok, task} =
234+
Executor.start_link(
235+
%StartOpts{
236+
task_supervisor_reference: task_supervisor,
237+
task_registry_reference: task_registry,
238+
debug_logging: debug_logging
239+
},
240+
%Event{job: job, node: Node.self()}
241+
)
242+
243+
assert :ok == wait_for_termination(task)
244+
end)
245+
246+
assert logs =~ ~r/\(exit\) :failure/
247+
end
248+
249+
test "logs throw", %{
250+
task_supervisor: task_supervisor,
251+
task_registry: task_registry,
252+
debug_logging: debug_logging
253+
} do
254+
ref = make_ref()
255+
256+
job =
257+
TestScheduler.new_job()
258+
|> Job.set_task(fn -> throw(ref) end)
259+
|> Job.set_overlap(false)
260+
261+
logs =
262+
capture_log(fn ->
263+
{:ok, task} =
264+
Executor.start_link(
265+
%StartOpts{
266+
task_supervisor_reference: task_supervisor,
267+
task_registry_reference: task_registry,
268+
debug_logging: debug_logging
269+
},
270+
%Event{job: job, node: Node.self()}
271+
)
272+
273+
assert :ok == wait_for_termination(task)
274+
end)
275+
276+
assert logs =~ "(throw) #{inspect(ref)}"
277+
end
192278
end
193279

194280
def send(caller) do
195281
send(caller, :executed)
196282
end
283+
284+
def wait_for_termination(pid, timeout \\ 5000) do
285+
ref = Process.monitor(pid)
286+
287+
receive do
288+
{:DOWN, ^ref, :process, _pid, _reason} ->
289+
:ok
290+
after
291+
timeout ->
292+
:error
293+
end
294+
end
197295
end

0 commit comments

Comments
 (0)