Skip to content

Commit 51699df

Browse files
committed
Actors now terminate normally once the choreography is done
1 parent db9f5d6 commit 51699df

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

lib/chorex/runtime.ex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ defmodule Chorex.Runtime do
116116

117117
def handle_continue({:finish_choreography, ret_val}, %RuntimeState{} = state) do
118118
send(state.vars.parent, {:chorex_return, state.actor, ret_val})
119-
{:noreply, state}
119+
{:stop, :normal, nil}
120120
end
121121

122122
defmacro __using__(_args) do

lib/chorex/runtime_monitor.ex

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,25 @@ defmodule Chorex.RuntimeMonitor do
118118
end
119119

120120
@impl true
121+
def handle_info({:DOWN, _down_ref, :process, _pid, :normal}, state) do
122+
# process terminated normally (end of choreography)
123+
124+
ok_to_finish =
125+
state.actors
126+
|> Enum.map(fn {_, {_ref, pid}} -> pid end)
127+
|> Enum.map(&Process.alive?/1)
128+
|> Enum.all?(& not &1)
129+
130+
if ok_to_finish do
131+
{:stop, :normal, nil}
132+
else
133+
{:noreply, state}
134+
end
135+
end
136+
121137
def handle_info({:DOWN, down_ref, :process, _pid, _reason}, state) do
138+
# process crashed
139+
122140
state_ = revive(down_ref, state)
123141
network = get_config_from_state(state_)
124142

lib/chorex/runtime_supervisor.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,20 @@ defmodule Chorex.RuntimeSupervisor do
99

1010
@impl true
1111
def init(_init_arg) do
12-
DynamicSupervisor.init(strategy: :one_for_one)
12+
DynamicSupervisor.init(strategy: :one_for_one)
1313
end
1414

1515
def start_link(session_token) do
16-
DynamicSupervisor.start_link(__MODULE__,
17-
[],
18-
name: {:via, Registry, {@registry_name, session_token}})
16+
DynamicSupervisor.start_link(
17+
__MODULE__,
18+
[],
19+
name: {:via, Registry, {@registry_name, session_token}}
20+
)
1921
end
2022

2123
def start_child(sup_name, mod_name, arg) do
22-
spec = %{id: mod_name, start: {GenServer, :start_link, [mod_name, arg]}}
24+
# restart is temporary; the RuntimeMonitor takes care of restarting crashed processes
25+
spec = %{id: mod_name, start: {GenServer, :start_link, [mod_name, arg]}, restart: :temporary}
2326
DynamicSupervisor.start_child(sup_name, spec)
2427
end
2528
end

test/mini_test.exs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ defmodule MiniTest do
77
def run() do
88
MtAlice.one() ~> MtBob.(x)
99
MtAlice.two() ~> MtBob.(y)
10-
MtBob.(x + y)
10+
MtBob.work(x + y)
1111
end
1212
end
1313
end
@@ -24,10 +24,15 @@ defmodule MiniTest do
2424

2525
defmodule MyMtBob do
2626
use MiniTestChor.Chorex, :mtbob
27+
28+
@impl true
29+
def work(n) do
30+
n
31+
end
2732
end
2833

2934
test "smallest choreography test" do
3035
Chorex.start(MiniTestChor.Chorex, %{MtAlice => MyMtAlice, MtBob => MyMtBob}, [])
31-
assert_receive({:chorex_return, MtBob, 42})
36+
assert_receive({:chorex_return, MtBob, 42}, 500)
3237
end
3338
end

0 commit comments

Comments
 (0)