Skip to main content

runtara_environment/
lib.rs

1// Copyright (C) 2025 SyncMyOrders Sp. z o.o.
2// SPDX-License-Identifier: AGPL-3.0-or-later
3//! Runtara Environment - Instance Lifecycle Management
4//!
5//! This crate provides the control plane for managing workflow instances.
6//! It handles image registration, instance lifecycle, container execution,
7//! and wake scheduling for durable sleeps.
8//!
9//! # Architecture
10//!
11//! ```text
12//! ┌─────────────────────────────────────────────────────────────────────────┐
13//! │                         External Clients                                 │
14//! │                    (runtara-management-sdk, CLI)                         │
15//! └─────────────────────────────────────────────────────────────────────────┘
16//!                                    │
17//!                                    ▼
18//! ┌─────────────────────────────────────────────────────────────────────────┐
19//! │                   runtara-environment (This Crate)                       │
20//! │                         Port 8002                                        │
21//! │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐     │
22//! │  │   Image     │  │  Instance   │  │    Wake     │  │  Container  │     │
23//! │  │  Registry   │  │  Lifecycle  │  │  Scheduler  │  │   Runner    │     │
24//! │  └─────────────┘  └─────────────┘  └─────────────┘  └─────────────┘     │
25//! └─────────────────────────────────────────────────────────────────────────┘
26//!           │                 │                              │
27//!           │                 │ Proxy signals                │ Spawn
28//!           │                 ▼                              ▼
29//!           │       ┌───────────────────┐        ┌─────────────────────────┐
30//!           │       │   runtara-core    │◄───────│   Workflow Instances    │
31//!           │       │   Port 8001/8003  │        │   (OCI containers)      │
32//!           │       └───────────────────┘        └─────────────────────────┘
33//!           │                 │
34//!           ▼                 ▼
35//! ┌───────────────────────────────────────────────────────────────────────┐
36//! │                           PostgreSQL                                   │
37//! │              (Images, Instances, Wake Queue)                          │
38//! └───────────────────────────────────────────────────────────────────────┘
39//! ```
40//!
41//! # HTTP Server (Environment Protocol - Port 8002)
42//!
43//! Environment exposes an HTTP server for all management operations.
44//! External clients (via runtara-management-sdk) connect here.
45//!
46//! ## Image Operations
47//!
48//! | Operation | Description |
49//! |-----------|-------------|
50//! | `RegisterImage` | Register a new image (single-frame upload < 16MB) |
51//! | `RegisterImageStream` | Register a large image via streaming upload |
52//! | `ListImages` | List images with optional tenant filter and pagination |
53//! | `GetImage` | Get image details by ID |
54//! | `DeleteImage` | Delete an image |
55//!
56//! ## Instance Operations
57//!
58//! | Operation | Description |
59//! |-----------|-------------|
60//! | `StartInstance` | Start a new instance from an image |
61//! | `StopInstance` | Stop a running instance with grace period |
62//! | `ResumeInstance` | Resume a suspended instance |
63//! | `GetInstanceStatus` | Query instance status |
64//! | `ListInstances` | List instances with filtering and pagination |
65//!
66//! ## Signal Operations
67//!
68//! | Operation | Description |
69//! |-----------|-------------|
70//! | `SendSignal` | Send cancel/pause/resume signal to instance |
71//!
72//! Signals are proxied to runtara-core which stores them for the instance.
73//!
74//! # Runner Types
75//!
76//! Environment supports multiple runner backends for executing workflow binaries:
77//!
78//! | Runner | Description |
79//! |--------|-------------|
80//! | OCI (default) | Execute in OCI containers via runc |
81//! | Native | Execute as direct processes (development) |
82//! | Wasm | Execute as WebAssembly modules (planned) |
83//!
84//! # Instance Status State Machine
85//!
86//! ```text
87//!                     ┌─────────┐
88//!                     │ PENDING │
89//!                     └────┬────┘
90//!                          │ register
91//!                          ▼
92//!                     ┌─────────┐
93//!          ┌──────────│ RUNNING │──────────┐
94//!          │          └────┬────┘          │
95//!          │               │               │
96//!     pause│          sleep│          cancel
97//!          │               │               │
98//!          ▼               ▼               ▼
99//!     ┌──────────┐   ┌──────────┐   ┌───────────┐
100//!     │SUSPENDED │   │SUSPENDED │   │ CANCELLED │
101//!     └────┬─────┘   └────┬─────┘   └───────────┘
102//!          │               │
103//!     resume│          wake│
104//!          │               │
105//!          └───────┬───────┘
106//!                  │
107//!                  ▼
108//!             ┌─────────┐
109//!             │ RUNNING │──────────┬──────────┐
110//!             └─────────┘          │          │
111//!                             complete      fail
112//!                                  │          │
113//!                                  ▼          ▼
114//!                            ┌───────────┐ ┌────────┐
115//!                            │ COMPLETED │ │ FAILED │
116//!                            └───────────┘ └────────┘
117//! ```
118//!
119//! # Configuration
120//!
121//! Configuration is loaded from environment variables:
122//!
123//! | Variable | Required | Default | Description |
124//! |----------|----------|---------|-------------|
125//! | `RUNTARA_ENVIRONMENT_DATABASE_URL` | Yes* | - | PostgreSQL connection string |
126//! | `RUNTARA_DATABASE_URL` | Yes* | - | Fallback if above not set |
127//! | `RUNTARA_ENV_HTTP_PORT` | No | `8002` | HTTP server port |
128//! | `RUNTARA_CORE_ADDR` | No | `127.0.0.1:8001` | runtara-core address |
129//! | `DATA_DIR` | No | `.data` | Data directory for images and bundles |
130//! | `RUNTARA_SKIP_CERT_VERIFICATION` | No | `false` | Skip TLS verification |
131//!
132//! # Modules
133//!
134//! - [`config`]: Server configuration from environment variables
135//! - [`db`]: PostgreSQL persistence for images, instances, and wake queue
136//! - [`error`]: Error types for Environment operations
137//! - [`handlers`]: Environment protocol request handlers
138//! - [`image_registry`]: Image storage and retrieval
139//! - [`container_registry`]: Running container tracking
140//! - [`instance_output`]: Instance output types (legacy, used by SDK)
141//! - [`runner`]: Container/process execution backends
142//! - [`http_server`]: HTTP server implementation
143//! - [`wake_scheduler`]: Durable sleep wake scheduling
144
145#![deny(missing_docs)]
146
147/// Database migrations for runtara-environment.
148///
149/// Environment extends runtara-core's schema. Calling `migrations::run()` will
150/// apply both core and environment migrations in the correct order.
151///
152/// ```ignore
153/// use runtara_environment::migrations;
154///
155/// let pool = PgPool::connect(&database_url).await?;
156/// migrations::run(&pool).await?;
157/// ```
158pub mod migrations;
159
160/// Server configuration loaded from environment variables.
161pub mod config;
162
163/// PostgreSQL database operations for images, instances, and wake queue.
164pub mod db;
165
166/// Error types for Environment operations.
167pub mod error;
168
169/// Environment protocol request handlers.
170pub mod handlers;
171
172/// Image storage and retrieval.
173pub mod image_registry;
174
175/// Running container tracking and management.
176pub mod container_registry;
177
178/// Instance output types (legacy, used by SDK).
179pub mod instance_output;
180
181/// Container/process execution backends (OCI, Native, Wasm).
182pub mod runner;
183
184/// HTTP server for the Environment protocol.
185pub mod http_server;
186
187/// Durable sleep wake scheduling.
188pub mod wake_scheduler;
189
190/// Background worker for cleaning up old run directories.
191pub mod cleanup_worker;
192
193/// Background worker for cleaning up old database records.
194pub mod db_cleanup_worker;
195
196/// Background worker for cleaning up unused images.
197pub mod image_cleanup_worker;
198
199/// Background worker for detecting and failing stale instances.
200pub mod heartbeat_monitor;
201
202/// Embeddable runtime for runtara-environment.
203pub mod runtime;
204
205pub use config::Config;
206pub use error::Error;