Skip to content

Commit

Permalink
Terminate all instances of an actor with a count of zero (#310)
Browse files Browse the repository at this point in the history
* terminate all instances of an actor for 0 count

Signed-off-by: Brooks Townsend <[email protected]>

* added test for 0 count terminate

Signed-off-by: Brooks Townsend <[email protected]>
  • Loading branch information
brooksmtownsend authored Dec 7, 2021
1 parent 91f1e26 commit 426250a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 7 deletions.
18 changes: 11 additions & 7 deletions host_core/lib/host_core/actors/actor_supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,18 +201,22 @@ defmodule HostCore.Actors.ActorSupervisor do
end
end

# Terminate `count` instances of an actor
def terminate_actor(public_key, count) when count > 0 do
children =
Registry.lookup(Registry.ActorRegistry, public_key)
|> Enum.take(count)
|> Enum.map(fn {pid, _v} -> pid end)

children |> Enum.each(fn pid -> ActorModule.halt(pid) end)
Registry.lookup(Registry.ActorRegistry, public_key)
|> Enum.take(count)
|> Enum.map(fn {pid, _v} -> pid end)
|> Enum.each(fn pid -> ActorModule.halt(pid) end)

:ok
end

def terminate_actor(_public_key, 0) do
# Terminate all instances of an actor
def terminate_actor(public_key, 0) do
Registry.lookup(Registry.ActorRegistry, public_key)
|> Enum.map(fn {pid, _v} -> pid end)
|> Enum.each(fn pid -> ActorModule.halt(pid) end)

:ok
end

Expand Down
35 changes: 35 additions & 0 deletions host_core/test/host_core/actors_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -310,4 +310,39 @@ defmodule HostCore.ActorsTest do
{:error,
"Cannot start new instance of MBCFOPM6JW2APJLXJD3Z5O4CN7CPYJ2B4FTKLJUR5YR5MITIU7HD3WD5 from OCI 'wasmcloud.azurecr.io/echo:0.3.0', it is already running with different OCI reference. To upgrade an actor, use live update."}
end

test "stop with zero count terminates all", %{:evt_watcher => evt_watcher} do
on_exit(fn -> HostCore.Host.purge() end)
{:ok, bytes} = File.read(@kvcounter_path)
{:ok, _pid} = HostCore.Actors.ActorSupervisor.start_actor(bytes)
{:ok, _pid} = HostCore.Actors.ActorSupervisor.start_actor(bytes)
{:ok, _pid} = HostCore.Actors.ActorSupervisor.start_actor(bytes)
{:ok, _pid} = HostCore.Actors.ActorSupervisor.start_actor(bytes)
{:ok, _pid} = HostCore.Actors.ActorSupervisor.start_actor(bytes)

:ok =
HostCoreTest.EventWatcher.wait_for_event(
evt_watcher,
:actor_started,
%{"public_key" => @kvcounter_key},
5
)

actor_count =
Map.get(HostCore.Actors.ActorSupervisor.all_actors(), @kvcounter_key)
|> length

assert actor_count == 5
HostCore.Actors.ActorSupervisor.terminate_actor(@kvcounter_key, 0)

:ok =
HostCoreTest.EventWatcher.wait_for_event(
evt_watcher,
:actor_stopped,
%{"public_key" => @kvcounter_key},
5
)

assert Map.get(HostCore.Actors.ActorSupervisor.all_actors(), @kvcounter_key) == nil
end
end

0 comments on commit 426250a

Please sign in to comment.