Announcing a tiny library called Conditional Child, that allows you to define directly in your supervision tree a condition for when a process should run. The condition is checked periodically, so it allows you to toggle processes in runtime.
A common use case is to manage processes using feature flags, but any condition can be used.
Example:
defmodule Demo.Application do
@moduledoc false
use Application
@impl true
def start(_type, _args) do
children = [
{ConditionalChild, child: Demo.Worker, start_if: fn -> your_condition() end}
]
opts = [strategy: :one_for_one, name: Demo.Supervisor]
Supervisor.start_link(children, opts)
end
end
Links: