1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
use crate::prelude::{AbstractTaskStorage, Task};
use async_trait::async_trait;
use serde::{de::DeserializeOwned, Serialize};
use std::sync::Arc;
use thiserror::Error;

use super::worker::WorkerId;

#[derive(Error, Debug)]
pub enum ComputationError {
    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
    #[error("Database error: {0}")]
    Db(String),
    #[error("Task storage error: {0}")]
    Storage(String),
    #[error("Task error: {0}")]
    Task(String),
    #[error("Computation execution error: {0}")]
    Function(String),
    #[error("Max retries: {0}")]
    MaxRetries(String),
}

/// A trait defining the interface for processing a task. This trait is
/// intended to be implemented by a worker that will process tasks
/// of a specific type.
#[async_trait]
pub trait Computation<Data, Ctx>
where
    Data: Clone + Serialize + DeserializeOwned + Send + Sync + 'static,
    Ctx: Send + Sync + 'static,
{
    /// Do computation The worker_id is passed for logging or
    /// debugging purposes. The task is a mutable reference,
    /// allowing the processor to modify the task data as part of the processing.
    async fn call(
        &self,
        worker_id: WorkerId,
        ctx: Arc<Ctx>,
        // NOTE: i used type alias instead of this
        // is something put this line back and remove next one!
        // storage: Arc<dyn TaskStorage<Data> + Send + Sync>,
        storage: AbstractTaskStorage<Data>,
        task: &mut Task<Data>,
    ) -> Result<(), ComputationError>;
}