Skip to content

Commit 4bc15e2

Browse files
committed
fix: lazy Postgres connect so the daemon handshake never blocks (v0.1.1)
connect_lazy + background migrate — a cold DB dial on (re)spawn no longer makes the process miss the initialize handshake ('plugin connection lost').
1 parent b6fc24b commit 4bc15e2

2 files changed

Lines changed: 16 additions & 5 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "animus-queue-postgres"
3-
version = "0.1.0"
3+
version = "0.1.1"
44
edition = "2021"
55
license = "Elastic-2.0"
66
description = "Durable Postgres-backed queue plugin for Animus — survives daemon restarts/redeploys and reclaims crashed leases. Wire-compatible with animus-queue-default (the `queue` PluginKind)."

src/store/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,31 @@ pub struct Store {
9292
impl Store {
9393
/// Connect to Postgres and apply the idempotent schema.
9494
pub async fn open(config: &QueueConfig) -> Result<Self> {
95+
// LAZY connect + background migrate so a cold DB dial on (re)spawn never
96+
// blocks the daemon initialize handshake (which surfaced as "plugin
97+
// connection lost"). First query connects; migration is idempotent.
9598
let pool = PgPoolOptions::new()
9699
// Small pool: this plugin shares one Railway Postgres with Better
97100
// Auth + the config/subject/chat backends. Keep the shared
98101
// connection budget well under Postgres `max_connections`.
99102
.max_connections(4)
100-
.connect(&config.database_url)
101-
.await
102-
.context("failed to connect to Postgres database")?;
103+
.connect_lazy(&config.database_url)
104+
.context("failed to create Postgres pool")?;
103105
let store = Self {
104106
pool,
105107
table: config.table.clone(),
106108
lease_ttl_secs: config.lease_ttl_secs,
107109
};
108-
store.migrate().await?;
110+
let migrate_store = Self {
111+
pool: store.pool.clone(),
112+
table: store.table.clone(),
113+
lease_ttl_secs: store.lease_ttl_secs,
114+
};
115+
tokio::spawn(async move {
116+
if let Err(error) = migrate_store.migrate().await {
117+
eprintln!("[animus-queue-postgres] background migrate failed (schema likely present; retried next spawn): {error:#}");
118+
}
119+
});
109120
Ok(store)
110121
}
111122

0 commit comments

Comments
 (0)