duroxide_pg/lib.rs
1// Copyright (c) Microsoft Corporation.
2// Licensed under the MIT License.
3
4//! # Duroxide PostgreSQL Provider
5//!
6//! A PostgreSQL-based provider implementation for [Duroxide](https://crates.io/crates/duroxide),
7//! a durable task orchestration framework for Rust.
8//!
9//! ## Usage
10//!
11//! ```rust,no_run
12//! use duroxide_pg::PostgresProvider;
13//! use duroxide::runtime::Runtime;
14//! use std::sync::Arc;
15//!
16//! # async fn example() -> anyhow::Result<()> {
17//! // Create a provider with the database URL
18//! let provider = PostgresProvider::new("postgres://user:password@localhost:5432/mydb").await?;
19//!
20//! // Use with the Duroxide runtime
21//! // let runtime = Runtime::start_with_store(Arc::new(provider), activity_registry, orchestration_registry).await;
22//! # Ok(())
23//! # }
24//! ```
25//!
26//! ## Custom Schema
27//!
28//! To isolate data in a specific PostgreSQL schema (useful for multi-tenant deployments):
29//!
30//! ```rust,no_run
31//! use duroxide_pg::PostgresProvider;
32//!
33//! # async fn example() -> anyhow::Result<()> {
34//! let provider = PostgresProvider::new_with_schema(
35//! "postgres://user:password@localhost:5432/mydb",
36//! Some("my_schema"),
37//! ).await?;
38//! # Ok(())
39//! # }
40//! ```
41//!
42//! ## Microsoft Entra ID Authentication
43//!
44//! Connect to Azure Database for PostgreSQL Flexible Server using an Entra
45//! access token. A background task refreshes the token before expiry. See
46//! [`EntraAuthOptions`] for tunables.
47//!
48//! ```rust,no_run
49//! use duroxide_pg::{EntraAuthOptions, PostgresProvider, ProviderConfig};
50//!
51//! # async fn example() -> anyhow::Result<()> {
52//! let config = ProviderConfig::entra(
53//! "myserver.postgres.database.azure.com",
54//! 5432,
55//! "mydb",
56//! "my-entra-principal@contoso.onmicrosoft.com",
57//! EntraAuthOptions::new(),
58//! );
59//! let provider = PostgresProvider::new_with_config(config).await?;
60//! # Ok(())
61//! # }
62//! ```
63//!
64//! All Entra connections use `PgSslMode::VerifyFull`. The default credential
65//! chain is `[WorkloadIdentityCredential (added only when AKS federated env
66//! vars are present), ManagedIdentityCredential, DeveloperToolsCredential]`,
67//! which works for AKS Workload Identity, other Azure-hosted managed
68//! identities, and developer workstations logged in via `az login`.
69//!
70//! ## Configuration
71//!
72//! | Environment Variable | Description | Default |
73//! |---------------------|-------------|---------|
74//! | `DUROXIDE_PG_POOL_MAX` | Maximum connection pool size | `10` |
75//!
76//! ## Optional Long-Polling
77//!
78//! Long-polling is disabled by default. Enable it on worker providers to wait
79//! for PostgreSQL notifications instead of repeatedly querying empty queues:
80//!
81//! ```rust,no_run
82//! use duroxide_pg::{PostgresProvider, ProviderConfig};
83//!
84//! # async fn example(database_url: &str) -> anyhow::Result<()> {
85//! let mut config = ProviderConfig::url(database_url);
86//! config.long_poll.enabled = true;
87//! let provider = PostgresProvider::new_with_config(config).await?;
88//! # Ok(())
89//! # }
90//! ```
91//!
92//! One pool connection is reserved for LISTEN; at least two are required.
93//! Client-only providers can leave this disabled. See [`LongPollConfig`] for
94//! timer and recovery settings. A zero fetch timeout still returns immediately
95//! after checking the queue.
96//!
97//! ## Features
98//!
99//! - Automatic schema migration on startup
100//! - Connection pooling via sqlx
101//! - Custom schema support for multi-tenant isolation
102//! - Full implementation of the Duroxide `Provider` and `ProviderAdmin` traits
103
104pub mod entra;
105pub mod migrations;
106mod notifier;
107pub mod provider;
108
109pub use entra::EntraAuthOptions;
110pub use notifier::LongPollConfig;
111pub use provider::{ConnectionConfig, MigrationPolicy, PostgresProvider, ProviderConfig};