mirror of
https://github.com/windmill-labs/windmill.git
synced 2026-09-08 00:03:07 +00:00
* refactor: extract windmill-api into 4 subcrates (api-auth, store, api-sse, api-jobs) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: eliminate refresh_token OnceLock bridge in windmill-store Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: eliminate FromRequestParts OnceLock bridge in windmill-api-auth Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: wire subcrates into workspace and clean up unused re-exports Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * fix: resolve cargo check --all-features errors in subcrate wiring Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * sqlx * all * chore: update ee-repo-ref for warning fixes Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * refactor: extract windmill-trigger crate and expand windmill-api-jobs Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: extract windmill-trigger-kafka crate from windmill-api Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: extract windmill-trigger-postgres crate from windmill-api Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: extract windmill-trigger-websocket and windmill-trigger-mqtt crates Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: extract windmill-trigger-nats, sqs, gcp, and email crates Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: extract windmill-trigger-http crate from windmill-api Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: move token creation and permission helpers to windmill-api-auth Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * refactor: extract windmill-native-triggers crate from windmill-api Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> * sqlx * all * refactor: extract windmill-api-embeddings crate and fix CI warnings Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: resolve type mismatch in oauth2_oss and remaining warnings Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * fix: use correct HTTP_CLIENT config in embeddings crate (30s timeout, cert override) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * all * fix: gate oauth_refresh_ee on oauth2 feature to fix warnings Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> * all --------- Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
67 lines
1.9 KiB
Rust
67 lines
1.9 KiB
Rust
/*
|
|
* Author: Windmill Labs
|
|
* Copyright: Windmill Labs, Inc 2024
|
|
* This file and its contents are licensed under the AGPLv3 License.
|
|
* Please see the included NOTICE for copyright information and
|
|
* LICENSE-AGPL for a copy of the license.
|
|
*/
|
|
|
|
//! Polling-based event notification system.
|
|
//!
|
|
//! This module provides a table-based alternative to PostgreSQL LISTEN/NOTIFY
|
|
//! for propagating cache invalidation and setting change events across
|
|
//! workers and servers.
|
|
|
|
use sqlx::{FromRow, Pool, Postgres};
|
|
|
|
use crate::error::Error;
|
|
|
|
#[derive(Debug, Clone, FromRow)]
|
|
pub struct NotifyEvent {
|
|
pub id: i64,
|
|
pub channel: String,
|
|
pub payload: String,
|
|
}
|
|
|
|
/// Fetch all events with id greater than `last_event_id`.
|
|
/// Returns events ordered by id ascending.
|
|
pub async fn poll_notify_events(
|
|
db: &Pool<Postgres>,
|
|
last_event_id: i64,
|
|
) -> Result<Vec<NotifyEvent>, Error> {
|
|
let events = sqlx::query_as::<_, NotifyEvent>(
|
|
"SELECT id, channel, payload FROM notify_event WHERE id > $1 ORDER BY id LIMIT 1000",
|
|
)
|
|
.bind(last_event_id)
|
|
.fetch_all(db)
|
|
.await?;
|
|
|
|
Ok(events)
|
|
}
|
|
|
|
/// Get the current maximum event id.
|
|
/// Used to initialize last_event_id on startup to avoid processing old events.
|
|
pub async fn get_latest_event_id(db: &Pool<Postgres>) -> Result<i64, Error> {
|
|
let result: (i64,) = sqlx::query_as("SELECT COALESCE(MAX(id), 0) FROM notify_event")
|
|
.fetch_one(db)
|
|
.await?;
|
|
|
|
Ok(result.0)
|
|
}
|
|
|
|
/// Delete events older than the specified number of minutes.
|
|
/// Returns the number of deleted rows.
|
|
pub async fn cleanup_old_events(
|
|
db: &Pool<Postgres>,
|
|
older_than_minutes: i32,
|
|
) -> Result<u64, Error> {
|
|
let result = sqlx::query(
|
|
"DELETE FROM notify_event WHERE created_at < now() - make_interval(mins => $1)",
|
|
)
|
|
.bind(older_than_minutes)
|
|
.execute(db)
|
|
.await?;
|
|
|
|
Ok(result.rows_affected())
|
|
}
|