Skip to content

Commit

Permalink
Include commanded#431 in CHANGELOG
Browse files Browse the repository at this point in the history
  • Loading branch information
slashdotdash committed Jan 31, 2021
1 parent 7f3ab04 commit b1fd121
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
- Telemetry `[:commanded, :aggregate, :execute]` events ([#407](https://github.com/commanded/commanded/pull/407)).
- Telemetry `[:commanded, :event, :handle]` events ([#408](https://github.com/commanded/commanded/pull/408)).
- Telemetry `[:commanded, :process_manager, :handle]` events ([#418](https://github.com/commanded/commanded/pull/418)).
- Telemetry `[:commanded, :application, :dispatch]` ([#423 ](https://github.com/commanded/commanded/pull/423)).
- Telemetry `[:commanded, :application, :dispatch]` ([#423](https://github.com/commanded/commanded/pull/423)).
- Graceful shutdown of event handlers ([#431](https://github.com/commanded/commanded/pull/431)).

## v1.2.0

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ Commanded exists thanks to the following people who have contributed.
- [Chris Brodt](https://github.com/uberbrodt)
- [Chris Martin](https://github.com/trbngr)
- [Christophe Juniet](https://github.com/cjuniet)
- [Danilo Silva](https://github.com/silvadanilo)
- [Dave Lucia](https://github.com/davydog187)
- [David Carlin](https://github.com/davich)
- [Danni Friedland](https://github.com/BlueHotDog)
Expand Down
1 change: 1 addition & 0 deletions lib/commanded/event/handler.ex
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,7 @@ defmodule Commanded.Event.Handler do
@impl GenServer
def init(%Handler{} = state) do
Process.flag(:trap_exit, true)

{:ok, state, {:continue, :subscribe_to_events}}
end

Expand Down
4 changes: 2 additions & 2 deletions test/commands/dispatch_consistency_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,8 @@ defmodule Commanded.Commands.DispatchConsistencyTest do
end

def start_event_handlers(_context) do
start_supervised!(StronglyConsistentEventHandler)
start_supervised!(EventuallyConsistentEventHandler)
start_supervised!(StronglyConsistentEventHandler, shutdown: :brutal_kill)
start_supervised!(EventuallyConsistentEventHandler, shutdown: :brutal_kill)

:ok
end
Expand Down
24 changes: 17 additions & 7 deletions test/event/event_handler_graceful_shutdown_test.exs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
defmodule Commanded.Event.EventHandlerGracefulShutdownTest do
use ExUnit.Case

alias Commanded.Event.GracefulShutdownHandler
alias Commanded.DefaultApp
alias Commanded.Event.GracefulShutdownHandler
alias Commanded.Helpers.EventFactory

defmodule AnEvent do
@derive Jason.Encoder
defstruct [:reply_to, :sleep_for]
defstruct [:reply_to]
end

defmodule EventHandlersSupervisor do
Expand Down Expand Up @@ -39,14 +39,24 @@ defmodule Commanded.Event.EventHandlerGracefulShutdownTest do

test "stop the event handler while it is handling an event" do
supervisor = start_supervised!(EventHandlersSupervisor, restart: :transient)
{_, handler, _, _} = supervisor |> Supervisor.which_children() |> List.first()
[{_, handler, _, _}] = Supervisor.which_children(supervisor)

event = %AnEvent{reply_to: self(), sleep_for: 200}
event = %AnEvent{reply_to: self()}
send_events_to_handler(handler, [event])
Supervisor.stop(supervisor, {:shutdown, :graceful})

assert Process.whereis(EventHandlersSupervisor) == nil
assert_received {:event, ^event, _metadata}
Task.async(fn ->
Supervisor.stop(supervisor, :shutdown)
end)

assert_receive {:event, ^event, _metadata}
refute_receive {:continue, ^event, _metadata}

ref = Process.monitor(handler)

send(handler, :continue)

assert_receive {:continue, ^event, _metadata}
assert_receive {:DOWN, ^ref, :process, ^handler, :shutdown}
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ defmodule Commanded.Event.GracefulShutdownHandler do
application: Commanded.DefaultApp,
name: __MODULE__

def handle(%{sleep_for: milliseconds} = event, metadata) do
def handle(event, metadata) do
%{reply_to: reply_to} = event

Process.sleep(milliseconds)

send(reply_to, {:event, event, metadata})

receive do
:continue ->
send(reply_to, {:continue, event, metadata})

:ok
end

:ok
end
end

0 comments on commit b1fd121

Please sign in to comment.