use crate::error::Error;
use crate::proto::{Batch, BatchId, Client, Job};
pub struct BatchHandle<'a> {
bid: BatchId,
c: &'a mut Client,
}
impl<'a> BatchHandle<'a> {
pub(crate) fn new(bid: BatchId, c: &mut Client) -> BatchHandle<'_> {
BatchHandle { bid, c }
}
}
impl BatchHandle<'_> {
pub fn id(&self) -> &BatchId {
&self.bid
}
}
impl BatchHandle<'_> {
pub async fn add(&mut self, mut job: Job) -> Result<Option<serde_json::Value>, Error> {
let bid = job.custom.insert("bid".into(), self.bid.clone().into());
self.c.enqueue(job).await.map(|_| bid)
}
pub async fn start_batch(&mut self, mut batch: Batch) -> Result<BatchHandle<'_>, Error> {
batch.parent_bid = Some(self.bid.clone());
self.c.start_batch(batch).await
}
pub async fn commit(self) -> Result<(), Error> {
self.c.commit_batch(&self.bid).await
}
}