Skip to main content

bpm_engine_storage/
external_task_store.rs

1//! External Task store trait (plan §12): fetch-and-lock, complete, fail, reclaim, create.
2
3use async_trait::async_trait;
4use bpm_engine_core::ExternalTask;
5use std::collections::HashMap;
6use std::time::Duration;
7
8#[async_trait]
9pub trait ExternalTaskStore: Send + Sync {
10    /// Create a READY external task when token arrives at ExternalTask node.
11    async fn create(
12        &self,
13        token_id: &str,
14        process_instance_id: &str,
15        task_type: &str,
16        retries: i32,
17        timeout_secs: u64,
18        variables: HashMap<String, String>,
19    ) -> anyhow::Result<String>;
20
21    /// Fetch READY tasks matching task_types, lock to worker, return locked tasks.
22    async fn fetch_and_lock(
23        &self,
24        worker_id: &str,
25        task_types: &[String],
26        max_tasks: usize,
27        lock_duration: Duration,
28    ) -> anyhow::Result<Vec<ExternalTask>>;
29
30    /// Complete: LOCKED + lock_owner + not expired -> COMPLETED; merge variables.
31    async fn complete(
32        &self,
33        task_id: &str,
34        worker_id: &str,
35        variables: HashMap<String, String>,
36    ) -> anyhow::Result<()>;
37
38    /// Fail: retries -= 1; if retries > 0 -> READY else -> FAILED.
39    async fn fail(
40        &self,
41        task_id: &str,
42        worker_id: &str,
43        error: String,
44        retry_after: Option<Duration>,
45    ) -> anyhow::Result<()>;
46
47    /// Reclaim LOCKED tasks whose lock_expire_at < now to READY (plan §9).
48    async fn reclaim_expired_locks(&self) -> anyhow::Result<usize>;
49
50    /// Load task by id (for REST layer after complete to get token_id/instance_id for transition).
51    async fn get(&self, task_id: &str) -> anyhow::Result<Option<ExternalTask>>;
52}