Skip to main content

Photon

Struct Photon 

Source
pub struct Photon { /* private fields */ }
Expand description

Main Photon runtime handle.

Keep this value alive for the lifetime of the process that publishes or runs handlers. Build it once with Photon::builder, pass it to publish_on / subscribe_on, and call start_executor on Mode 1 hosts and Mode 2 worker binaries.

RoleWhat to call
Publisher (Mode 2)publish_on(&photon) — usually no executor
Worker (Mode 2)start_executor + #[subscribe]
Embedded (Mode 1)both publish and start_executor

Getting started: Mode 1, Mode 2.

§Example

use std::sync::Arc;

use photon_core::JsonIdentityFactory;
use photon_runtime::Photon;

let photon = Photon::builder().auto_registry().build()?;
photon.start_executor(Arc::new(JsonIdentityFactory))?;

Implementations§

Source§

impl Photon

Source

pub fn builder() -> PhotonBuilder

Start building a Photon runtime instance.

See crate::builder::PhotonBuilder for Mode 1 / Mode 2 wiring.

Source

pub fn backend_label(&self) -> &'static str

Telemetry label for the installed backend.

Source

pub async fn admin_snapshot(&self) -> Result<AdminSnapshot>

Compose a read-only ops introspection snapshot for host admin UIs.

Aggregates the topic catalog, handler inventory, backend capabilities, and checkpoint cursors for inventory-registered handlers. Does not touch publish/subscribe hot paths.

§Errors

Returns an error if a checkpoint load fails.

Source

pub async fn publish( &self, topic_name: &str, topic_key: Option<&str>, actor_json: Value, payload_json: Value, ) -> Result<String>

Publish a single event to a topic by name (low-level).

Prefer the typed API generated by topic: EventType { … }.publish_on(&photon).await.

§Example
// After #[topic(name = "orders.created")] on OrderCreated:
OrderCreated {
    order_id: "ord-1".into(),
    amount_cents: 9900,
}
.publish_on(&photon)
.await?;
§Errors

Returns an error if the storage adapter rejects the append.

Source

pub fn subscribe( &self, topic_name: &str, topic_key_filter: Option<&str>, after_seq: Option<i64>, ) -> Pin<Box<dyn Stream<Item = Result<Event>> + Send>>

Subscribe to topic events as a raw JSON stream (low-level).

Prefer the typed API from topic: EventType::subscribe_on(&photon, opts), or inventory handlers via #[subscribe] + start_executor.

Runnable typed stream: cargo run -p uf-photon --example keyed_topic --features runtime,mem. Runnable raw stream: cargo run -p uf-photon --example manual_subscribe --features runtime,mem.

§Example (typed — preferred)
use futures::StreamExt;
use photon::{SubscribeOpts, topic};

#[topic(name = "orders.created")]
struct OrderCreated { order_id: String }

let mut stream = OrderCreated::subscribe_on(
    photon,
    SubscribeOpts::default_ephemeral(),
)
.await?;
if let Some(Ok(envelope)) = stream.next().await {
    let _ = envelope.payload.order_id;
}
Source

pub fn subscribe_consumer_group( &self, topic_name: &str, shard_ids: &[u32], after_seq_by_shard: HashMap<u32, Option<i64>>, ) -> Pin<Box<dyn Stream<Item = Result<Event>> + Send>>

Subscribe to assigned virtual shards for a consumer group (multiplexed stream).

Source

pub async fn get_event(&self, event_id: &str) -> Result<Option<Event>>

Load a specific event by ID.

§Errors

Returns an error if the operation fails.

Source

pub fn registry(&self) -> &TopicRegistry

Return the registered topic catalog.

Source

pub async fn get_checkpoint_seq( &self, subscription_name: &str, topic_name: &str, topic_key: Option<&str>, ) -> Result<Option<i64>>

Read the last checkpoint sequence for a subscription/topic pair.

§Errors

Returns an error if the operation fails.

Source

pub async fn set_checkpoint( &self, subscription_name: &str, topic_name: &str, topic_key: Option<&str>, last_seq: i64, ) -> Result<()>

Persist an updated checkpoint sequence for a subscription/topic pair.

§Errors

Returns an error if the operation fails.

Source

pub const fn runtime(&self) -> &PhotonRuntimeState

Shared tailer / executor services.

Source

pub async fn reclaim_transport(&self) -> Result<Vec<ReclaimReport>>

Reclaim transport log rows past the safe watermark (ops / retention entry point).

Call periodically (or from a headless ops job) after durable subscribers have advanced checkpoints. Retention knobs: crate config (PHOTON_TRANSPORT_* / builder retention_policy).

§Errors

Returns an error if a storage reclaim operation fails.

Source

pub fn start_executor(&self, identity: Arc<dyn IdentityFactory>) -> Result<()>

Start inventory-registered #[photon::subscribe] handlers.

Required on Mode 1 hosts and Mode 2 worker binaries. Publisher-only Mode 2 processes typically skip this. Requires an IdentityFactory (e.g. photon_core::JsonIdentityFactory for examples/tests) for actor reconstruction.

See Getting started → Mode 2.

§Example
use std::sync::Arc;

use photon_core::JsonIdentityFactory;
use photon_runtime::Photon;

let photon = Photon::builder().auto_registry().build()?;
photon.start_executor(Arc::new(JsonIdentityFactory))?;
photon.shutdown_executor();
photon.join_executor().await;
§Errors

Returns an error if the executor was already started on this runtime.

Source

pub fn shutdown_executor(&self)

Signal handler loops to stop accepting new events.

§Contract

Idempotent. Pair with Self::join_executor to await in-flight work.

Source

pub async fn join_executor(&self)

Await handler loops and in-flight dispatches after Self::shutdown_executor.

§Contract

Safe when the executor was never started. Restart requires a new Photon build.

Trait Implementations§

Source§

impl Clone for Photon

Source§

fn clone(&self) -> Photon

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more