Octoroom.Agent.Pipeline.Core (octoroom_agents v0.1.0)

Copy Markdown View Source

The pure part of an agent's pipeline.

The core holds the processors, delivers frames between them, and executes their effects. It starts no processes and sends no messages. Each call returns a list of commands for its host to carry out, and the host feeds task results, timer expiries and messages back in. Hosts are the room worker (Octoroom.Agent.Worker) and the test harness (Octoroom.Agent.Pipeline.Test).

Commands:

  • {:output, frame} - a frame left the last stage downstream
  • {:output_upstream, frame} - a frame left the first stage upstream (an Error frame there should end the call)
  • {:start_task, id, fun} - run fun.(emit); report each emitted message and then {:done, result} (or {:failed, reason}) with async_message/3
  • {:kill_task, id} - kill that task; anything it still reports is dropped
  • {:start_timer, id, ms} / {:cancel_timer, id} - call timer_fired/2 after ms
  • {:notify, event} - send event to the session owner
  • {:interrupted, generation} - an interruption happened; reset playback
  • {:observe, frame} - a frame of an observed type was pushed (see observe/2)

An Octoroom.Agent.Frames.UpdateStage frame goes to the handle_update/2 of the stage it names, not its handle_frame/3, and goes no further. A stage without handle_update/2 reports an error instead. One that names no stage here leaves the pipeline like any other frame.

Task and timer ids are opaque. The core drops reports for ids it no longer knows, so the host doesn't have to track generations.

Summary

Functions

Reports a message from a task started with {:start_task, id, fun}.

Delivers a message to handle_info/2.

Validates the spec and initializes every processor. ctx is merged into each processor's init/2 context; pass runner: pid to give processors a :reply_to for Octoroom.Agent.Processor.send_to/2. ctx[:telemetry] is added to telemetry metadata (the runner puts the session id there).

Emits {:observe, frame} whenever a frame of one of modules is pushed.

Pushes a frame into the first stage (downstream) or the last (upstream).

Sets a key in the telemetry metadata carried on every event emitted by this core.

The index of stage name, or nil.

The stage names, in order.

The current state of stage name (for tests and debugging).

Calls every processor's terminate/2 and returns the cleanup commands.

Reports that a timer started with {:start_timer, id, ms} fired.

Types

command()

@type command() ::
  {:output, struct()}
  | {:output_upstream, struct()}
  | {:start_task, term(), function()}
  | {:kill_task, term()}
  | {:start_timer, term(), non_neg_integer()}
  | {:cancel_timer, term()}
  | {:notify, term()}
  | {:interrupted, non_neg_integer()}
  | {:observe, struct()}

t()

@type t() :: %Octoroom.Agent.Pipeline.Core{
  commands: term(),
  current: term(),
  generation: term(),
  index: term(),
  interrupting?: term(),
  observed: term(),
  queue: term(),
  seq: term(),
  stages: term(),
  system: term(),
  tasks: term(),
  telemetry: term(),
  timers: term()
}

Functions

async_message(core, id, message)

@spec async_message(t(), term(), term()) :: {[command()], t()}

Reports a message from a task started with {:start_task, id, fun}.

info(core, name, message)

@spec info(t(), atom() | [atom()] | :broadcast, term()) :: {[command()], t()}

Delivers a message to handle_info/2.

target is a stage name, a path of stage names into composite stages (as in ctx.reply_to), or :broadcast for every stage. Broadcast is for messages from processes that can't address one stage, such as STT sockets started with the runner as their parent.

new(spec, ctx \\ %{})

@spec new(Octoroom.Agent.Pipeline.Spec.t(), map()) :: {:ok, t()} | {:error, term()}

Validates the spec and initializes every processor. ctx is merged into each processor's init/2 context; pass runner: pid to give processors a :reply_to for Octoroom.Agent.Processor.send_to/2. ctx[:telemetry] is added to telemetry metadata (the runner puts the session id there).

observe(core, modules)

@spec observe(t(), Enumerable.t()) :: t()

Emits {:observe, frame} whenever a frame of one of modules is pushed.

push(core, frame, direction \\ :downstream)

@spec push(t(), struct(), :downstream | :upstream) :: {[command()], t()}

Pushes a frame into the first stage (downstream) or the last (upstream).

put_telemetry(core, key, value)

@spec put_telemetry(t(), atom(), term()) :: t()

Sets a key in the telemetry metadata carried on every event emitted by this core.

stage_index(core, name)

The index of stage name, or nil.

stage_names(core)

The stage names, in order.

stage_state(core, name)

The current state of stage name (for tests and debugging).

terminate(core, reason)

@spec terminate(t(), term()) :: [command()]

Calls every processor's terminate/2 and returns the cleanup commands.

timer_fired(core, id)

@spec timer_fired(t(), term()) :: {[command()], t()}

Reports that a timer started with {:start_timer, id, ms} fired.