No behaviour change. Casts replaced by try_from / Duration math; schedules.rs reuses agent_renewal::unix_now; the five swarm-queue-client KV open-or-create copies share one private kv::open_or_create.
34 lines
1 KiB
Rust
34 lines
1 KiB
Rust
//! The open-or-create sequence every KV bucket module shares.
|
|
|
|
use crate::Error;
|
|
|
|
/// Open `bucket`, creating it with `history: 1` and `description` when it
|
|
/// does not exist yet. `label` names the bucket in the creation log line.
|
|
pub(crate) async fn open_or_create(
|
|
js: &async_nats::jetstream::Context,
|
|
bucket: &str,
|
|
label: &str,
|
|
description: String,
|
|
) -> Result<async_nats::jetstream::kv::Store, Error> {
|
|
match js.get_key_value(bucket).await {
|
|
Ok(store) => Ok(store),
|
|
Err(e) => {
|
|
tracing::info!(
|
|
bucket,
|
|
reason = %e,
|
|
"{label} bucket not available, creating it"
|
|
);
|
|
js.create_key_value(async_nats::jetstream::kv::Config {
|
|
bucket: bucket.to_owned(),
|
|
description,
|
|
history: 1,
|
|
..Default::default()
|
|
})
|
|
.await
|
|
.map_err(|source| Error::CreateBucket {
|
|
bucket: bucket.to_owned(),
|
|
source,
|
|
})
|
|
}
|
|
}
|
|
}
|