Skip to content

Commit 01b90d0

Browse files
committed
Allow manually providing reporter pid to log funcs
1 parent b90a04e commit 01b90d0

File tree

5 files changed

+33
-11
lines changed

5 files changed

+33
-11
lines changed

lib/chore_runner/dsl.ex

+6-1
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,14 @@ defmodule ChoreRunner.DSL do
3333
import ChoreRunner.Reporter,
3434
only: [
3535
report_failed: 1,
36+
report_failed: 2,
3637
log: 1,
38+
log: 2,
3739
set_counter: 2,
38-
inc_counter: 2
40+
set_counter: 3,
41+
inc_counter: 2,
42+
inc_counter: 3,
43+
get_reporter: 0
3944
]
4045

4146
import ChoreRunner.Input,

lib/chore_runner/reporter.ex

+14-8
Original file line numberDiff line numberDiff line change
@@ -40,22 +40,28 @@ defmodule ChoreRunner.Reporter do
4040
defp report_finished(result),
4141
do: GenServer.cast(get_reporter_pid(), {:chore_finished, DateTime.utc_now(), result})
4242

43-
def report_failed(reason),
44-
do: GenServer.cast(get_reporter_pid(), {:chore_failed, reason, DateTime.utc_now()})
43+
def report_failed(reporter \\ nil, reason),
44+
do:
45+
GenServer.cast(reporter || get_reporter_pid(), {:chore_failed, reason, DateTime.utc_now()})
4546

46-
def log(message),
47-
do: GenServer.cast(get_reporter_pid(), {:log, message, DateTime.utc_now()})
47+
def log(reporter \\ nil, message),
48+
do: GenServer.cast(reporter || get_reporter_pid(), {:log, message, DateTime.utc_now()})
4849

49-
def set_counter(name, value), do: do_update_counter(name, value, :set)
50-
def inc_counter(name, amount), do: do_update_counter(name, amount, :inc)
50+
def set_counter(reporter \\ nil, name, value),
51+
do: do_update_counter(reporter, name, value, :set)
5152

52-
defp do_update_counter(name, value, operation),
53-
do: GenServer.cast(get_reporter_pid(), {:update_counter, name, value, operation})
53+
def inc_counter(reporter \\ nil, name, amount),
54+
do: do_update_counter(reporter, name, amount, :inc)
55+
56+
defp do_update_counter(reporter, name, value, operation),
57+
do: GenServer.cast(reporter || get_reporter_pid(), {:update_counter, name, value, operation})
5458

5559
def get_chore_state(%Chore{} = chore) do
5660
GenServer.call(name(chore), :chore_state)
5761
end
5862

63+
def get_reporter, do: get_reporter_pid()
64+
5965
def handle_call(:chore_state, _from, %{chore: chore} = state), do: {:reply, chore, state}
6066

6167
def handle_call(

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ defmodule ChoreRunner.MixProject do
66
def project do
77
[
88
app: :chore_runner,
9-
version: "0.2.1",
9+
version: "0.2.2",
1010
elixir: "~> 1.12",
1111
start_permanent: Mix.env() == :prod,
1212
deps: deps(),

test/chore_test.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ defmodule ChoreTest do
4444
sleep?: false,
4545
sleep_length: 0
4646
},
47-
logs: [],
47+
logs: [_, _],
4848
mod: ChoreRunner.TestChore,
4949
result:
5050
{:ok,

test/support/test_chore.ex

+11
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ defmodule ChoreRunner.TestChore do
2121
} = attrs
2222
) do
2323
if sleep?, do: Process.sleep(sleep_length)
24+
reporter = get_reporter()
25+
log("test")
26+
set_counter(:test, 5)
27+
inc_counter(:test, 1)
28+
29+
spawn(fn ->
30+
log(reporter, "test from process")
31+
set_counter(reporter, :process_test, 10)
32+
inc_counter(reporter, :process_test, 2)
33+
end)
34+
2435
{:ok, Map.put(attrs, :my_file_value, File.read!(my_file))}
2536
end
2637
end

0 commit comments

Comments
 (0)