Skip to content

Commit

Permalink
Implement handle_child_terminated, bump version to 1.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
FelonEkonom committed Oct 17, 2024
1 parent a9b052d commit 010ef5c
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 6 deletions.
23 changes: 21 additions & 2 deletions lib/membrane/bin.ex
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,20 @@ defmodule Membrane.Bin do
state
) :: callback_return

@doc """
Callback invoked after a child terminates.
Terminated child won't occur in callback context. It is allowed to spawn a new child with the same
name as the one that have just terminated.
By default, it does nothing.
"""
@callback handle_child_terminated(
child :: Child.name(),
context :: CallbackContext.t(),
state
) :: callback_return

@doc """
Callback invoked upon each timer tick. A timer can be started with `t:Membrane.Bin.Action.start_timer/0`
action.
Expand Down Expand Up @@ -248,7 +262,8 @@ defmodule Membrane.Bin do
handle_tick: 3,
handle_crash_group_down: 3,
handle_terminate_request: 2,
handle_child_pad_removed: 4
handle_child_pad_removed: 4,
handle_child_terminated: 3

@doc PadsSpecs.def_pad_docs(:input, :bin)
defmacro def_input_pad(name, spec) do
Expand Down Expand Up @@ -405,6 +420,9 @@ defmodule Membrane.Bin do
@impl true
def handle_terminate_request(_ctx, state), do: {[terminate: :normal], state}

@impl true
def handle_child_terminated(_child, _ctx, state), do: {[], state}

defoverridable handle_init: 2,
handle_pad_added: 3,
handle_pad_removed: 3,
Expand All @@ -418,7 +436,8 @@ defmodule Membrane.Bin do
handle_child_notification: 4,
handle_parent_notification: 3,
handle_crash_group_down: 3,
handle_terminate_request: 2
handle_terminate_request: 2,
handle_child_terminated: 3
end
end

Expand Down
16 changes: 15 additions & 1 deletion lib/membrane/core/parent/child_life_controller.ex
Original file line number Diff line number Diff line change
Expand Up @@ -724,10 +724,14 @@ defmodule Membrane.Core.Parent.ChildLifeController do
CrashGroupUtils.handle_crash_group_member_death(child_name, crash_group, reason, state)
|> ChildrenModel.delete_child(child_name)

state = exec_handle_child_terminated(child_name, state)

{:ok, state}
else
:error when reason == :normal ->
{:ok, ChildrenModel.delete_child(state, child_name)}
state = ChildrenModel.delete_child(state, child_name)
state = exec_handle_child_terminated(child_name, state)
{:ok, state}

:error when reason == {:shutdown, :membrane_crash_group_kill} ->
raise Membrane.PipelineError,
Expand Down Expand Up @@ -771,4 +775,14 @@ defmodule Membrane.Core.Parent.ChildLifeController do
state = %{state | pending_specs: Map.merge(state.pending_specs, related_specs)}
related_specs |> Map.keys() |> Enum.reduce(state, &proceed_spec_startup/2)
end

defp exec_handle_child_terminated(child_name, state) do
CallbackHandler.exec_and_handle_callback(
:handle_child_terminated,
Component.action_handler(state),
%{context: &Component.context_from_state/1},
[child_name],
state
)
end
end
23 changes: 21 additions & 2 deletions lib/membrane/pipeline.ex
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,20 @@ defmodule Membrane.Pipeline do
state
) :: {[Action.common_actions()], state()}

@doc """
Callback invoked after a child terminates.
Terminated child won't occur in callback context. It is allowed to spawn a new child with the same
name as the one that have just terminated.
By default, it does nothing.
"""
@callback handle_child_terminated(
child :: Child.name(),
context :: CallbackContext.t(),
state
) :: callback_return

@doc """
Callback invoked upon each timer tick. A timer can be started with `Membrane.Pipeline.Action.start_timer`
action.
Expand Down Expand Up @@ -291,7 +305,8 @@ defmodule Membrane.Pipeline do
handle_crash_group_down: 3,
handle_call: 3,
handle_terminate_request: 2,
handle_child_pad_removed: 4
handle_child_pad_removed: 4,
handle_child_terminated: 3

@doc """
Starts the pipeline based on the given module and links it to the current process.
Expand Down Expand Up @@ -542,6 +557,9 @@ defmodule Membrane.Pipeline do
@impl true
def handle_child_setup_completed(_child, _ctx, state), do: {[], state}

@impl true
def handle_child_terminated(_child, _ctx, state), do: {[], state}

@impl true
def handle_child_playing(_child, _ctx, state), do: {[], state}

Expand Down Expand Up @@ -575,7 +593,8 @@ defmodule Membrane.Pipeline do
handle_child_notification: 4,
handle_crash_group_down: 3,
handle_call: 3,
handle_terminate_request: 2
handle_terminate_request: 2,
handle_child_terminated: 3
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Membrane.Mixfile do
use Mix.Project

@version "1.1.1"
@version "1.1.2"
@source_ref "v#{@version}"

def project do
Expand Down

0 comments on commit 010ef5c

Please sign in to comment.