Skip to main content

AlienContext

Struct AlienContext 

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

Main context for Alien applications that provides access to:

  • Resource bindings (storage, kv, queue, vault, etc.)
  • Event handlers (storage events, cron events, queue messages, commands)
  • Background tasks (wait_until)
  • HTTP server registration

Implementations§

Source§

impl AlienContext

Source

pub async fn from_env() -> Result<Self>

Creates a new AlienContext from environment variables. This automatically sets up gRPC communication and starts the drain listener.

Source

pub async fn from_env_with_vars( env_vars: &HashMap<String, String>, ) -> Result<Self>

Creates a new AlienContext from provided environment variables. This is useful for testing or when environment variables are not available via std::env.

Source

pub fn new( wait_until_context: Arc<WaitUntilContext>, bindings_provider: Arc<dyn BindingsProviderApi>, ) -> Self

Creates a new AlienContext with custom provider and wait_until context. This is mainly useful for testing or advanced use cases.

Source

pub fn bindings(&self) -> &dyn BindingsProviderApi

Gets the bindings provider for accessing storage, build, and other resources.

Source

pub fn get_bindings(&self) -> Arc<dyn BindingsProviderApi>

Gets the bindings provider as an Arc

Source

pub fn on_storage_event<F, Fut>(&self, resource: &str, handler: F)
where F: Fn(StorageEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Registers a handler for storage events on the specified bucket/resource.

§Example
ctx.on_storage_event("uploads", |event| async move {
    println!("File {} was {}", event.key, event.event_type);
    Ok(())
});
Source

pub fn on_cron_event<F, Fut>(&self, schedule: &str, handler: F)
where F: Fn(CronEvent) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Registers a handler for cron/scheduled events.

§Example
ctx.on_cron_event("daily-cleanup", |event| async move {
    cleanup_old_files().await;
    Ok(())
});
Source

pub fn on_queue_message<F, Fut>(&self, queue: &str, handler: F)
where F: Fn(QueueMessage) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<()>> + Send + 'static,

Registers a handler for queue messages.

§Example
ctx.on_queue_message("tasks", |message| async move {
    process_task(&message.payload).await;
    Ok(())
});
Source

pub fn on_command<P, R, F, Fut>(&self, command: &str, handler: F)
where P: DeserializeOwned + Send + 'static, R: Serialize + Send + 'static, F: Fn(P) -> Fut + Send + Sync + 'static, Fut: Future<Output = Result<R>> + Send + 'static,

Registers a command handler for ARC remote calls.

§Example
ctx.on_command::<GenerateReportParams, ReportResult>("generate-report", |params| async move {
    let report = generate_report(params.start_date, params.end_date).await?;
    Ok(report)
});
Source

pub async fn register_http_server(&self, port: u16) -> Result<()>

Registers the application’s HTTP server port with the runtime. The runtime will forward HTTP requests to this port.

§Example
let listener = TcpListener::bind("127.0.0.1:0").await?;
let port = listener.local_addr()?.port();
ctx.register_http_server(port).await?;
Source

pub async fn run(&self) -> Result<()>

Enters the main event loop and processes events from the runtime. This blocks until shutdown is signaled.

Call this after registering all event handlers.

§Example
ctx.on_storage_event("uploads", handler);
ctx.on_command("process", cmd_handler);
ctx.run().await?;
Source

pub fn wait_until<F, Fut>(&self, task_fn: F) -> Result<()>
where F: FnOnce() -> Fut + Send + 'static, Fut: Future<Output = ()> + Send + 'static,

Registers a background task that will run even after the main handler returns. The task runs in the application process and is tracked by the runtime for proper shutdown coordination.

Source

pub fn application_id(&self) -> &str

Gets the application ID for this context.

Source

pub async fn get_task_count(&self) -> Result<u32>

Gets the current number of registered wait_until tasks.

Source

pub async fn get_current_function(&self) -> Result<Option<Arc<dyn Function>>>

Gets the current function binding if available.

Source

pub async fn get_current_container(&self) -> Result<Option<Arc<dyn Container>>>

Gets the current container binding if available.

This allows a container to discover its own public and internal URLs. Useful for constructing callback URLs, OAuth redirects, etc.

§Example
let ctx = AlienContext::from_env().await?;
if let Some(container) = ctx.get_current_container().await? {
    let public_url = container.get_public_url();
    let callback_url = format!("{}/callback", public_url.unwrap_or(""));
}

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<L> LayerExt<L> for L

Source§

fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>
where L: Layer<S>,

Applies the layer to a service and wraps it in Layered.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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