glixir/supervisor

Type-safe, bounded DynamicSupervisor interop for Gleam/Elixir

This module provides a phantom-typed interface to Elixir DynamicSupervisor. All child processes are parameterized by their args and reply types, so misuse is a compile error, not a runtime surprise.

Types

pub type ChildCounts {
  ChildCounts(
    specs: Int,
    active: Int,
    supervisors: Int,
    workers: Int,
  )
}

Constructors

  • ChildCounts(
      specs: Int,
      active: Int,
      supervisors: Int,
      workers: Int,
    )

Supervisor child info (partial for demo—expand as needed)

pub type ChildInfo(child_args, child_reply) {
  ChildInfo(
    id: String,
    pid: option.Option(process.Pid),
    child_type: ChildType,
    status: ChildStatus,
    args: child_args,
    reply: child_reply,
  )
}

Constructors

Opaque error type for child operations (for completeness)

pub type ChildOperationError {
  Running
  Restarting
  NotFound
  SimpleOneForOne
  OtherError(String)
}

Constructors

  • Running
  • Restarting
  • NotFound
  • SimpleOneForOne
  • OtherError(String)

Strongly-typed child specification

pub type ChildSpec(child_args, child_reply) {
  ChildSpec(
    id: String,
    start_module: atom.Atom,
    start_function: atom.Atom,
    start_args: child_args,
    restart: RestartStrategy,
    shutdown_timeout: Int,
    child_type: ChildType,
  )
}

Constructors

Child status for introspection

pub type ChildStatus {
  ChildPid(process.Pid)
  ChildRestarting
  ChildUndefined
}

Constructors

Type of child process

pub type ChildType {
  Worker
  SupervisorChild
}

Constructors

  • Worker
  • SupervisorChild
pub type DynamicChildResult {
  DynamicStartChildOk(pid: process.Pid)
  DynamicStartChildError(reason: dynamic.Dynamic)
}

Constructors

Opaque type representing a supervisor process

pub opaque type DynamicSupervisor(child_args, child_reply)
pub type DynamicSupervisorResult {
  DynamicSupervisorOk(pid: process.Pid)
  DynamicSupervisorError(reason: dynamic.Dynamic)
}

Constructors

pub type DynamicTerminateResult {
  DynamicTerminateChildOk
  DynamicTerminateChildError(reason: dynamic.Dynamic)
}

Constructors

  • DynamicTerminateChildOk
  • DynamicTerminateChildError(reason: dynamic.Dynamic)

Child restart strategy

pub type RestartStrategy {
  Permanent
  Temporary
  Transient
}

Constructors

  • Permanent
  • Temporary
  • Transient

Strongly-typed result when starting a child

pub type StartChildResult(child_reply) {
  ChildStarted(process.Pid, child_reply)
  StartChildError(String)
}

Constructors

  • ChildStarted(process.Pid, child_reply)
  • StartChildError(String)

Supervisor errors

pub type SupervisorError {
  StartError(String)
  ChildStartError(String, String)
  ChildNotFound(String)
  AlreadyStarted(String)
  InvalidChildSpec(String)
}

Constructors

  • StartError(String)
  • ChildStartError(String, String)
  • ChildNotFound(String)
  • AlreadyStarted(String)
  • InvalidChildSpec(String)

Values

pub fn child_spec(
  id id: String,
  module module: String,
  function function: String,
  args args: child_args,
  restart restart: RestartStrategy,
  shutdown_timeout shutdown_timeout: Int,
  child_type child_type: ChildType,
  encode encode: fn(child_args) -> List(dynamic.Dynamic),
) -> ChildSpec(child_args, child_reply)

Build a type-safe child spec

pub fn count_dynamic_children(
  supervisor: DynamicSupervisor(child_args, child_reply),
) -> Result(ChildCounts, String)
pub fn encode_child_args(child_args: a) -> List(dynamic.Dynamic)
pub fn start_dynamic_child(
  supervisor: DynamicSupervisor(child_args, child_reply),
  spec: ChildSpec(child_args, child_reply),
  encode: fn(child_args) -> List(dynamic.Dynamic),
  decode: fn(dynamic.Dynamic) -> Result(child_reply, String),
) -> StartChildResult(child_reply)

Start a child with typed args and replies

pub fn start_dynamic_supervisor_named(
  name: atom.Atom,
) -> Result(
  DynamicSupervisor(child_args, child_reply),
  SupervisorError,
)

Start a named dynamic supervisor with bounded child types

pub fn terminate_dynamic_child(
  supervisor: DynamicSupervisor(child_args, child_reply),
  child_pid: process.Pid,
) -> Result(Nil, String)

Terminate a child (by pid)

pub fn which_dynamic_children(
  supervisor: DynamicSupervisor(child_args, child_reply),
) -> List(dynamic.Dynamic)

Returns all dynamic children as a list of Dynamic. You might want to write a real decoder for full type-safety!

Search Document