use super::Task;
use crate::bee::{TaskId, TaskMeta, Worker};
use crate::hive::{Outcome, OutcomeSender};
pub use task_impl::TaskInput;
impl<W: Worker> Task<W> {
#[inline]
pub fn id(&self) -> TaskId {
self.meta.id()
}
#[inline]
pub fn meta(&self) -> &TaskMeta {
&self.meta
}
pub fn into_parts(self) -> (W::Input, TaskMeta, Option<OutcomeSender<W>>) {
(self.input, self.meta, self.outcome_tx)
}
pub fn into_unprocessed(self) -> (Outcome<W>, Option<OutcomeSender<W>>) {
let outcome = Outcome::Unprocessed {
input: self.input,
task_id: self.meta.id(),
};
(outcome, self.outcome_tx)
}
#[cfg(feature = "retry")]
pub fn next_retry_attempt(
input: W::Input,
mut meta: TaskMeta,
outcome_tx: Option<OutcomeSender<W>>,
) -> Self {
meta.inc_attempt();
Self {
input,
meta,
outcome_tx,
}
}
}
#[cfg(not(feature = "local-batch"))]
mod task_impl {
use super::Task;
use crate::bee::{TaskId, TaskMeta, Worker};
use crate::hive::OutcomeSender;
pub type TaskInput<W> = <W as Worker>::Input;
impl<W: Worker> Task<W> {
pub fn new(
task_id: TaskId,
input: TaskInput<W>,
outcome_tx: Option<OutcomeSender<W>>,
) -> Self {
Task {
input,
meta: TaskMeta::new(task_id),
outcome_tx,
}
}
}
}
#[cfg(feature = "local-batch")]
mod task_impl {
use super::Task;
use crate::bee::{TaskId, TaskMeta, Worker};
use crate::hive::{Outcome, OutcomeSender, Weighted};
pub type TaskInput<W> = Weighted<<W as Worker>::Input>;
impl<W: Worker> Task<W> {
pub fn new(
task_id: TaskId,
input: TaskInput<W>,
outcome_tx: Option<OutcomeSender<W>>,
) -> Self {
let (input, weight) = input.into_parts();
Task {
input,
meta: TaskMeta::with_weight(task_id, weight),
outcome_tx,
}
}
pub fn into_overweight(self) -> (Outcome<W>, Option<OutcomeSender<W>>) {
let outcome = Outcome::WeightLimitExceeded {
input: self.input,
weight: self.meta.weight(),
task_id: self.meta.id(),
};
(outcome, self.outcome_tx)
}
}
}
impl<I: Clone, W: Worker<Input = I>> Clone for Task<W> {
fn clone(&self) -> Self {
Self {
input: self.input.clone(),
meta: self.meta.clone(),
outcome_tx: self.outcome_tx.clone(),
}
}
}
impl<W: Worker> PartialEq for Task<W> {
fn eq(&self, other: &Self) -> bool {
self.meta.id() == other.meta.id()
}
}
impl<W: Worker> Eq for Task<W> {}
impl<W: Worker> PartialOrd for Task<W> {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
Some(self.cmp(other))
}
}
impl<W: Worker> Ord for Task<W> {
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
self.meta.id().cmp(&other.meta.id())
}
}