hive-c0re/swarm-*: drop clippy allows and dedupe unix_now + KV open_or_create
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.
This commit is contained in:
parent
f53ccb2740
commit
b874d21cda
11 changed files with 74 additions and 133 deletions
34
swarm-queue-client/src/kv.rs
Normal file
34
swarm-queue-client/src/kv.rs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
//! 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,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue