Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
feat(calendar): extract Google Calendar sync into services/calendar_service (#6577)
gbirmanb41f2221925b
diff --git a/.github/workspace-dep-closures.json b/.github/workspace-dep-closures.jsonindex 84c04024ff..bf4038bb24 100644--- a/.github/workspace-dep-closures.json+++ b/.github/workspace-dep-closures.json@@ -313,6 +313,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/harnesses",@@ -456,6 +457,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/import",@@ -823,6 +825,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/import",@@ -936,6 +939,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -1035,6 +1039,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/import",@@ -1221,6 +1226,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/google_token","crates/gtm_invite","crates/harness_id","crates/harness_token",@@ -1466,6 +1472,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/graphql_common","crates/graphql_soup_filter_input","crates/harness_id",@@ -1567,6 +1574,68 @@"crates/remote_env_var","crates/workspace-hack"],+ "calendar_service": [+ "crates/agent",+ "crates/ai_toolset",+ "crates/ai_usage",+ "crates/attachment",+ "crates/authentication_service_client",+ "crates/bot_id",+ "crates/bot_token",+ "crates/broadcast",+ "crates/calendar_events",+ "crates/channel_sender",+ "crates/connection_gateway_client",+ "crates/connection_gateway_models",+ "crates/cowlike",+ "crates/document_sub_type",+ "crates/email_db_client",+ "crates/email_utils",+ "crates/genai_telemetry",+ "crates/google_token",+ "crates/harness_id",+ "crates/harness_token",+ "crates/invite_email",+ "crates/kafka_util",+ "crates/last_online_tracker",+ "crates/macro_auth",+ "crates/macro_authorization",+ "crates/macro_aws_config",+ "crates/macro_config",+ "crates/macro_config_derive",+ "crates/macro_cors",+ "crates/macro_entrypoint",+ "crates/macro_env",+ "crates/macro_env_var",+ "crates/macro_event_broker",+ "crates/macro_event_topics",+ "crates/macro_queues",+ "crates/macro_service_urls",+ "crates/macro_tower_layers",+ "crates/macro_user_id",+ "crates/macro_uuid",+ "crates/mention_utils",+ "crates/model",+ "crates/model-entity",+ "crates/model-error-response",+ "crates/model_file_type",+ "crates/model_notifications",+ "crates/model_user",+ "crates/models_bulk_upload",+ "crates/models_email",+ "crates/models_pagination",+ "crates/models_permissions",+ "crates/non_empty",+ "crates/notification",+ "crates/notification_state",+ "crates/prompt",+ "crates/rate_limit",+ "crates/remote_env_var",+ "crates/secretsmanager_client",+ "crates/sqs_worker",+ "crates/workspace-hack",+ "services/calendar_service"+ ],"call": ["crates/activity","crates/agent",@@ -1693,6 +1762,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/import",@@ -2114,6 +2184,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/harnesses",@@ -2348,6 +2419,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/graphql_activity","crates/graphql_channel","crates/graphql_common",@@ -2622,6 +2694,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -2905,6 +2978,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -3023,6 +3097,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/import",@@ -3177,6 +3252,7 @@"crates/genai_telemetry","crates/generic_email_domains","crates/github",+ "crates/google_token","crates/graphql_activity","crates/graphql_channel","crates/graphql_common",@@ -3566,6 +3642,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -3660,6 +3737,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -3788,6 +3866,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -3866,6 +3945,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -3952,6 +4032,7 @@"crates/frecency","crates/genai_telemetry","crates/gmail_client",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -4046,6 +4127,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -4118,6 +4200,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -4549,6 +4632,40 @@"crates/models_pagination","crates/workspace-hack"],+ "google_token": [+ "crates/agent",+ "crates/ai_toolset",+ "crates/ai_usage",+ "crates/attachment",+ "crates/authentication_service_client",+ "crates/bot_id",+ "crates/bot_token",+ "crates/cowlike",+ "crates/document_sub_type",+ "crates/email_utils",+ "crates/genai_telemetry",+ "crates/google_token",+ "crates/harness_id",+ "crates/harness_token",+ "crates/macro_auth",+ "crates/macro_authorization",+ "crates/macro_env",+ "crates/macro_env_var",+ "crates/macro_user_id",+ "crates/macro_uuid",+ "crates/model",+ "crates/model-entity",+ "crates/model-error-response",+ "crates/model_file_type",+ "crates/model_user",+ "crates/models_bulk_upload",+ "crates/models_pagination",+ "crates/models_permissions",+ "crates/non_empty",+ "crates/prompt",+ "crates/remote_env_var",+ "crates/workspace-hack"+ ],"graphql_activity": ["crates/activity","crates/ai_toolset",@@ -4706,6 +4823,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/graphql_common","crates/graphql_email","crates/harness_id",@@ -4791,6 +4909,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/graphql_common","crates/graphql_email","crates/graphql_entity_mutation",@@ -4908,6 +5027,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/graphql_common","crates/graphql_email","crates/graphql_entity_mutation",@@ -5161,6 +5281,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/graphql_common","crates/graphql_email","crates/graphql_permission",@@ -6054,6 +6175,7 @@"crates/fusionauth","crates/genai_telemetry","crates/generic_email_domains",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/import",@@ -6199,6 +6321,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/import",@@ -6528,6 +6651,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -6623,6 +6747,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -7041,6 +7166,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -7119,6 +7245,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -7230,6 +7357,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -7566,6 +7694,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/import",@@ -7701,6 +7830,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -7819,6 +7949,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -7915,6 +8046,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -8176,6 +8308,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -8274,6 +8407,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -8361,6 +8495,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/graphql_common","crates/graphql_soup_filter_input","crates/harness_id",@@ -8454,6 +8589,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -8549,6 +8685,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -8639,6 +8776,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -8921,6 +9059,7 @@"crates/frecency","crates/genai_telemetry","crates/generic_email_domains",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -9024,6 +9163,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -9102,6 +9242,7 @@"crates/filter_ast","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",@@ -9258,6 +9399,7 @@"crates/foreign_entity","crates/frecency","crates/genai_telemetry",+ "crates/google_token","crates/harness_id","crates/harness_token","crates/invite_email",diff --git a/Cargo.lock b/Cargo.lockindex 83cfc0234f..d286a92bb5 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -3182,6 +3182,53 @@ dependencies = ["workspace-hack",]+[[package]]+name = "calendar_service"+version = "0.1.0"+dependencies = [+ "anyhow",+ "authentication_service_client",+ "aws-sdk-secretsmanager",+ "aws-sdk-sqs",+ "axum",+ "calendar_events",+ "chrono",+ "connection_gateway_client",+ "email_db_client",+ "email_utils",+ "futures",+ "google_token",+ "macro_auth",+ "macro_authorization",+ "macro_aws_config",+ "macro_config",+ "macro_cors",+ "macro_entrypoint",+ "macro_env",+ "macro_env_var",+ "macro_event_broker",+ "macro_queues",+ "macro_service_urls",+ "models_email",+ "redis",+ "reqwest 0.13.4",+ "secretsmanager_client",+ "serde",+ "serde_json",+ "sqlx",+ "sqs_worker",+ "subtle",+ "tokio",+ "tokio-util",+ "tower 0.5.3",+ "tower-http",+ "tracing",+ "utoipa",+ "utoipa-swagger-ui",+ "uuid",+ "workspace-hack",+]+[[package]]name = "call"version = "0.1.0"@@ -5952,6 +5999,7 @@ dependencies = ["filter_ast","frecency","futures",+ "google_token","item_filters","lexical_client","lol_html",@@ -6150,6 +6198,7 @@ dependencies = ["frecency","futures","gmail_client",+ "google_token","html-escape","http-body-util","infer 0.19.0",@@ -7379,6 +7428,18 @@ dependencies = ["workspace-hack",]+[[package]]+name = "google_token"+version = "0.1.0"+dependencies = [+ "anyhow",+ "authentication_service_client",+ "email_utils",+ "redis",+ "tracing",+ "workspace-hack",+]+[[package]]name = "graphql_activity"version = "0.1.0"diff --git a/Cargo.toml b/Cargo.tomlindex c55ce5b743..c007320b1a 100644--- a/Cargo.toml+++ b/Cargo.toml@@ -41,6 +41,7 @@ members = ["crates/genai_telemetry","crates/generic_email_domains","crates/gmail_client",+ "crates/google_token","crates/graphql_activity","crates/graphql_channel","crates/graphql_common",@@ -96,6 +97,7 @@ members = ["services/agent_trigger_service","services/ai_projections_refresh_handler","services/authentication_service",+ "services/calendar_service","services/call_recording_preview_handler","services/connection_gateway","services/contacts_service",diff --git a/crates/email/Cargo.toml b/crates/email/Cargo.tomlindex 593731f9f0..525e68a973 100644--- a/crates/email/Cargo.toml+++ b/crates/email/Cargo.toml@@ -33,7 +33,7 @@ axum = ["dep:utoipa","models_pagination/axum",]-gmail_token = ["dep:authentication_service_client", "dep:redis", "ports"]+gmail_token = ["dep:authentication_service_client", "dep:google_token", "dep:redis", "ports"]http_client = ["dep:reqwest", "ports"]inbound = ["ports"]outbound = ["dep:sqlx", "ports"]@@ -61,6 +61,7 @@ either = { workspace = true }email_utils = { path = "../email_utils" }filter_ast = { path = "../filter_ast" }frecency = { path = "../frecency" }+google_token = { path = "../google_token", optional = true }item_filters = { path = "../item_filters" }lol_html = { workspace = true }scraper = { workspace = true }diff --git a/crates/email/src/outbound.rs b/crates/email/src/outbound.rsindex c52c3db158..be3997e114 100644--- a/crates/email/src/outbound.rs+++ b/crates/email/src/outbound.rs@@ -10,6 +10,4 @@ pub use email_pg_repo::EmailPgRepo;#[cfg(feature = "http_client")]pub use email_service_http::EmailServiceHttpClient;#[cfg(feature = "gmail_token")]-pub use gmail_token_provider::{- GmailTokenProviderImpl, fetch_gmail_access_token, fetch_gmail_access_token_no_cache,-};+pub use gmail_token_provider::GmailTokenProviderImpl;diff --git a/crates/email/src/outbound/gmail_token_provider.rs b/crates/email/src/outbound/gmail_token_provider.rsindex 2d22ea3cb2..d9f56883b9 100644--- a/crates/email/src/outbound/gmail_token_provider.rs+++ b/crates/email/src/outbound/gmail_token_provider.rs@@ -1,16 +1,11 @@-use anyhow::Context;use authentication_service_client::AuthServiceClient;use email_utils::token_cache_key::TokenCacheKey;-use redis::AsyncCommands;use redis::aio::MultiplexedConnection;use std::sync::Arc;use crate::domain::models::{EmailErr, Link};use crate::domain::ports::GmailTokenProvider;-/// Ten minutes less than the hour the token is valid for, for long-running jobs.-static GMAIL_ACCESS_TOKEN_EXPIRY_SECONDS: u64 = 3000;-/// Adapter implementing [`GmailTokenProvider`] using Redis cache with auth service fallback.pub struct GmailTokenProviderImpl {redis_conn: MultiplexedConnection,@@ -39,7 +34,7 @@ impl GmailTokenProvider for GmailTokenProviderImpl {link.email_address.0.as_ref(),link.provider.as_str(),);- fetch_gmail_access_token(&key, &self.redis_conn, &self.auth_service_client)+ google_token::fetch_gmail_access_token(&key, &self.redis_conn, &self.auth_service_client).await.map_err(EmailErr::ProviderErr)}@@ -51,88 +46,12 @@ impl GmailTokenProvider for GmailTokenProviderImpl {link.email_address.0.as_ref(),link.provider.as_str(),);- fetch_gmail_access_token_no_cache(&key, &self.redis_conn, &self.auth_service_client)- .await- .map_err(EmailErr::ProviderErr)- }-}--/// Fetches a Gmail access token, first checking the Redis cache then falling back to the auth-/// service. Caches newly fetched tokens in Redis.-///-/// [`MultiplexedConnection`] is cheap to clone internally, so passing a reference is fine.-pub async fn fetch_gmail_access_token(- key: &TokenCacheKey,- redis_conn: &MultiplexedConnection,- auth_service_client: &AuthServiceClient,-) -> anyhow::Result<String> {- // MultiplexedConnection::clone is just an Arc bump- let mut conn = redis_conn.clone();- let redis_key = key.to_redis_key();-- let token_from_redis: Option<String> = conn- .get(&redis_key)+ google_token::fetch_gmail_access_token_no_cache(+ &key,+ &self.redis_conn,+ &self.auth_service_client,+ ).await- .map_err(|e| anyhow::anyhow!("Redis error: {}. TokenCacheKey: {:?}", e, key))- .ok()- .flatten();-- let access_token = if let Some(token) = token_from_redis {- token- } else {- let token = fetch_token_from_auth_service(key, auth_service_client).await?;- cache_token_in_redis(&mut conn, key, &token).await;- token- };-- Ok(access_token)-}--/// Fetches a Gmail access token directly from the auth service, bypassing the Redis cache for-/// reads but still caching the newly fetched token.-#[tracing::instrument(skip(key, redis_conn, auth_service_client), err(level = "debug"))]-pub async fn fetch_gmail_access_token_no_cache(- key: &TokenCacheKey,- redis_conn: &MultiplexedConnection,- auth_service_client: &AuthServiceClient,-) -> anyhow::Result<String> {- let token = fetch_token_from_auth_service(key, auth_service_client).await?;- let mut conn = redis_conn.clone();- cache_token_in_redis(&mut conn, key, &token).await;- Ok(token)-}--/// Best-effort cache write. Logs a warning on failure but never errors.-#[tracing::instrument(skip(conn, key, token))]-async fn cache_token_in_redis(conn: &mut MultiplexedConnection, key: &TokenCacheKey, token: &str) {- let redis_key = key.to_redis_key();- if let Err(cache_err) = conn- .set_ex::<&str, &str, ()>(&redis_key, token, GMAIL_ACCESS_TOKEN_EXPIRY_SECONDS)- .await- {- tracing::warn!(- error = ?cache_err,- token_cache_key = ?key,- "Failed to cache fetched access token in Redis"- );+ .map_err(EmailErr::ProviderErr)}}--/// Fetches a Gmail access token from the auth service.-#[tracing::instrument(skip(key, auth_service_client), err(level = "debug"))]-async fn fetch_token_from_auth_service(- key: &TokenCacheKey,- auth_service_client: &AuthServiceClient,-) -> anyhow::Result<String> {- let fetched_token = auth_service_client- .get_google_access_token(&key.fusion_user_id, &key.email_address)- .await- .with_context(|| {- format!(- "Failed to get Google access token from auth service. TokenCacheKey: {:?}",- key- )- })?;-- Ok(fetched_token.access_token)-}diff --git a/crates/google_token/Cargo.toml b/crates/google_token/Cargo.tomlnew file mode 100644index 0000000000..4e1679261d--- /dev/null+++ b/crates/google_token/Cargo.toml@@ -0,0 +1,13 @@+[package]+edition = "2024"+name = "google_token"+publish = false+version = "0.1.0"++[dependencies]+anyhow = { workspace = true }+authentication_service_client = { path = "../authentication_service_client" }+email_utils = { path = "../email_utils" }+redis = { workspace = true, features = ["tokio-rustls-comp"] }+tracing = { workspace = true }+workspace-hack = { version = "0.1", path = "../workspace-hack" }diff --git a/crates/google_token/src/lib.rs b/crates/google_token/src/lib.rsnew file mode 100644index 0000000000..eec4e8605b--- /dev/null+++ b/crates/google_token/src/lib.rs@@ -0,0 +1,92 @@+#![deny(missing_docs)]+//! Shared Google/Gmail access-token acquisition against the Redis cache and+//! authentication service, reused by the email and calendar hosts.++use anyhow::Context;+use authentication_service_client::AuthServiceClient;+use email_utils::token_cache_key::TokenCacheKey;+use redis::AsyncCommands;+use redis::aio::MultiplexedConnection;++/// Ten minutes less than the hour the token is valid for, for long-running jobs.+static GMAIL_ACCESS_TOKEN_EXPIRY_SECONDS: u64 = 3000;++/// Fetches a Gmail access token, first checking the Redis cache then falling back to the auth+/// service. Caches newly fetched tokens in Redis.+///+/// [`MultiplexedConnection`] is cheap to clone internally, so passing a reference is fine.+pub async fn fetch_gmail_access_token(+ key: &TokenCacheKey,+ redis_conn: &MultiplexedConnection,+ auth_service_client: &AuthServiceClient,+) -> anyhow::Result<String> {+ // MultiplexedConnection::clone is just an Arc bump+ let mut conn = redis_conn.clone();+ let redis_key = key.to_redis_key();++ let token_from_redis: Option<String> = conn+ .get(&redis_key)+ .await+ .map_err(|e| anyhow::anyhow!("Redis error: {}. TokenCacheKey: {:?}", e, key))+ .ok()+ .flatten();++ let access_token = if let Some(token) = token_from_redis {+ token+ } else {+ let token = fetch_token_from_auth_service(key, auth_service_client).await?;+ cache_token_in_redis(&mut conn, key, &token).await;+ token+ };++ Ok(access_token)+}++/// Fetches a Gmail access token directly from the auth service, bypassing the Redis cache for+/// reads but still caching the newly fetched token.+#[tracing::instrument(skip(key, redis_conn, auth_service_client), err(level = "debug"))]+pub async fn fetch_gmail_access_token_no_cache(+ key: &TokenCacheKey,+ redis_conn: &MultiplexedConnection,+ auth_service_client: &AuthServiceClient,+) -> anyhow::Result<String> {+ let token = fetch_token_from_auth_service(key, auth_service_client).await?;+ let mut conn = redis_conn.clone();+ cache_token_in_redis(&mut conn, key, &token).await;+ Ok(token)+}++/// Best-effort cache write. Logs a warning on failure but never errors.+#[tracing::instrument(skip(conn, key, token))]+async fn cache_token_in_redis(conn: &mut MultiplexedConnection, key: &TokenCacheKey, token: &str) {+ let redis_key = key.to_redis_key();+ if let Err(cache_err) = conn+ .set_ex::<&str, &str, ()>(&redis_key, token, GMAIL_ACCESS_TOKEN_EXPIRY_SECONDS)+ .await+ {+ tracing::warn!(+ error = ?cache_err,+ token_cache_key = ?key,+ "Failed to cache fetched access token in Redis"+ );+ }+}++/// Fetches a Gmail access token from the auth service.+#[tracing::instrument(skip(key, auth_service_client), err(level = "debug"))]+async fn fetch_token_from_auth_service(+ key: &TokenCacheKey,+ auth_service_client: &AuthServiceClient,+) -> anyhow::Result<String> {+ let fetched_token = auth_service_client+ .get_google_access_token(&key.fusion_user_id, &key.email_address)+ .await+ .with_context(|| {+ format!(+ "Failed to get Google access token from auth service. TokenCacheKey: {:?}",+ key+ )+ })?;++ Ok(fetched_token.access_token)+}diff --git a/crates/macro_queues/src/lib.rs b/crates/macro_queues/src/lib.rsindex 0ee2bcf9d4..a3b799b666 100644--- a/crates/macro_queues/src/lib.rs+++ b/crates/macro_queues/src/lib.rs@@ -627,6 +627,15 @@ queue! {dev: "calendar-reminder-dispatch-queue-dev",prod: "calendar-reminder-dispatch-queue-prod",},+ /// Queue for the Google Calendar sync backfill process (calendar_service).+ ///+ /// Calendar keeps its own backfill queue rather than sharing the email+ /// backfill queue, so its sync deploy lifecycle is independent of email.+ pub CalendarServiceBackfillQueue {+ local: "calendar-service-backfill-queue",+ dev: "calendar-service-backfill-queue-dev",+ prod: "calendar-service-backfill-queue-prod",+ },/// Queue for the organization retention handler/// (organization_retention_trigger `ORGANIZATION_RETENTION_QUEUE`).pub OrganizationRetentionQueue {diff --git a/services/calendar_service/Cargo.toml b/services/calendar_service/Cargo.tomlnew file mode 100644index 0000000000..98431e6654--- /dev/null+++ b/services/calendar_service/Cargo.toml@@ -0,0 +1,67 @@+[package]+default-run = "calendar_service"+edition = "2024"+name = "calendar_service"+publish = false+version = "0.1.0"++[[bin]]+name = "calendar_service"+path = "src/main.rs"++[[bin]]+name = "calendar_service_openapi"+path = "src/openapi.rs"++[features]+connection_gateway = []+default = ["connection_gateway"]++[dependencies]+anyhow = { workspace = true }+authentication_service_client = { path = "../../crates/authentication_service_client" }+aws-sdk-secretsmanager = { workspace = true }+aws-sdk-sqs = { workspace = true }+axum = { workspace = true }+calendar_events = { path = "../../crates/calendar_events", features = [+ "google",+ "inbound",+ "postgres",+] }+chrono = { workspace = true }+connection_gateway_client = { path = "../../crates/connection_gateway_client" }+email_db_client = { path = "../../crates/email_db_client" }+email_utils = { path = "../../crates/email_utils" }+futures = { workspace = true }+google_token = { path = "../../crates/google_token" }+macro_auth = { path = "../../crates/macro_auth" }+macro_authorization = { path = "../../crates/macro_authorization", features = [+ "postgres",+] }+macro_aws_config = { path = "../../crates/macro_aws_config" }+macro_config = { path = "../../crates/macro_config" }+macro_cors = { path = "../../crates/macro_cors" }+macro_entrypoint = { path = "../../crates/macro_entrypoint" }+macro_env = { path = "../../crates/macro_env" }+macro_env_var = { path = "../../crates/macro_env_var" }+macro_event_broker = { path = "../../crates/macro_event_broker" }+macro_queues = { path = "../../crates/macro_queues" }+macro_service_urls = { path = "../../crates/macro_service_urls" }+models_email = { path = "../../crates/models_email" }+redis = { workspace = true, features = ["tokio-rustls-comp"] }+reqwest = { workspace = true }+secretsmanager_client = { path = "../../crates/secretsmanager_client" }+serde = { workspace = true }+serde_json = { workspace = true }+sqlx = { workspace = true }+sqs_worker = { path = "../../crates/sqs_worker" }+subtle = { workspace = true }+tokio = { workspace = true, features = ["signal"] }+tokio-util = { workspace = true, features = ["rt"] }+tower = { workspace = true }+tower-http = { workspace = true, features = ["compression-gzip"] }+tracing = { workspace = true }+utoipa = { workspace = true }+utoipa-swagger-ui = { workspace = true }+uuid = { workspace = true }+workspace-hack = { version = "0.1", path = "../../crates/workspace-hack" }diff --git a/services/calendar_service/src/api.rs b/services/calendar_service/src/api.rsnew file mode 100644index 0000000000..ca892c3e47--- /dev/null+++ b/services/calendar_service/src/api.rs@@ -0,0 +1,74 @@+use anyhow::Context;+use axum::Router;+use calendar_events::inbound::mutation_router::{+ CalendarMutationRouterState, calendar_mutation_router,+};+use context::ApiContext;+use tower::ServiceBuilder;+use tower_http::{compression::CompressionLayer, trace::TraceLayer};+use utoipa::OpenApi;+use utoipa_swagger_ui::SwaggerUi;++pub mod context;++mod calendar_watch;+pub mod swagger;++/// Path prefix the shared gateway ALB forwards unmodified. Dual-mounted+/// alongside `/` so a dedicated ALB keeps working during cutover.+const GATEWAY_PATH_PREFIX: &str = "/calendar";++/// Build the application and serve it until a shutdown signal arrives.+pub async fn setup_and_serve(state: ApiContext) -> anyhow::Result<()> {+ let env = state.config.environment;+ let port = state.config.port;+ let traced_api = api_router(state.clone()).with_state(state).layer(+ ServiceBuilder::new()+ .layer(TraceLayer::new_for_http())+ .layer(macro_cors::cors_layer())+ .layer(CompressionLayer::new().gzip(true)),+ );+ let health = crate::health::router();+ let app = mount_at_root_and_prefix(traced_api.merge(health)).merge(swagger_ui());++ let listener = tokio::net::TcpListener::bind(format!("0.0.0.0:{}", port))+ .await+ .unwrap();+ tracing::info!(+ "calendar service is up and running with environment {:?} on port {}",+ env,+ port+ );+ axum::serve(listener, app.into_make_service())+ .with_graceful_shutdown(macro_entrypoint::shutdown_signal())+ .await+ .context("error starting service")+}++fn mount_at_root_and_prefix(inner: Router) -> Router {+ Router::new()+ .merge(inner.clone())+ .nest(GATEWAY_PATH_PREFIX, inner)+}++fn swagger_ui() -> Router {+ Router::new()+ .merge(SwaggerUi::new("/docs").url("/api-doc/openapi.json", swagger::ApiDoc::openapi()))+ .merge(+ SwaggerUi::new("/calendar/docs")+ .url("/calendar/api-doc/openapi.json", swagger::ApiDoc::openapi()),+ )+}++fn api_router(state: ApiContext) -> Router<ApiContext> {+ // Calendar mutations follow the calendar sync kill switch: without sync a+ // provider write would never be reflected locally.+ if state.config.calendar_sync_enabled {+ calendar_watch::router().merge(calendar_mutation_router(CalendarMutationRouterState::new(+ state.calendar_mutation_service.clone(),+ state.authorization_state.clone(),+ )))+ } else {+ calendar_watch::router()+ }+}diff --git a/services/calendar_service/src/api/calendar_watch.rs b/services/calendar_service/src/api/calendar_watch.rsnew file mode 100644index 0000000000..c2bc107e32--- /dev/null+++ b/services/calendar_service/src/api/calendar_watch.rs@@ -0,0 +1,68 @@+//! Google Calendar push notification webhook.+//!+//! Google delivers content-free notifications here for every calendar with+//! an open `events.watch` channel. The handler verifies the shared channel+//! token minted at channel creation, then re-arms the watched inbox's sync+//! job; the regular poll remains the backstop for dropped notifications, so+//! unmatched or failed notifications are acknowledged rather than retried.++use axum::Router;+use axum::extract::State;+use axum::http::{HeaderMap, StatusCode};+use axum::routing::post;+use subtle::ConstantTimeEq;++use crate::api::context::ApiContext;+use crate::config::calendar_watch_config;++/// Build the unauthenticated watch notification router.+pub fn router() -> Router<ApiContext> {+ Router::new().route("/notifications", post(handler))+}++fn header<'a>(headers: &'a HeaderMap, name: &str) -> Option<&'a str> {+ headers.get(name).and_then(|value| value.to_str().ok())+}++#[tracing::instrument(skip_all)]+async fn handler(State(ctx): State<ApiContext>, headers: HeaderMap) -> StatusCode {+ let Some(config) = calendar_watch_config() else {+ return StatusCode::NOT_FOUND;+ };++ let valid = header(&headers, "x-goog-channel-token")+ .is_some_and(|token| bool::from(token.as_bytes().ct_eq(config.token.as_bytes())));++ if !valid {+ return StatusCode::FORBIDDEN;+ }++ if header(&headers, "x-goog-resource-state") == Some("sync") {+ return StatusCode::OK;+ }++ let (Some(channel_id), Some(resource_id)) = (+ header(&headers, "x-goog-channel-id"),+ header(&headers, "x-goog-resource-id"),+ ) else {+ return StatusCode::BAD_REQUEST;+ };+ match ctx+ .calendar_service+ .handle_watch_notification(channel_id, resource_id)+ .await+ {+ Ok(matched) => {+ if !matched {+ tracing::debug!(+ channel_id,+ "calendar notification matched no active channel"+ );+ }+ }+ Err(error) => {+ tracing::warn!(error = ?error, channel_id, "failed to apply calendar watch notification");+ }+ }+ StatusCode::OK+}diff --git a/services/calendar_service/src/api/context.rs b/services/calendar_service/src/api/context.rsnew file mode 100644index 0000000000..4072864621--- /dev/null+++ b/services/calendar_service/src/api/context.rs@@ -0,0 +1,41 @@+use std::sync::Arc;++use calendar_events::{+ domain::{mutations::CalendarMutationServiceImpl, service::CalendarService},+ outbound::{google::GoogleCalendarClient, pg::PgCalendarRepository},+};+use macro_authorization::{+ MacroAuthJwtValidator, MacroAuthorizationServiceImpl, MacroAuthorizationState,+};++use crate::calendar_backfill::CalendarEventBroker;+use crate::calendar_backfill_adapters::RedisCalendarRequestGate;+use crate::calendar_refresh::ConnectionGatewayCalendarRefresh;+use crate::calendar_tokens::CalendarTokenProviderAdapter;+use crate::config::Config;++/// JWT-validating authorization service used by the mutation routes.+pub type AuthorizationService = MacroAuthorizationServiceImpl<MacroAuthJwtValidator>;+/// Calendar grant/watch service backing the push webhook.+pub type CalendarGrantService = CalendarService<PgCalendarRepository>;+/// User-initiated calendar mutation service.+pub type CalendarMutationSvc = CalendarMutationServiceImpl<+ PgCalendarRepository,+ GoogleCalendarClient<RedisCalendarRequestGate>,+ CalendarTokenProviderAdapter,+ CalendarEventBroker,+ ConnectionGatewayCalendarRefresh,+>;++/// Shared HTTP application state for the calendar service.+#[derive(Clone)]+pub struct ApiContext {+ /// Resolved service configuration.+ pub config: Arc<Config>,+ /// Authorization state for authenticated mutation routes.+ pub authorization_state: MacroAuthorizationState<AuthorizationService>,+ /// Calendar grant/watch service backing the push webhook.+ pub calendar_service: Arc<CalendarGrantService>,+ /// User-initiated calendar mutation service.+ pub calendar_mutation_service: Arc<CalendarMutationSvc>,+}diff --git a/services/calendar_service/src/api/swagger.rs b/services/calendar_service/src/api/swagger.rsnew file mode 100644index 0000000000..566f0830df--- /dev/null+++ b/services/calendar_service/src/api/swagger.rs@@ -0,0 +1,6 @@+use utoipa::OpenApi;++/// OpenAPI document for the calendar service.+#[derive(OpenApi)]+#[openapi(paths(crate::health::health_handler))]+pub struct ApiDoc;diff --git a/services/calendar_service/src/backfill_queue.rs b/services/calendar_service/src/backfill_queue.rsnew file mode 100644index 0000000000..161ca6fae1--- /dev/null+++ b/services/calendar_service/src/backfill_queue.rs@@ -0,0 +1,51 @@+//! Calendar's own backfill queue: message type and SQS enqueue client.+//!+//! Calendar keeps a queue and message type separate from the shared email+//! backfill queue so its sync deploy lifecycle is independent of email.++use uuid::Uuid;++/// A delivery on the calendar backfill queue. The `kind` tag mirrors the+/// `calendar_backfill_jobs.kind` column so new provider kinds slot in without+/// changing the transport.+#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]+#[serde(tag = "kind", rename_all = "snake_case")]+pub enum CalendarBackfillMessage {+ /// Run a Google Calendar backfill for a connected inbox.+ GoogleCalendar {+ /// Connected inbox whose grant authorizes the sync.+ link_id: Uuid,+ /// Durable calendar backfill job id.+ calendar_job_id: Uuid,+ },+}++/// SQS client scoped to the calendar backfill queue.+#[derive(Clone)]+pub struct CalendarBackfillQueueClient {+ client: aws_sdk_sqs::Client,+ queue_url: String,+}++impl CalendarBackfillQueueClient {+ /// Construct the client over an SQS client and the resolved queue name/URL.+ pub fn new(client: aws_sdk_sqs::Client, queue_url: impl Into<String>) -> Self {+ Self {+ client,+ queue_url: queue_url.into(),+ }+ }++ /// Publish one calendar backfill message.+ #[tracing::instrument(skip(self))]+ pub async fn enqueue(&self, message: &CalendarBackfillMessage) -> anyhow::Result<()> {+ let body = serde_json::to_string(message)?;+ self.client+ .send_message()+ .queue_url(&self.queue_url)+ .message_body(body)+ .send()+ .await?;+ Ok(())+ }+}diff --git a/services/calendar_service/src/calendar_backfill.rs b/services/calendar_service/src/calendar_backfill.rsnew file mode 100644index 0000000000..a79f5a5efc--- /dev/null+++ b/services/calendar_service/src/calendar_backfill.rs@@ -0,0 +1,393 @@+//! Calendar backfill queue consumer.+//!+//! Drains calendar's own backfill queue, resolves the connected inbox's grant,+//! and drives the Google Calendar backfill coordinator. Modeled on the calendar+//! branch of email_service's backfill worker, but self-contained: tokens come+//! from [`google_token`] rather than the Gmail provider stack.++use std::sync::Arc;+use std::time::Duration;++use authentication_service_client::{AuthServiceClient, error::AuthServiceClientError};+use calendar_events::domain::{+ models::{+ CalendarBackfillFailureDisposition, CalendarBackfillJobKey, GoogleBackfillRunReport,+ GoogleWatchConfig, OccurrenceRange,+ },+ service::{+ GoogleCalendarBackfillCoordinator, GoogleCalendarBackfillFailureService,+ GoogleCalendarBackfillRunError,+ },+};+use calendar_events::outbound::{google::GoogleCalendarClient, pg::PgCalendarRepository};+use chrono::Utc;+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};+use models_email::email::service::link::Link;+use redis::aio::MultiplexedConnection;+use sqlx::PgPool;+use tokio_util::sync::CancellationToken;+use tokio_util::task::TaskTracker;+use uuid::Uuid;++use crate::backfill_queue::CalendarBackfillMessage;+use crate::calendar_backfill_adapters::RedisCalendarRequestGate;+use crate::calendar_outbox::republish_calendar_job;+use crate::calendar_ratelimit::CalendarRateLimiter;+use crate::pubsub_util::cg_refresh_calendar;++/// The event broker used by the backfill coordinator.+pub type CalendarEventBroker = MacroEventBrokerService<KafkaEventPublisher, TaskTracker>;++/// Concrete Google Calendar backfill coordinator for this service.+type GoogleCalendarBackfillService = GoogleCalendarBackfillCoordinator<+ PgCalendarRepository,+ GoogleCalendarClient<RedisCalendarRequestGate>,+ PgCalendarRepository,+ CalendarEventBroker,+>;++/// Concrete pre-lease Google Calendar failure handler for this service.+type GoogleCalendarBackfillFailureHandler =+ GoogleCalendarBackfillFailureService<PgCalendarRepository>;++/// Everything a backfill worker needs to process one calendar delivery.+#[derive(Clone)]+pub struct CalendarBackfillContext {+ db: PgPool,+ sqs_worker: sqs_worker::SQSWorker,+ redis_conn: MultiplexedConnection,+ auth_service_client: AuthServiceClient,+ connection_gateway_client: connection_gateway_client::client::ConnectionGatewayClient,+ coordinator: Arc<GoogleCalendarBackfillService>,+ failure: Arc<GoogleCalendarBackfillFailureHandler>,+ calendar_sync_enabled: bool,+}++impl CalendarBackfillContext {+ /// Compose the backfill context from process-level adapters.+ #[allow(clippy::too_many_arguments)]+ pub fn new(+ db: PgPool,+ sqs_worker: sqs_worker::SQSWorker,+ rate_limiter: CalendarRateLimiter,+ redis_conn: MultiplexedConnection,+ auth_service_client: AuthServiceClient,+ connection_gateway_client: connection_gateway_client::client::ConnectionGatewayClient,+ macro_event_broker: CalendarEventBroker,+ watch: Option<GoogleWatchConfig>,+ calendar_sync_enabled: bool,+ ) -> Self {+ let repository = PgCalendarRepository::new(db.clone());+ let coordinator = Arc::new(GoogleCalendarBackfillCoordinator::new(+ repository.clone(),+ GoogleCalendarClient::with_gate(+ reqwest::Client::builder()+ .timeout(Duration::from_secs(30))+ .build()+ .expect("calendar client configuration is valid"),+ RedisCalendarRequestGate::new(rate_limiter),+ ),+ repository.clone(),+ macro_event_broker,+ watch,+ ));+ let failure = Arc::new(GoogleCalendarBackfillFailureService::new(repository));+ Self {+ db,+ sqs_worker,+ redis_conn,+ auth_service_client,+ connection_gateway_client,+ coordinator,+ failure,+ calendar_sync_enabled,+ }+ }+}++/// The queue-visible outcome of processing a delivery.+enum Disposition {+ /// Delete the message; work completed or reached a terminal state.+ Ack,+ /// Leave the message for redelivery after its visibility timeout.+ Retry(anyhow::Error),+}++/// Poll the calendar backfill queue until cancellation is requested, restarting+/// the inner loop if it panics.+pub async fn run_worker(+ ctx: CalendarBackfillContext,+ worker: sqs_worker::SQSWorker,+ cancellation_token: CancellationToken,+) {+ loop {+ let worker_result = tokio::spawn({+ let ctx = ctx.clone();+ let worker = worker.clone();+ let cancellation_token = cancellation_token.clone();+ async move {+ loop {+ let receive_result = tokio::select! {+ result = worker.receive_messages() => result,+ _ = cancellation_token.cancelled() => return,+ };+ match receive_result {+ Ok(messages) => {+ for message in &messages {+ if let Err(error) = process_message(&ctx, message).await {+ tracing::error!(+ message_id = message.message_id,+ error = ?error,+ "error processing calendar backfill message"+ );+ }+ }+ }+ Err(error) => {+ tracing::error!(error = ?error, "error receiving calendar backfill messages");+ }+ }+ }+ }+ })+ .await;++ if cancellation_token.is_cancelled() {+ return;+ }++ match worker_result {+ Ok(()) => tracing::error!("calendar backfill worker exited unexpectedly"),+ Err(error) => tracing::error!(error = ?error, "calendar backfill worker crashed"),+ }++ tracing::info!("CALENDAR BACKFILL WORKER RESTARTING...");+ tokio::select! {+ _ = tokio::time::sleep(Duration::from_secs(5)) => {}+ _ = cancellation_token.cancelled() => return,+ }+ }+}++async fn process_message(+ ctx: &CalendarBackfillContext,+ message: &aws_sdk_sqs::types::Message,+) -> anyhow::Result<()> {+ // Malformed JSON is not retryable: ack it so it does not redeliver forever.+ let data = match extract_message(message) {+ Ok(data) => data,+ Err(error) => {+ tracing::error!(error = %error, "failed to extract calendar backfill message; acking");+ sqs_worker::cleanup_message(&ctx.sqs_worker, message)+ .await+ .inspect_err(|cleanup_error| {+ tracing::error!(error = %cleanup_error, "failed to clean up unparseable message");+ })+ .ok();+ return Err(error);+ }+ };++ match dispatch(ctx, &data).await {+ Disposition::Ack => {+ sqs_worker::cleanup_message(&ctx.sqs_worker, message).await?;+ Ok(())+ }+ Disposition::Retry(error) => {+ tracing::warn!(error = ?error, "retryable calendar backfill error; leaving message for redelivery");+ Ok(())+ }+ }+}++async fn dispatch(ctx: &CalendarBackfillContext, data: &CalendarBackfillMessage) -> Disposition {+ match data {+ CalendarBackfillMessage::GoogleCalendar {+ link_id,+ calendar_job_id,+ } => google_backfill(ctx, *link_id, *calendar_job_id).await,+ }+}++#[tracing::instrument(skip(ctx), fields(%calendar_job_id, %link_id))]+async fn google_backfill(+ ctx: &CalendarBackfillContext,+ link_id: Uuid,+ calendar_job_id: Uuid,+) -> Disposition {+ // When calendar sync is disabled, return the delivery to the outbox and ack;+ // the drain republishes it once the switch flips back on.+ if !ctx.calendar_sync_enabled {+ tracing::info!(%calendar_job_id, "calendar sync disabled; returning delivery to the outbox");+ return match republish_calendar_job(&ctx.db, calendar_job_id).await {+ Ok(()) => Disposition::Ack,+ Err(error) => Disposition::Retry(+ error.context("failed to return calendar delivery to the outbox"),+ ),+ };+ }++ let link = match email_db_client::links::get::fetch_link_by_id(&ctx.db, link_id).await {+ Ok(Some(link)) => link,+ Ok(None) => {+ tracing::error!(%link_id, "link not found for calendar backfill; failing job");+ return fail_unclaimed(+ ctx,+ link_id,+ calendar_job_id,+ CalendarBackfillFailureDisposition::Permanent,+ "link not found for calendar backfill",+ )+ .await;+ }+ Err(error) => {+ return Disposition::Retry(error.context("failed to load link for calendar backfill"));+ }+ };++ // Calendar jobs can be created immediately after an incremental Google scope+ // grant. Bypass the token cache so this job does not reuse a pre-consent+ // access token that lacks calendar scopes.+ let key = email_utils::token_cache_key::TokenCacheKey::new(+ &link.fusionauth_user_id,+ link.email_address.0.as_ref(),+ link.provider.as_str(),+ );+ let access_token = match google_token::fetch_gmail_access_token_no_cache(+ &key,+ &ctx.redis_conn,+ &ctx.auth_service_client,+ )+ .await+ {+ Ok(token) => token,+ Err(error) => {+ if is_reauth_required_error(&error) {+ return fail_unclaimed(+ ctx,+ link_id,+ calendar_job_id,+ CalendarBackfillFailureDisposition::ReauthRequired,+ &format!("{error:?}"),+ )+ .await;+ }+ return Disposition::Retry(+ error.context("failed to fetch token for Google Calendar backfill"),+ );+ }+ };++ run_coordinator(ctx, &access_token, &link, calendar_job_id).await+}++async fn run_coordinator(+ ctx: &CalendarBackfillContext,+ access_token: &str,+ link: &Link,+ calendar_job_id: Uuid,+) -> Disposition {+ let mut report = GoogleBackfillRunReport::default();+ let run_result = ctx+ .coordinator+ .run(+ CalendarBackfillJobKey {+ job_id: calendar_job_id,+ email_link_id: link.id,+ },+ link.macro_id.as_ref(),+ access_token,+ OccurrenceRange::maintenance_horizon(Utc::now()),+ &mut report,+ )+ .await;+ // Quiet token-only polls change nothing; only real changes nudge active+ // viewers to refetch. The nudge goes out even when the run ultimately+ // failed: per-calendar commits before the error are durable, and the+ // retry's quiet re-run would never report them.+ if report.changed() {+ cg_refresh_calendar(+ &ctx.connection_gateway_client,+ &ctx.db,+ link.macro_id.as_ref(),+ link.id,+ )+ .await;+ }++ match run_result {+ Ok(()) => Disposition::Ack,+ Err(error) => {+ let retryable = matches!(+ error,+ GoogleCalendarBackfillRunError::Busy+ | GoogleCalendarBackfillRunError::LeaseLost+ | GoogleCalendarBackfillRunError::Retryable(_)+ );+ if retryable {+ Disposition::Retry(anyhow::Error::new(error))+ } else {+ // The coordinator already recorded this post-lease failure; ack.+ tracing::error!(error = ?error, %calendar_job_id, "non-retryable Google Calendar backfill failure");+ Disposition::Ack+ }+ }+ }+}++/// Terminate an unclaimed job whose token fetch or link lookup failed before a+/// lease could be claimed. Returns [`Disposition::Retry`] when the terminal+/// state cannot be persisted, so the delivery redelivers rather than acking a+/// job that was left non-terminal.+async fn fail_unclaimed(+ ctx: &CalendarBackfillContext,+ link_id: Uuid,+ calendar_job_id: Uuid,+ disposition: CalendarBackfillFailureDisposition,+ message: &str,+) -> Disposition {+ match ctx+ .failure+ .fail_unclaimed(+ CalendarBackfillJobKey {+ job_id: calendar_job_id,+ email_link_id: link_id,+ },+ disposition,+ message,+ )+ .await+ {+ Ok(_) => Disposition::Ack,+ Err(error) => {+ tracing::error!(error = ?error, %calendar_job_id, "failed to record terminal calendar backfill failure");+ Disposition::Retry(anyhow::anyhow!(+ "failed to persist terminal calendar backfill failure: {error:?}"+ ))+ }+ }+}++fn is_reauth_required_error(error: &anyhow::Error) -> bool {+ error.chain().any(|cause| {+ cause+ .downcast_ref::<AuthServiceClientError>()+ .is_some_and(|cause| {+ matches!(+ cause,+ AuthServiceClientError::Forbidden | AuthServiceClientError::NotFound+ )+ })+ })+}++fn extract_message(+ message: &aws_sdk_sqs::types::Message,+) -> anyhow::Result<CalendarBackfillMessage> {+ let body = message+ .body()+ .ok_or_else(|| anyhow::anyhow!("message body not found"))?;+ serde_json::from_str(body).map_err(|error| {+ anyhow::anyhow!("failed to deserialize calendar backfill message: {error}")+ })+}diff --git a/services/calendar_service/src/calendar_backfill_adapters.rs b/services/calendar_service/src/calendar_backfill_adapters.rsnew file mode 100644index 0000000000..94eb8b9419--- /dev/null+++ b/services/calendar_service/src/calendar_backfill_adapters.rs@@ -0,0 +1,32 @@+//! Process-level adapters backing the calendar application services.++use calendar_events::domain::ports::{GoogleProviderError, GoogleProviderErrorKind};+use calendar_events::outbound::google::GoogleRequestGate;+use uuid::Uuid;++use crate::calendar_ratelimit::CalendarRateLimiter;++/// Enforces the per-inbox Google Calendar API quota before each request.+#[derive(Clone)]+pub struct RedisCalendarRequestGate {+ limiter: CalendarRateLimiter,+}++impl RedisCalendarRequestGate {+ /// Construct the gate over the process-level Redis rate limiter.+ pub fn new(limiter: CalendarRateLimiter) -> Self {+ Self { limiter }+ }+}++impl GoogleRequestGate for RedisCalendarRequestGate {+ async fn acquire(&self, email_link_id: Uuid) -> Result<(), GoogleProviderError> {+ if self.limiter.is_calendar_rate_limited(email_link_id).await {+ return Err(GoogleProviderError::new(+ GoogleProviderErrorKind::Transient,+ "Google Calendar API rate limit reached for this inbox",+ ));+ }+ Ok(())+ }+}diff --git a/services/calendar_service/src/calendar_outbox.rs b/services/calendar_service/src/calendar_outbox.rsnew file mode 100644index 0000000000..bcc8533b48--- /dev/null+++ b/services/calendar_service/src/calendar_outbox.rs@@ -0,0 +1,154 @@+//! Durable calendar backfill publication.++use crate::backfill_queue::{CalendarBackfillMessage, CalendarBackfillQueueClient};+use calendar_events::domain::{+ ports::GoogleCalendarSyncRepository, service::GoogleCalendarSyncScheduler,+};+use chrono::Utc;+use sqlx::PgPool;+use uuid::Uuid;++const BATCH_SIZE: usize = 50;++struct OutboxRow {+ id: Uuid,+ backfill_job_id: Uuid,+ email_link_id: Uuid,+ kind: String,+}++/// Continuously schedule due syncs, reap wedged jobs, and publish calendar+/// outbox rows to calendar's own backfill queue.+///+/// A row lock is held through each SQS publish. A crash after publish but+/// before commit can duplicate a message, so every consumer remains+/// idempotent by calendar job id.+#[tracing::instrument(skip(db, queue, scheduler))]+pub async fn run<R>(+ db: PgPool,+ queue: CalendarBackfillQueueClient,+ scheduler: GoogleCalendarSyncScheduler<R>,+ calendar_sync_enabled: bool,+ cancellation_token: tokio_util::sync::CancellationToken,+) where+ R: GoogleCalendarSyncRepository,+{+ loop {+ if cancellation_token.is_cancelled() {+ return;+ }+ // With calendar sync disabled, outbox rows keep accumulating+ // unpublished and due syncs never re-arm — enabling the flag later+ // resumes everything without a migration.+ if calendar_sync_enabled {+ scheduler+ .run_once(Utc::now())+ .await+ .inspect_err(|error| {+ tracing::error!(error=?error, "failed to schedule due Google Calendar syncs");+ })+ .ok();+ scheduler+ .reap_once(Utc::now())+ .await+ .inspect_err(|error| {+ tracing::error!(error=?error, "failed to reap wedged Google Calendar syncs");+ })+ .ok();+ drain_calendar(&db, &queue)+ .await+ .inspect_err(|error| {+ tracing::error!(error = ?error, "failed to publish calendar backfill outbox");+ })+ .ok();+ }+ tokio::select! {+ _ = tokio::time::sleep(std::time::Duration::from_secs(5)) => {}+ _ = cancellation_token.cancelled() => return,+ }+ }+}++/// Return a calendar backfill delivery to the outbox so the drain republishes+/// it once calendar sync is enabled again. Deliveries received while the+/// switch is off would otherwise be acked and lost.+#[tracing::instrument(skip(db), err)]+pub async fn republish_calendar_job(db: &PgPool, backfill_job_id: Uuid) -> anyhow::Result<()> {+ sqlx::query!(+ r#"+ UPDATE calendar_sync_outbox+ SET published_at = NULL+ WHERE backfill_job_id = $1+ "#,+ backfill_job_id,+ )+ .execute(db)+ .await?;+ Ok(())+}++#[tracing::instrument(skip(db, queue), err)]+async fn drain_calendar(db: &PgPool, queue: &CalendarBackfillQueueClient) -> anyhow::Result<usize> {+ let mut published = 0;+ for _ in 0..BATCH_SIZE {+ let mut tx = db.begin().await?;+ let row = sqlx::query_as!(+ OutboxRow,+ r#"+ SELECT+ outbox.id,+ outbox.backfill_job_id,+ job.email_link_id,+ job.kind+ FROM calendar_sync_outbox outbox+ JOIN calendar_backfill_jobs job ON job.id = outbox.backfill_job_id+ WHERE outbox.published_at IS NULL+ ORDER BY outbox.created_at, outbox.id+ FOR UPDATE OF outbox SKIP LOCKED+ LIMIT 1+ "#,+ )+ .fetch_optional(&mut *tx)+ .await?;++ let Some(row) = row else {+ tx.commit().await?;+ break;+ };++ match to_queue_message(&row) {+ Ok(message) => {+ queue.enqueue(&message).await?;+ published += 1;+ }+ Err(error) => {+ // An unmappable row must still be marked published: the drain+ // always selects the oldest unpublished row, so leaving it+ // would wedge every calendar row behind it forever.+ tracing::error!(error = ?error, outbox_id = %row.id, "skipping unmappable calendar outbox row");+ }+ }+ sqlx::query!(+ r#"+ UPDATE calendar_sync_outbox+ SET published_at = now()+ WHERE id = $1+ "#,+ row.id,+ )+ .execute(&mut *tx)+ .await?;+ tx.commit().await?;+ }+ Ok(published)+}++fn to_queue_message(row: &OutboxRow) -> anyhow::Result<CalendarBackfillMessage> {+ match row.kind.as_str() {+ "google_calendar" => Ok(CalendarBackfillMessage::GoogleCalendar {+ link_id: row.email_link_id,+ calendar_job_id: row.backfill_job_id,+ }),+ kind => anyhow::bail!("unsupported calendar backfill kind: {kind}"),+ }+}diff --git a/services/calendar_service/src/calendar_ratelimit.rs b/services/calendar_service/src/calendar_ratelimit.rsnew file mode 100644index 0000000000..3196199b0b--- /dev/null+++ b/services/calendar_service/src/calendar_ratelimit.rs@@ -0,0 +1,117 @@+//! Redis-backed Google Calendar API quota gate.++use redis::Script;+use std::time::{SystemTime, UNIX_EPOCH};+use uuid::Uuid;++/// Flat Google Calendar per-inbox query budget for the sliding window.+const CALENDAR_RATE_LIMIT_QUERIES: u32 = 300;++/// Redis client holding the sliding-window parameters for calendar rate limiting.+#[derive(Clone)]+pub struct CalendarRateLimiter {+ inner: redis::Client,+ /// Duration of the sliding window in seconds.+ window_secs: u32,+}++impl CalendarRateLimiter {+ /// Construct the limiter over a Redis client and window duration.+ pub fn new(inner: redis::Client, window_secs: u32) -> Self {+ Self { inner, window_secs }+ }++ /// Checks if a Google Calendar API request is rate-limited, using an+ /// atomic sliding-window Lua script with a calendar-specific key and+ /// Calendar's flat one-query-per-request cost.+ pub async fn is_calendar_rate_limited(&self, email_link_id: Uuid) -> bool {+ let mut con = match self.inner.get_multiplexed_async_connection().await {+ Ok(conn) => conn,+ Err(e) => {+ tracing::warn!(+ "Failed to get Redis connection for calendar rate limiting link {}: {}",+ email_link_id,+ e+ );+ return false;+ }+ };++ let redis_key = format!("calendar-ratelimit:log:{}", email_link_id);+ let now_micros = SystemTime::now()+ .duration_since(UNIX_EPOCH)+ .expect("Time went backwards")+ .as_micros() as u64;+ let cost = 1u32;+ let member_id = format!("{}:{}", cost, Uuid::new_v4());+ let script = Script::new(get_rate_limit_script_with_usage());++ let (is_limited, _current_units): (i32, u32) = match script+ .key(&redis_key)+ .arg(CALENDAR_RATE_LIMIT_QUERIES)+ .arg(self.window_secs * 1_000_000)+ .arg(now_micros)+ .arg(cost)+ .arg(&member_id)+ .invoke_async(&mut con)+ .await+ {+ Ok(res) => res,+ Err(e) => {+ tracing::warn!(+ "Failed to execute calendar rate limit script for link {}: {}",+ email_link_id,+ e+ );+ return false;+ }+ };++ is_limited == 1+ }+}++/// Returns the raw Lua script for an atomic, cost-based sliding window rate limiter.+///+/// The script is executed atomically on the Redis server. It tracks the+/// cumulative cost (quota units) of requests within a sliding time window and+/// returns both the rate limit decision and the current usage.+fn get_rate_limit_script_with_usage() -> &'static str {+ r#"+ local key = KEYS[1]+ local limit_units = tonumber(ARGV[1])+ local window_micros = tonumber(ARGV[2])+ local now_micros = tonumber(ARGV[3])+ local new_request_units = tonumber(ARGV[4])+ local new_member = ARGV[5]++ local window_start = now_micros - window_micros++ -- Step 1: Cleanup old entries+ redis.call('ZREMRANGEBYSCORE', key, '-inf', window_start)++ -- Step 2: Calculate current usage+ local members = redis.call('ZRANGE', key, 0, -1)+ local current_units = 0+ for _, member in ipairs(members) do+ local cost = tonumber(string.match(member, "^(%d+):"))+ if cost then+ current_units = current_units + cost+ end+ end++ -- Step 3: Check limit and return a table with [is_limited, unit_count]+ if (current_units + new_request_units) > limit_units then+ -- DENIED: Return 1 and the current unit count (without adding the new request)+ return {1, current_units}+ else+ -- ALLOWED: Add the new request...+ redis.call('ZADD', key, now_micros, new_member)+ redis.call('EXPIRE', key, (window_micros / 1000000) * 2)++ -- ...and return 0 with the new total unit count+ local new_total = current_units + new_request_units+ return {0, new_total}+ end+ "#+}diff --git a/services/calendar_service/src/calendar_refresh.rs b/services/calendar_service/src/calendar_refresh.rsnew file mode 100644index 0000000000..9033434f82--- /dev/null+++ b/services/calendar_service/src/calendar_refresh.rs@@ -0,0 +1,38 @@+//! Connection-gateway adapter that nudges calendar viewers after mutations.++use calendar_events::domain::ports::CalendarRefreshNotifier;+use connection_gateway_client::client::ConnectionGatewayClient;+use uuid::Uuid;++use crate::pubsub_util::cg_refresh_calendar;++/// [`CalendarRefreshNotifier`] backed by the connection gateway: fans one+/// mutation's refresh nudge out to the link owner and its delegates, exactly+/// like the backfill-completion nudge, so open tabs refetch calendar+/// projections without waiting for a provider sync echo.+#[derive(Clone)]+pub struct ConnectionGatewayCalendarRefresh {+ client: ConnectionGatewayClient,+ db: sqlx::PgPool,+}++impl ConnectionGatewayCalendarRefresh {+ /// Construct the adapter.+ pub fn new(client: ConnectionGatewayClient, db: sqlx::PgPool) -> Self {+ Self { client, db }+ }+}++impl CalendarRefreshNotifier for ConnectionGatewayCalendarRefresh {+ async fn calendar_changed(&self, owner_id: &str, email_link_id: Uuid) {+ // Spawned so the mutation response never waits on the gateway: the+ // client has no request timeout and the fan-out posts serially per+ // recipient, while the write this announces is already committed.+ let client = self.client.clone();+ let db = self.db.clone();+ let owner_id = owner_id.to_string();+ tokio::spawn(async move {+ cg_refresh_calendar(&client, &db, &owner_id, email_link_id).await;+ });+ }+}diff --git a/services/calendar_service/src/calendar_tokens.rs b/services/calendar_service/src/calendar_tokens.rsnew file mode 100644index 0000000000..103714d0d0--- /dev/null+++ b/services/calendar_service/src/calendar_tokens.rs@@ -0,0 +1,65 @@+//! Access-token adapter for user-initiated calendar mutations.++use authentication_service_client::{AuthServiceClient, error::AuthServiceClientError};+use calendar_events::domain::{+ models::CalendarLinkTokenIdentity,+ ports::{CalendarAccessTokenProvider, CalendarTokenError},+};+use email_utils::token_cache_key::TokenCacheKey;+use google_token::fetch_gmail_access_token;+use redis::aio::MultiplexedConnection;+use std::sync::Arc;++/// Mints Google access tokens for calendar mutations through the same+/// Redis-cached auth-service path Gmail requests use.+#[derive(Clone)]+pub struct CalendarTokenProviderAdapter {+ redis_conn: MultiplexedConnection,+ auth_service_client: Arc<AuthServiceClient>,+}++impl CalendarTokenProviderAdapter {+ /// Construct the adapter from the shared Redis connection and auth client.+ pub fn new(+ redis_conn: MultiplexedConnection,+ auth_service_client: Arc<AuthServiceClient>,+ ) -> Self {+ Self {+ redis_conn,+ auth_service_client,+ }+ }+}++impl CalendarAccessTokenProvider for CalendarTokenProviderAdapter {+ async fn fetch_access_token(+ &self,+ identity: &CalendarLinkTokenIdentity,+ ) -> Result<String, CalendarTokenError> {+ let key = TokenCacheKey::new(+ &identity.fusionauth_user_id,+ &identity.email_address,+ &identity.provider,+ );+ fetch_gmail_access_token(&key, &self.redis_conn, &self.auth_service_client)+ .await+ .map_err(|error| {+ let reauth = error.chain().any(|cause| {+ cause+ .downcast_ref::<AuthServiceClientError>()+ .is_some_and(|cause| {+ matches!(+ cause,+ AuthServiceClientError::Forbidden+ | AuthServiceClientError::NotFound+ )+ })+ });+ if reauth {+ CalendarTokenError::ReauthRequired(format!("{error:?}"))+ } else {+ CalendarTokenError::Transient(format!("{error:?}"))+ }+ })+ }+}diff --git a/services/calendar_service/src/config.rs b/services/calendar_service/src/config.rsnew file mode 100644index 0000000000..55a473ddd0--- /dev/null+++ b/services/calendar_service/src/config.rs@@ -0,0 +1,90 @@+use anyhow::Context;+use calendar_events::domain::models::GoogleWatchConfig;+use macro_auth::InternalApiKey;+pub use macro_env::Environment;+use macro_env_var::env_vars;+use secretsmanager_client::LocalOrRemoteSecret;++/// Push notification channels are opened only when both optional watch+/// variables are configured; without them the periodic poll is the sole+/// freshness mechanism.+pub fn calendar_watch_config() -> Option<GoogleWatchConfig> {+ // A variable set to an empty string must count as unset: a blank token+ // would verify blank-header webhook requests.+ let read = |name| {+ macro_env_var::maybe_read_env(name)+ .map(|value| value.trim().to_owned())+ .filter(|value| !value.is_empty())+ };+ let address = read("CALENDAR_WATCH_WEBHOOK_URL")?;+ let token = read("CALENDAR_WATCH_TOKEN")?;+ Some(GoogleWatchConfig { address, token })+}++env_vars! {+ pub struct KafkaBrokers;+ pub struct MacroDbUrl;+ pub struct RedisUri;+ pub struct AuthenticationServiceSecretKey;+}++#[derive(macro_config::MacroConfig)]+#[serde(rename_all = "SCREAMING_SNAKE_CASE")]+pub struct Config {+ /// The connection URL for the macrodb instance this application should use.+ /// For deployed applications, this is a secret stored in AWS Secrets Manager.+ pub macro_db_url: MacroDbUrl,++ /// The port to listen for HTTP requests on.+ #[macro_config_default(8080)]+ pub port: usize,++ /// The Redis URI for the Redis this application should use.+ pub redis_uri: RedisUri,++ /// Comma-separated Kafka bootstrap servers for the macro event broker.+ pub kafka_brokers: KafkaBrokers,++ /// The environment we are in+ #[macro_config_default(Environment::new_or_prod())]+ pub environment: Environment,++ /// Master switch for calendar sync. When `false` (the default) the sync+ /// scheduler stays parked, the calendar outbox stops publishing, and+ /// calendar backfill deliveries requeue their outbox rows. Enabling it+ /// later resumes everything without a migration.+ #[macro_config_default(false)]+ pub calendar_sync_enabled: bool,++ /// Auth service secret key, used for internal access+ pub authentication_service_secret_key: LocalOrRemoteSecret<AuthenticationServiceSecretKey>,++ /// The internal api key+ pub internal_api_key: InternalApiKey,++ /// The number of workers we spawn for the calendar backfill queue+ #[macro_config_default(25)]+ pub backfill_queue_workers: i32,++ /// The queue max messages per poll for the calendar backfill queue+ #[macro_config_default(1)]+ pub backfill_queue_max_messages: i32,++ /// The queue max messages per poll+ #[macro_config_default(10)]+ pub queue_max_messages: i32,++ /// The queue wait time seconds+ #[macro_config_default(20)]+ pub queue_wait_time_seconds: i32,++ /// The size of the sliding window we use for Google Calendar rate limiting.+ #[macro_config_default(60)]+ pub redis_rate_limit_window_secs: u32,+}++impl Config {+ pub fn from_env() -> anyhow::Result<Self> {+ macro_config::ConfigLoader::load::<Config>().context("failed to load config")+ }+}diff --git a/services/calendar_service/src/health.rs b/services/calendar_service/src/health.rsnew file mode 100644index 0000000000..c3a4c82dbe--- /dev/null+++ b/services/calendar_service/src/health.rs@@ -0,0 +1,18 @@+use axum::{Router, routing::get};++/// Health check+#[utoipa::path(+ get,+ path = "/health",+ responses(+ (status = 200, description = "health", body = String),+ )+ )]+pub async fn health_handler() -> String {+ "healthy".to_string()+}++/// Build the health router.+pub fn router() -> Router {+ Router::new().route("/health", get(health_handler))+}diff --git a/services/calendar_service/src/lib.rs b/services/calendar_service/src/lib.rsnew file mode 100644index 0000000000..bd2d197f4b--- /dev/null+++ b/services/calendar_service/src/lib.rs@@ -0,0 +1,29 @@+#![recursion_limit = "256"]+//! Calendar sync service.+//!+//! Hosts the Google Calendar push webhook, user-initiated calendar mutations,+//! the sync scheduler + outbox drain, and the backfill queue consumer, so the+//! calendar sync deploy/ingress lifecycle is independent of email_service.++/// HTTP surface: the push webhook and calendar mutation routes.+pub mod api;+/// Calendar's own backfill queue: message type and SQS enqueue client.+pub mod backfill_queue;+/// Calendar backfill queue consumer.+pub mod calendar_backfill;+/// Process-level adapters backing the calendar application services.+pub mod calendar_backfill_adapters;+/// Sync scheduler and durable backfill publication.+pub mod calendar_outbox;+/// Redis-backed Google Calendar API quota gate.+pub mod calendar_ratelimit;+/// Connection-gateway calendar refresh notifier.+pub mod calendar_refresh;+/// Access-token adapter for user-initiated calendar mutations.+pub mod calendar_tokens;+/// Service configuration.+pub mod config;+/// Health check router.+pub mod health;+/// Shared calendar pubsub helpers.+pub mod pubsub_util;diff --git a/services/calendar_service/src/main.rs b/services/calendar_service/src/main.rsnew file mode 100644index 0000000000..20435888c2--- /dev/null+++ b/services/calendar_service/src/main.rs@@ -0,0 +1,206 @@+#![recursion_limit = "256"]+use std::sync::Arc;+use std::time::Duration;++use anyhow::Context;+use calendar_events::{+ domain::{mutations::CalendarMutationServiceImpl, service::CalendarService},+ outbound::{google::GoogleCalendarClient, pg::PgCalendarRepository},+};+use calendar_service::api::context::{ApiContext, AuthorizationService};+use calendar_service::backfill_queue::CalendarBackfillQueueClient;+use calendar_service::calendar_backfill::CalendarBackfillContext;+use calendar_service::calendar_backfill_adapters::RedisCalendarRequestGate;+use calendar_service::calendar_ratelimit::CalendarRateLimiter;+use calendar_service::calendar_refresh::ConnectionGatewayCalendarRefresh;+use calendar_service::calendar_tokens::CalendarTokenProviderAdapter;+use calendar_service::config::{Config, calendar_watch_config};+use macro_auth::middleware::decode_jwt::JwtValidationArgs;+use macro_authorization::{+ InternalAuthConfig, MacroAuthJwtValidator, MacroAuthorizationState,+ PgUserApiKeyAuthorizationRepo, PgUserApiKeyAuthorizer,+};+use macro_entrypoint::MacroEntrypoint;+use macro_env::Environment;+use macro_event_broker::{KafkaEventPublisher, MacroEventBrokerService};+use macro_service_urls::{AuthServiceUrl, ConnectionGatewayUrl};+use sqlx::postgres::PgPoolOptions;+use tokio_util::{sync::CancellationToken, task::TaskTracker};++const EVENT_BROKER_DRAIN_TIMEOUT: Duration = Duration::from_secs(10);++#[tokio::main]+#[tracing::instrument(err)]+async fn main() -> anyhow::Result<()> {+ MacroEntrypoint::default().init();+ let env = Environment::new_or_prod();++ let aws_config = macro_aws_config::get_macro_aws_config().await;+ let secretsmanager_client = secretsmanager_client::SecretsManager::new(+ aws_sdk_secretsmanager::Client::new(&aws_config),+ );++ let config = Config::from_env()+ .context("expected to be able to generate config")?+ .resolve_remote_secrets(env, &secretsmanager_client)+ .await+ .context("expected to be able to resolve config secrets")?;++ let (min_connections, max_connections): (u32, u32) = match config.environment {+ Environment::Production => (3, 20),+ Environment::Develop => (1, 10),+ Environment::Local => (1, 10),+ };++ let db = PgPoolOptions::new()+ .min_connections(min_connections)+ .max_connections(max_connections)+ .connect(&config.macro_db_url)+ .await+ .context("could not connect to db")?;++ let redis_inner_client = redis::Client::open(config.redis_uri.as_ref())+ .inspect(|client| {+ client+ .get_connection()+ .map(|_| tracing::info!("initialized redis connection"))+ .inspect_err(|e| {+ tracing::error!(error=?e, "failed to connect to redis");+ })+ .ok();+ })+ .context("failed to connect to redis")?;++ // One long-lived multiplexed connection shared by the token adapters;+ // cloning it is an Arc bump, not a new TCP dial per provider call.+ let redis_conn = redis_inner_client+ .get_multiplexed_async_connection()+ .await+ .context("failed to get multiplexed redis connection for token sources")?;++ let auth_service_client = authentication_service_client::AuthServiceClient::new(+ config+ .authentication_service_secret_key+ .as_ref()+ .to_string(),+ AuthServiceUrl::new()?.to_string(),+ );++ let connection_gateway_client = connection_gateway_client::client::ConnectionGatewayClient::new(+ config.internal_api_key.to_string(),+ ConnectionGatewayUrl::new()?.to_string(),+ );++ let event_broker_tracker = TaskTracker::new();+ let macro_event_broker = MacroEventBrokerService::new(+ KafkaEventPublisher::new(config.kafka_brokers.as_ref())+ .context("failed to create kafka event publisher")?,+ event_broker_tracker.clone(),+ );++ let worker_cancellation_token = CancellationToken::new();+ let worker_tracker = TaskTracker::new();++ let backfill_queue = macro_queues::CalendarServiceBackfillQueue::new();+ let backfill_queue_client = CalendarBackfillQueueClient::new(+ aws_sdk_sqs::Client::new(&aws_config),+ backfill_queue.to_string(),+ );+ let rate_limiter =+ CalendarRateLimiter::new(redis_inner_client, config.redis_rate_limit_window_secs);++ // Sync scheduler + calendar outbox drain.+ worker_tracker.spawn(calendar_service::calendar_outbox::run(+ db.clone(),+ backfill_queue_client,+ calendar_events::domain::service::GoogleCalendarSyncScheduler::new(+ PgCalendarRepository::new(db.clone()),+ ),+ config.calendar_sync_enabled,+ worker_cancellation_token.clone(),+ ));++ // Calendar backfill queue consumers.+ for _ in 0..config.backfill_queue_workers {+ let worker = sqs_worker::SQSWorker::new(+ aws_sdk_sqs::Client::new(&aws_config),+ backfill_queue.to_string(),+ config.backfill_queue_max_messages,+ config.queue_wait_time_seconds,+ );+ let ctx = CalendarBackfillContext::new(+ db.clone(),+ worker.clone(),+ rate_limiter.clone(),+ redis_conn.clone(),+ auth_service_client.clone(),+ connection_gateway_client.clone(),+ macro_event_broker.clone(),+ calendar_watch_config(),+ config.calendar_sync_enabled,+ );+ let cancellation_token = worker_cancellation_token.clone();+ worker_tracker.spawn(async move {+ calendar_service::calendar_backfill::run_worker(ctx, worker, cancellation_token).await;+ });+ }+ tracing::info!(+ num_workers = config.backfill_queue_workers,+ "calendar backfill workers started"+ );++ let jwt_args =+ JwtValidationArgs::new_with_secret_manager(config.environment, &secretsmanager_client)+ .await?;+ let authorization_state = MacroAuthorizationState::new(Arc::new(AuthorizationService::new(+ MacroAuthJwtValidator::new(jwt_args),+ InternalAuthConfig {+ api_key: config.internal_api_key.to_string(),+ default_user_id: None,+ },+ macro_authorization::NoBotAuthorizer,+ PgUserApiKeyAuthorizer::new(PgUserApiKeyAuthorizationRepo::new(db.clone())),+ )));++ let calendar_service = Arc::new(CalendarService::new(PgCalendarRepository::new(db.clone())));+ let calendar_mutation_service = Arc::new(CalendarMutationServiceImpl::new(+ PgCalendarRepository::new(db.clone()),+ GoogleCalendarClient::with_gate(+ reqwest::Client::builder()+ .timeout(Duration::from_secs(30))+ .build()+ .context("failed to build the google calendar mutation http client")?,+ RedisCalendarRequestGate::new(rate_limiter),+ ),+ CalendarTokenProviderAdapter::new(redis_conn, Arc::new(auth_service_client)),+ macro_event_broker.clone(),+ ConnectionGatewayCalendarRefresh::new(connection_gateway_client, db.clone()),+ ));++ let api_result = calendar_service::api::setup_and_serve(ApiContext {+ config: Arc::new(config),+ authorization_state,+ calendar_service,+ calendar_mutation_service,+ })+ .await;++ worker_cancellation_token.cancel();+ worker_tracker.close();+ tracing::info!("waiting for calendar workers to stop");+ worker_tracker.wait().await;+ tracing::info!("calendar workers stopped");++ event_broker_tracker.close();+ tracing::info!("waiting for event broker publishes to drain");+ match tokio::time::timeout(EVENT_BROKER_DRAIN_TIMEOUT, event_broker_tracker.wait()).await {+ Ok(()) => tracing::info!("event broker publishes drained"),+ Err(error) => tracing::warn!(+ error = ?error,+ timeout_seconds = EVENT_BROKER_DRAIN_TIMEOUT.as_secs(),+ "timed out waiting for event broker publishes to drain"+ ),+ }++ api_result+}diff --git a/services/calendar_service/src/openapi.rs b/services/calendar_service/src/openapi.rsnew file mode 100644index 0000000000..3d12e23367--- /dev/null+++ b/services/calendar_service/src/openapi.rs@@ -0,0 +1,6 @@+use calendar_service::api::swagger::ApiDoc;+use utoipa::OpenApi;++fn main() {+ println!("{}", ApiDoc::openapi().to_pretty_json().unwrap());+}diff --git a/services/calendar_service/src/pubsub_util.rs b/services/calendar_service/src/pubsub_util.rsnew file mode 100644index 0000000000..05a975debb--- /dev/null+++ b/services/calendar_service/src/pubsub_util.rs@@ -0,0 +1,45 @@+//! Shared calendar pubsub helpers.++use connection_gateway_client::client::ConnectionGatewayClient;++/// Signal every viewer of a connected inbox's calendars — the link owner+/// plus its delegated users — that the calendar projection changed. Best+/// effort: an inactive or unreachable viewer only misses a refresh nudge.+#[tracing::instrument(skip(client, db), level = "debug")]+pub async fn cg_refresh_calendar(+ client: &ConnectionGatewayClient,+ db: &sqlx::PgPool,+ owner_macro_id: &str,+ link_id: uuid::Uuid,+) {+ if !cfg!(feature = "connection_gateway") {+ return;+ }+ let payload = serde_json::to_value(+ calendar_events::domain::models::RefreshCalendarEvent::Synced { link_id },+ )+ .unwrap_or_default();+ let mut recipients = vec![owner_macro_id.to_string()];+ match sqlx::query_scalar!(+ "SELECT primary_macro_id FROM macro_user_links WHERE link_id = $1",+ link_id,+ )+ .fetch_all(db)+ .await+ {+ Ok(delegates) => recipients.extend(delegates),+ Err(error) => {+ tracing::warn!(error=?error, %link_id, "failed to resolve calendar refresh delegates");+ }+ }+ recipients.sort();+ recipients.dedup();+ for macro_id in recipients {+ let _ = client+ .refresh_calendar(¯o_id, payload.clone())+ .await+ .inspect_err(+ |e| tracing::error!(macro_id = %macro_id, "Failed to refresh calendar: {e}"),+ );+ }+}diff --git a/services/email_service/Cargo.toml b/services/email_service/Cargo.tomlindex c5509896ee..0c762cd515 100644--- a/services/email_service/Cargo.toml+++ b/services/email_service/Cargo.toml@@ -107,6 +107,7 @@ filter_ast = { path = "../../crates/filter_ast" }frecency = { path = "../../crates/frecency", features = ["postgres"] }futures = { workspace = true }gmail_client = { path = "../../crates/gmail_client" }+google_token = { path = "../../crates/google_token" }html-escape = { workspace = true }http-body-util = { workspace = true }infer = "0.19.0"diff --git a/services/email_service/src/calendar_tokens.rs b/services/email_service/src/calendar_tokens.rsindex cb9d156883..103714d0d0 100644--- a/services/email_service/src/calendar_tokens.rs+++ b/services/email_service/src/calendar_tokens.rs@@ -5,8 +5,8 @@ use calendar_events::domain::{models::CalendarLinkTokenIdentity,ports::{CalendarAccessTokenProvider, CalendarTokenError},};-use email::outbound::fetch_gmail_access_token;use email_utils::token_cache_key::TokenCacheKey;+use google_token::fetch_gmail_access_token;use redis::aio::MultiplexedConnection;use std::sync::Arc;diff --git a/services/email_service/src/outbound/email_api/token_source.rs b/services/email_service/src/outbound/email_api/token_source.rsindex 8d4ea7fbcd..e8afa25dd4 100644--- a/services/email_service/src/outbound/email_api/token_source.rs+++ b/services/email_service/src/outbound/email_api/token_source.rs@@ -124,7 +124,7 @@ impl EmailServiceTokenSource {match freshness {TokenFreshness::Cached => {- email::outbound::fetch_gmail_access_token(+ google_token::fetch_gmail_access_token(&key,&self.redis_conn,&self.auth_service_client,@@ -132,7 +132,7 @@ impl EmailServiceTokenSource {.await}TokenFreshness::Fresh => {- email::outbound::fetch_gmail_access_token_no_cache(+ google_token::fetch_gmail_access_token_no_cache(&key,&self.redis_conn,&self.auth_service_client,diff --git a/tooling/xtask/crates/xtask_nextest_filter/determinator.toml b/tooling/xtask/crates/xtask_nextest_filter/determinator.tomlindex f47e2406ae..459ade4d86 100644--- a/tooling/xtask/crates/xtask_nextest_filter/determinator.toml+++ b/tooling/xtask/crates/xtask_nextest_filter/determinator.toml@@ -8,5 +8,6 @@ mark-changed = ["complete_graph","documents","seed_cli",+ "sync_service","xtask_workflows",]