Oi.Runtime.Session (oi v0.7.0)

Copy Markdown

Session separates the whole application into several spaces where contains different steps, independent symbionts and storages.

Summary

Functions

Returns the per-session storage map if orchid_stratum is available.

Ensures a session is started and calls fun with session-scoped opts.

Functions

ensure_started(oi_name, opts \\ [])

@spec ensure_started(
  binary(),
  keyword()
) :: :ignore | {:error, any()} | {:ok, pid()} | {:ok, pid(), any()}

instances(oi)

@spec instances(Oi.name()) :: Oi.Runtime.Registry.key()

instances_tuple(oi)

@spec instances_tuple(Oi.name()) :: Oi.Runtime.Registry.via_tuple()

resolve(oi_name)

@spec resolve(Oi.name()) :: {:error, :session_not_found} | {:ok, pid()}

start(oi_name, opts \\ [])

@spec start(
  Oi.name(),
  keyword()
) :: :ignore | {:error, any()} | {:ok, pid()} | {:ok, pid(), any()}

stop(oi_name)

@spec stop(Oi.name()) :: :ok | {:error, :session_not_found}

storage(oi_name)

@spec storage(Oi.name()) :: map() | nil

Returns the per-session storage map if orchid_stratum is available.

The map can be merged into orchid_baggage:

%{meta_store: {EtsAdapter, ref}, blob_store: {EtsAdapter, ref}}

Returns nil when orchid_stratum is not loaded or the session was started without storage.

tasks_tuple(oi)

@spec tasks_tuple(Oi.name()) :: Oi.Runtime.Registry.via_tuple()

with_session(oi_name, opts \\ [], fun)

@spec with_session(Oi.name(), keyword(), (keyword() -> result)) :: result
when result: term()

Ensures a session is started and calls fun with session-scoped opts.

fun receives a keyword list suitable for merging into Oi.run/2 or Oi.execute/2:

  • :executorOi.Executor.TaskSup
  • :executor_opts[sup: Session.tasks_tuple(name)]
  • :orchid_baggage%{scope_id: name}
  • :name — the session name

The session is not stopped when fun returns — sessions are designed to be long-lived. Call stop/1 when done.

Example

Session.with_session("tenant-1", fn session ->
  Oi.run(graph, Keyword.merge(session, data: %{greeter: %{name: "Alice"}}))
end)