Skip to main content

Runtime

Struct Runtime 

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

The ironflow runtime server builder.

Runtime uses a builder pattern: create one with Runtime::new, register webhook routes with Runtime::webhook and cron jobs with Runtime::cron, then call Runtime::serve to start both the HTTP server and the cron scheduler, or Runtime::run_crons to run only the cron scheduler without an HTTP listener.

§Built-in endpoints

MethodPathDescription
GET/healthReturns 200 OK with body "ok". Useful for load-balancer health checks.
POSTuser-definedWebhook endpoints registered via Runtime::webhook.

§Examples

use ironflow_runtime::prelude::*;

#[tokio::main]
async fn main() -> Result<(), ironflow_runtime::error::RuntimeError> {
    Runtime::new()
        .webhook("/hooks/deploy", WebhookAuth::github("secret"), |payload| async move {
            println!("deploy triggered: {payload}");
        })
        .cron("0 0 * * * *", "hourly-sync", || async {
            println!("syncing...");
        })
        .serve("0.0.0.0:3000")
        .await?;

    Ok(())
}

Implementations§

Source§

impl Runtime

Source

pub fn new() -> Self

Creates a new, empty Runtime with no webhooks or cron jobs.

§Examples
use ironflow_runtime::runtime::Runtime;

let runtime = Runtime::new();
Source

pub fn max_body_size(self, bytes: usize) -> Self

Set the maximum allowed body size for webhook payloads.

Requests exceeding this limit are rejected by axum before reaching the handler. Defaults to 2 MiB.

§Examples
use ironflow_runtime::runtime::Runtime;

let runtime = Runtime::new().max_body_size(512 * 1024); // 512 KiB
Source

pub fn max_concurrent_handlers(self, limit: usize) -> Self

Set the maximum number of concurrently running webhook handlers.

When the limit is reached, new webhook requests still receive 202 Accepted but their handlers are queued until a slot is available. Defaults to 64.

§Panics

Panics if limit is 0.

§Examples
use ironflow_runtime::runtime::Runtime;

let runtime = Runtime::new().max_concurrent_handlers(16);
Source

pub fn with_shutdown<F>(self, signal: F) -> Self
where F: Future<Output = ()> + Send + 'static,

Override the default shutdown signal (Ctrl+C / SIGTERM).

By default, Runtime::serve and Runtime::run_crons block until the process receives Ctrl+C or SIGTERM. Use this method to provide a custom future that resolves when the runtime should shut down.

This is useful in tests where you want to trigger a clean shutdown (including scheduler.shutdown()) without relying on OS signals.

§Examples
use ironflow_runtime::runtime::Runtime;
use tokio::sync::oneshot;

let (tx, rx) = oneshot::channel::<()>();

let rt = Runtime::new()
    .with_shutdown(async { let _ = rx.await; })
    .cron("0 */5 * * * *", "check", || async {});

// In another task: tx.send(()) to trigger shutdown.
rt.run_crons().await?;
Source

pub fn webhook<F, Fut>(self, path: &str, auth: WebhookAuth, handler: F) -> Self
where F: Fn(Value) -> Fut + Send + Sync + Clone + 'static, Fut: Future<Output = ()> + Send + 'static,

Registers a webhook route.

The handler receives the parsed JSON body as a serde_json::Value. When a request arrives, the server verifies authentication using auth, then spawns the handler in the background and immediately returns 202 Accepted to the caller.

§Arguments
  • path - The URL path to listen on (e.g. "/hooks/github").
  • auth - The WebhookAuth strategy for this endpoint.
  • handler - An async function receiving the JSON payload.
§Examples
use ironflow_runtime::prelude::*;

let runtime = Runtime::new()
    .webhook("/hooks/github", WebhookAuth::github("secret"), |payload| async move {
        println!("payload: {payload}");
    });
Source

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

Registers a cron job.

The schedule uses a 6-field cron expression (seconds granularity): sec min hour day-of-month month day-of-week.

§Arguments
  • schedule - A 6-field cron expression, e.g. "0 */5 * * * *" for every 5 minutes.
  • name - A human-readable name for logging.
  • handler - An async function to execute on each tick.
§Examples
use ironflow_runtime::prelude::*;

let runtime = Runtime::new()
    .cron("0 0 * * * *", "hourly-cleanup", || async {
        println!("cleaning up...");
    });
Source

pub fn into_router(self) -> Router

Consumes the runtime and returns only the axum Router.

Cron jobs are not started. This is useful for testing the HTTP layer in isolation without side-effects (e.g. with tower::ServiceExt::oneshot).

§Examples
use ironflow_runtime::prelude::*;

let router = Runtime::new()
    .webhook("/hooks/test", WebhookAuth::none(), |_payload| async {})
    .into_router();
Source

pub async fn run_crons(self) -> Result<(), RuntimeError>

Starts only the cron scheduler, blocking until a shutdown signal is received (Ctrl+C / SIGTERM).

Unlike Runtime::serve, this does not start an HTTP server. Any registered webhooks are ignored (a warning is logged if webhooks were registered).

§Errors

Returns an error if:

  • The cron scheduler fails to initialise or a cron expression is invalid.
  • The scheduler fails to shut down cleanly.
§Examples
use ironflow_runtime::prelude::*;

#[tokio::main]
async fn main() -> Result<(), ironflow_runtime::error::RuntimeError> {
    Runtime::new()
        .cron("0 0 * * * *", "hourly-sync", || async {
            println!("syncing...");
        })
        .run_crons()
        .await?;
    Ok(())
}
Source

pub async fn serve(self, addr: &str) -> Result<(), RuntimeError>

Starts the HTTP server and cron scheduler, blocking until shutdown.

This method:

  1. Loads environment variables from .env via dotenvy.
  2. Starts the tokio_cron_scheduler scheduler with all registered cron jobs.
  3. Builds an Axum router with all registered webhook routes plus a GET /health endpoint.
  4. Binds to addr and serves until a Ctrl+C signal is received.
  5. Gracefully shuts down the scheduler before returning.

If you only need cron jobs without an HTTP server, use Runtime::run_crons instead.

§Errors

Returns an error if:

  • The cron scheduler fails to initialise or a cron expression is invalid.
  • The TCP listener cannot bind to addr.
  • The Axum server encounters a fatal I/O error.
§Examples
use ironflow_runtime::prelude::*;

#[tokio::main]
async fn main() -> Result<(), ironflow_runtime::error::RuntimeError> {
    Runtime::new()
        .serve("127.0.0.1:3000")
        .await?;
    Ok(())
}

Trait Implementations§

Source§

impl Default for Runtime

Source§

fn default() -> Self

Returns the “default value” for a type. 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> 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> 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<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