publish the agent's skills to the agent-commands bucket
This commit is contained in:
parent
c45615a555
commit
49719842b0
1 changed files with 416 additions and 69 deletions
|
|
@ -19,28 +19,14 @@
|
|||
//! one are retried with backoff until one is acked.
|
||||
|
||||
use std::path::Path;
|
||||
use std::time::Duration;
|
||||
|
||||
use async_nats::jetstream::context::PublishErrorKind;
|
||||
use backon::{ExponentialBuilder, Retryable};
|
||||
use crate::swarm_kv_put::{Failure, Put, put_retrying};
|
||||
|
||||
/// Where the agent's NixOS config puts the icon
|
||||
/// (`services.hyperhive.agent.icon`), and the file `GET /icon` serves. Absent
|
||||
/// when none is configured.
|
||||
const ICON_PATH: &str = "/etc/hyperhive/icon.svg";
|
||||
|
||||
/// First retry delay after a failed write, doubled per failure up to
|
||||
/// [`MAX_RETRY`], with no cap on the number of tries.
|
||||
const FIRST_RETRY: Duration = Duration::from_secs(5);
|
||||
|
||||
/// Longest wait between retries.
|
||||
const MAX_RETRY: Duration = Duration::from_mins(5);
|
||||
|
||||
/// The KV header and value that mark an entry deleted. `kv::Store::get`
|
||||
/// answers `None` for an entry carrying them.
|
||||
const KV_OPERATION: &str = "KV-Operation";
|
||||
const KV_OPERATION_DELETE: &str = "DEL";
|
||||
|
||||
/// What to write under this agent's key.
|
||||
#[derive(Debug, PartialEq, Eq)]
|
||||
enum Icon {
|
||||
|
|
@ -80,21 +66,11 @@ async fn run(agent: String) {
|
|||
return;
|
||||
}
|
||||
};
|
||||
let js = async_nats::jetstream::new(client.clone());
|
||||
let written = (|| write(&client, &js, &subject, &icon))
|
||||
.retry(
|
||||
ExponentialBuilder::default()
|
||||
.with_min_delay(FIRST_RETRY)
|
||||
.with_max_delay(MAX_RETRY)
|
||||
.without_max_times(),
|
||||
)
|
||||
.when(|failure| matches!(failure, Failure::Retry(_)))
|
||||
.notify(|failure, delay| {
|
||||
if let Failure::Retry(e) = failure {
|
||||
tracing::warn!(error = e, retry_in = ?delay, "publishing the agent icon failed");
|
||||
}
|
||||
})
|
||||
.await;
|
||||
let put = match &icon {
|
||||
Icon::Set(bytes) => Put::Set(bytes),
|
||||
Icon::Unset => Put::Delete,
|
||||
};
|
||||
let written = put_retrying(&client, &subject, put, "the agent icon").await;
|
||||
match written {
|
||||
Ok(()) => tracing::info!(
|
||||
subject,
|
||||
|
|
@ -111,44 +87,6 @@ async fn run(agent: String) {
|
|||
}
|
||||
}
|
||||
|
||||
/// Why a write did not land.
|
||||
enum Failure {
|
||||
/// The payload can never fit; retrying cannot help.
|
||||
TooLarge(async_nats::jetstream::context::PublishError),
|
||||
/// Anything else, rendered for the log.
|
||||
Retry(String),
|
||||
}
|
||||
|
||||
/// One acked write of `icon` to `subject`.
|
||||
async fn write(
|
||||
client: &async_nats::Client,
|
||||
js: &async_nats::jetstream::Context,
|
||||
subject: &str,
|
||||
icon: &Icon,
|
||||
) -> Result<(), Failure> {
|
||||
// An unconnected client does not fail a publish, it buffers it, and the
|
||||
// payload limit it checks against is the library default until the
|
||||
// server has announced its own.
|
||||
swarm_queue_client::ensure_connected(client)
|
||||
.map_err(|e| Failure::Retry(swarm_queue_client::chain(&e)))?;
|
||||
let sent = match icon {
|
||||
Icon::Set(bytes) => js.publish(subject.to_owned(), bytes.clone().into()).await,
|
||||
Icon::Unset => {
|
||||
let mut headers = async_nats::HeaderMap::new();
|
||||
headers.insert(KV_OPERATION, KV_OPERATION_DELETE);
|
||||
js.publish_with_headers(subject.to_owned(), headers, Vec::new().into())
|
||||
.await
|
||||
}
|
||||
};
|
||||
let ack = sent.map_err(|e| match e.kind() {
|
||||
PublishErrorKind::MaxPayloadExceeded => Failure::TooLarge(e),
|
||||
_ => Failure::Retry(e.to_string()),
|
||||
})?;
|
||||
// `StreamNotFound` here is the bucket not existing yet.
|
||||
ack.await.map_err(|e| Failure::Retry(e.to_string()))?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::{Icon, read_icon};
|
||||
|
|
|
|||
Loading…
Reference in a new issue