PhoenixFlags.Server (PhoenixFlags v0.11.0)

Copy Markdown View Source

The process that owns an instance's cache, and the API a config module delegates to.

Each use PhoenixFlags module starts its own Server, named after the module. Reads go straight to :persistent_term and never call it; cached writes go through it, so a write and a cache reload cannot interleave.

Summary

Functions

Returns all config entries grouped by category.

Returns audit log entries, newest first: every flag's, or one key's.

Returns a specification to start this module under a supervisor.

Returns the config for the given instance. Used by the UI to resolve actor.

Deletes a targeting rule by id. Its conditions go with it.

Returns the cached value for a config key.

Deletes stored flags the config module no longer declares, with their targeting rules, and returns the deleted keys.

Adds a targeting rule to a flag.

Returns the targeting rules for a flag, in evaluation order.

Updates a config entry and refreshes the cache.

Returns the variant assigned to identity for a :variant flag.

Functions

all_grouped(instance)

Returns all config entries grouped by category.

audit_log(instance, key_or_opts \\ [])

Returns audit log entries, newest first: every flag's, or one key's.

audit_log(MyApp.SystemConfig)
audit_log(MyApp.SystemConfig, "enable_benefits")
audit_log(MyApp.SystemConfig, limit: 50)
audit_log(MyApp.SystemConfig, "enable_benefits", limit: 50)

Raises PhoenixFlags.Error unless the instance is configured with audit: true.

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

config(instance)

Returns the config for the given instance. Used by the UI to resolve actor.

delete_target(instance, target_id, opts \\ [])

Deletes a targeting rule by id. Its conditions go with it.

Returns {:error, :not_found} for an unknown id and {:error, :unavailable} when the database cannot be reached. Pass actor: in opts for the audit log.

get(instance, key, default \\ nil, opts \\ [])

Returns the cached value for a config key.

Targeting rules are consulted first: if one matches the request context, its value wins over the stored value. See PhoenixFlags.Target.

When cache_enabled: false, checks the process dictionary first (for test overrides), then targeting rules, then falls back to a direct DB read.

Options

  • :context — attributes for targeting rules, overriding the process-scoped PhoenixFlags.Context. Given neither, targeting is skipped entirely.

Raises for a :variant flag: it holds a split rather than a single value, and returning the struct would leak it into application code. Use variant/4.

prune_undeclared(instance)

Deletes stored flags the config module no longer declares, with their targeting rules, and returns the deleted keys.

Startup never deletes a row: during a rolling deploy the old and new releases disagree about which flags exist. Run this once every node runs code that no longer declares the flags.

put_target(instance, key, attrs, opts \\ [])

Adds a targeting rule to a flag.

attrs takes :value and :conditions, and optionally :position:

put_target(MyApp.SystemConfig, "enable_benefits",
  conditions: [[attribute: :company_id, operator: :in, values: [123]]],
  value: "true"
)

The value must be valid for the flag's type, checked with the same rules a dashboard save goes through. :secret flags cannot be targeted — a rule value would sit in the targets table as plaintext, defeating the encryptor.

Pass actor: in opts to name who added it in the audit log.

targets(instance, key)

Returns the targeting rules for a flag, in evaluation order.

update_entry(instance, key, attrs, opts \\ [])

Updates a config entry and refreshes the cache.

When cache_enabled: false, runs in the caller's process for Ecto sandbox compatibility.

Accepts an optional :timeout (in milliseconds, default 5000). A write that takes longer returns {:error, :timeout} rather than exiting, and may still land — read the value back to learn which.

variant(instance, key, identity, opts \\ [])

Returns the variant assigned to identity for a :variant flag.

Deterministic for a given identity, split and seed — see PhoenixFlags.Variant. Cached, it is a :persistent_term read, a hash and a short list walk; the split is parsed once when the cache loads.

Options

  • :default — returned when the flag does not exist, is not a :variant flag, the instance is not running, or the identity is unusable. Defaults to nil.
  • :telemetry — when true, emits [:phoenix_flags, :variant, :assigned] with %{flag: key, identity: identity, variant: variant, instance: instance} as metadata, so exposures can be piped into your own analytics. Off by default to keep the read path free.
  • :context — attributes for targeting rules, overriding the process-scoped context. A matching rule pins the caller to its variant, ahead of the split.
  • :now — current time in milliseconds, for testing TTL rollover.