A small wrapper for Supervisor that dynamically starts/terminates children based on feature flags.
The package can be installed by adding feature_supervisor to your list of dependencies in mix.exs:
defdepsdo[{:feature_supervisor,"~> 0.1.0"}# {x-release-please-version}]endFeatureSupervisor should be used the same way you would use Supervisor.
defmoduleMyApp.ApplicationdouseApplication@mix_envMix.env()defstart(_type,_args)dochildren=[# a regular childChild1,# supposed to be disabled in testsFeatureSupervisor.child_spec(Child2,enabled?: @mix_env!=:test),# supposed to run only when the feature is enabledFeatureSupervisor.child_spec({Child3,name: Child3},enabled?: &feature_enabled?/1,feature_id: "my-feature")]FeatureSupervisor.start_link(children,strategy: :one_for_one,sync_interval: 1000)enddefpfeature_enabled?(spec)doMyApp.Features.enabled?(spec.feature_id)endend