use std::sync::Arc;
use crate::aggregator::{Aggregator, Aggregators};
use crate::dedup::{Dedup, Dedups};
use crate::scheduler::{InFlight, Queue};
use crate::task::{Envelope, Task};
pub struct Context {
pub(crate) queue: Queue,
pub(crate) in_flight: Arc<InFlight>,
pub(crate) aggregators: Arc<Aggregators>,
pub(crate) dedups: Arc<Dedups>,
pub(crate) shard: usize,
}
impl Context {
pub async fn emit<T: Task>(&self, task: T) -> crate::Result<()> {
self.in_flight.inc();
if self.queue.send(Envelope::new(task)).is_err() {
self.in_flight.dec();
return Err(crate::Error::Stopped);
}
Ok(())
}
pub fn aggregate<A: Aggregator>(&self, input: A::Input) {
self.aggregators.fold::<A>(self.shard, input);
}
pub fn first_seen<D: Dedup>(&self, key: D::Key) -> bool {
self.dedups.first_seen::<D>(key)
}
}