View Source FeatureSupervisor (FeatureSupervisor v0.1.0)

A small wrapper for Supervisor that dynamically starts/terminates children based on feature flags.

Example

defmodule MyApp.Application do
  use Application

  @mix_env Mix.env()

  def start(_type, _args) do
    children = [
      # a regular child
      Child1,
      # supposed to be disabled in tests
      FeatureSupervisor.child_spec(Child2, enabled?: @mix_env != :test),
      # supposed to run only when the feature is enabled
      FeatureSupervisor.child_spec({Child3, name: Child3},
        enabled?: &feature_enabled?/1,
        feature_id: "my-feature"
      )
    ]

    FeatureSupervisor.start_link(children, strategy: :one_for_one, sync_interval: 1000)
  end

  defp feature_enabled?(spec) do
    MyApp.Features.enabled?(spec.feature_id)
  end
end

Summary

Types

@type child() ::
  Supervisor.child_spec()
  | {module(), term()}
  | module()
  | (old_erlang_child_spec :: :supervisor.child_spec())
@type option() :: {:sync_interval, non_neg_integer()}

Functions

Link to this function

child_spec(child, overrides)

View Source
@spec child_spec(child(), Keyword.t()) :: map()

Same as Supervisor.child_spec/2 but allows any key in the overrides.

Link to this function

start_link(children, options)

View Source
@spec start_link([child()], [
  Supervisor.option() | Supervisor.init_option() | option()
]) ::
  {:ok, pid()}
  | {:error, {:already_started, pid()} | {:shutdown, term()} | term()}

Wraps the Supervisor.start_link/2 function.

Children will be split into two groups:

  • "static" - those without the :enabled? field or with enabled?: true
  • "dynamic" - those with the :enabled? field set to a function

Children with enabled?: false will be excluded.

"static" children will be started as normal.

"dynamic" children (if any) will be started separately via Supervisor.start_child/2.

If a "dynamic" child is expected to be started/terminated later via a feature flag you should provide the sync_interval option. The child's :restart option must be set to :permanent.