use std::sync::Arc;
use super::IO_TIMEOUT;
use crate::operations::{Context, Operation, OperationDeclaration, StreamOutput};
use crate::storage::{FileBatch, FileProgress, StorageManager};
use crate::{Error, Result};
use harmos_domain::operations::storage::FollowProduction;
pub(super) struct Handler<C> {
storage: Arc<StorageManager<C>>,
}
impl<C: 'static> Operation for Handler<C> {
type Declaration = FollowProduction;
type Resources = StorageManager<C>;
type Error = Error;
type Context<'a> = Context<'a>;
type Endpoints = StreamOutput<FileBatch>;
async fn initialize<'ctx>(storage: Arc<StorageManager<C>>, _: Self::Context<'ctx>) -> Result<Self> {
Ok(Self { storage })
}
#[tracing::instrument(target = "harmos::storage", name = "storage.operation.follow_production", level = "trace", skip_all)]
async fn run<'a>(
&mut self, ctx: Self::Context<'a>, request: &mut FollowProduction, output: &mut Self::Endpoints,
) -> Result<crate::operations::Response<FileProgress>> {
let grant = harmos_domain::plugin::Grant::of(FollowProduction::ID, request.file.id.as_str().to_owned());
ctx.admit_stream_grant(&grant)?;
let mut follower = self.storage.follow_production(&request.file, request.offset).await?;
let batch_bytes = self.storage.settings().response.batch_bytes;
while let Some(batch) = follower.next_batch(batch_bytes).await? {
ctx.admit_stream_grant(&grant)?;
let response = crate::operations::Response::from_owner(batch.owner());
tokio::time::timeout(IO_TIMEOUT, output.send_owned(response)).await.map_err(|_| super::output_timeout())??;
}
Ok(follower.progress().into())
}
fn classify(error: &Error) -> crate::operations::FailureKind {
super::classify(error)
}
}