use async_trait::async_trait;
use serde_json::Value;
use std::collections::HashMap;
use std::future::Future;
use std::pin::Pin;
use std::sync::Arc;
use crate::error::Result;
use crate::models::{Task, TaskInProgress, TaskResult, TaskResultStatus};
#[derive(Debug, Clone)]
pub enum WorkerOutput {
Completed(HashMap<String, Value>),
Failed(String),
FailedWithTerminalError(String),
InProgress(TaskInProgress),
}
impl WorkerOutput {
pub fn completed_with_result(result: impl serde::Serialize) -> Self {
let mut output = HashMap::new();
output.insert(
"result".to_owned(),
serde_json::to_value(result).unwrap_or(Value::Null),
);
WorkerOutput::Completed(output)
}
#[must_use]
pub fn completed(output: HashMap<String, Value>) -> Self {
WorkerOutput::Completed(output)
}
#[must_use]
pub fn complete() -> Self {
WorkerOutput::Completed(HashMap::new())
}
pub fn failed(reason: impl Into<String>) -> Self {
WorkerOutput::Failed(reason.into())
}
#[must_use]
pub fn in_progress(callback_after_seconds: i64) -> Self {
WorkerOutput::InProgress(TaskInProgress::new(callback_after_seconds))
}
#[must_use]
pub fn into_task_result(self, task: &Task, worker_id: &str) -> TaskResult {
match self {
WorkerOutput::Completed(output) => TaskResult {
task_id: task.task_id.clone(),
workflow_instance_id: task.workflow_instance_id.clone(),
worker_id: Some(worker_id.to_owned()),
status: TaskResultStatus::Completed,
output_data: output,
..Default::default()
},
WorkerOutput::Failed(reason) => TaskResult {
task_id: task.task_id.clone(),
workflow_instance_id: task.workflow_instance_id.clone(),
worker_id: Some(worker_id.to_owned()),
status: TaskResultStatus::Failed,
reason_for_incompletion: Some(reason),
..Default::default()
},
WorkerOutput::FailedWithTerminalError(reason) => TaskResult {
task_id: task.task_id.clone(),
workflow_instance_id: task.workflow_instance_id.clone(),
worker_id: Some(worker_id.to_owned()),
status: TaskResultStatus::FailedWithTerminalError,
reason_for_incompletion: Some(reason),
..Default::default()
},
WorkerOutput::InProgress(tip) => TaskResult {
task_id: task.task_id.clone(),
workflow_instance_id: task.workflow_instance_id.clone(),
worker_id: Some(worker_id.to_owned()),
status: TaskResultStatus::InProgress,
callback_after_seconds: tip.callback_after_seconds,
output_data: tip.output,
..Default::default()
},
}
}
}
#[async_trait]
pub trait Worker: Send + Sync {
fn task_definition_name(&self) -> &str;
async fn execute(&self, task: &Task) -> Result<WorkerOutput>;
fn identity(&self) -> String {
let hostname = hostname::get().map_or_else(
|_| "unknown".to_owned(),
|h| h.to_string_lossy().into_owned(),
);
format!("{}-{}", hostname, std::process::id())
}
fn domain(&self) -> Option<&str> {
None
}
fn poll_interval_millis(&self) -> u64 {
100
}
fn thread_count(&self) -> usize {
1
}
fn input_schema(&self) -> Option<serde_json::Value> {
None
}
fn output_schema(&self) -> Option<serde_json::Value> {
None
}
fn declared_credentials(&self) -> Vec<String> {
Vec::new()
}
}
pub type WorkerFn =
Arc<dyn Fn(Task) -> Pin<Box<dyn Future<Output = Result<WorkerOutput>> + Send>> + Send + Sync>;
pub struct FnWorker {
task_name: String,
execute_fn: WorkerFn,
identity: String,
domain: Option<String>,
poll_interval_millis: u64,
thread_count: usize,
input_schema: Option<serde_json::Value>,
output_schema: Option<serde_json::Value>,
}
impl FnWorker {
pub fn new<F, Fut>(task_name: impl Into<String>, f: F) -> Self
where
F: Fn(Task) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<WorkerOutput>> + Send + 'static,
{
let task_name = task_name.into();
let identity = format!(
"{}-{}",
hostname::get().map_or_else(
|_| "unknown".to_owned(),
|h| h.to_string_lossy().into_owned()
),
std::process::id()
);
Self {
task_name,
execute_fn: Arc::new(move |task| Box::pin(f(task))),
identity,
domain: None,
poll_interval_millis: 100,
thread_count: 1,
input_schema: None,
output_schema: None,
}
}
#[must_use]
pub fn with_domain(mut self, domain: impl Into<String>) -> Self {
self.domain = Some(domain.into());
self
}
#[must_use]
pub fn with_poll_interval_millis(mut self, millis: u64) -> Self {
self.poll_interval_millis = millis;
self
}
#[must_use]
pub fn with_thread_count(mut self, count: usize) -> Self {
self.thread_count = count;
self
}
#[must_use]
pub fn with_identity(mut self, identity: impl Into<String>) -> Self {
self.identity = identity.into();
self
}
#[must_use]
pub fn with_input_schema(mut self, schema: serde_json::Value) -> Self {
self.input_schema = Some(schema);
self
}
#[must_use]
pub fn with_output_schema(mut self, schema: serde_json::Value) -> Self {
self.output_schema = Some(schema);
self
}
#[must_use]
pub fn with_input_schema_from<T>(mut self, strict: bool) -> Self
where
T: schemars::JsonSchema,
{
self.input_schema = Some(crate::schema::generate_schema::<T>(strict));
self
}
#[must_use]
pub fn with_output_schema_from<T>(mut self, strict: bool) -> Self
where
T: schemars::JsonSchema,
{
self.output_schema = Some(crate::schema::generate_schema::<T>(strict));
self
}
}
#[async_trait]
impl Worker for FnWorker {
fn task_definition_name(&self) -> &str {
&self.task_name
}
async fn execute(&self, task: &Task) -> Result<WorkerOutput> {
(self.execute_fn)(task.clone()).await
}
fn identity(&self) -> String {
self.identity.clone()
}
fn domain(&self) -> Option<&str> {
self.domain.as_deref()
}
fn poll_interval_millis(&self) -> u64 {
self.poll_interval_millis
}
fn thread_count(&self) -> usize {
self.thread_count
}
fn input_schema(&self) -> Option<serde_json::Value> {
self.input_schema.clone()
}
fn output_schema(&self) -> Option<serde_json::Value> {
self.output_schema.clone()
}
}
pub type WorkerFnArc = Arc<
dyn Fn(Arc<Task>) -> Pin<Box<dyn Future<Output = Result<WorkerOutput>> + Send>> + Send + Sync,
>;
pub struct FnWorkerArc {
task_name: String,
execute_fn: WorkerFnArc,
identity: String,
domain: Option<String>,
poll_interval_millis: u64,
thread_count: usize,
input_schema: Option<serde_json::Value>,
output_schema: Option<serde_json::Value>,
}
impl FnWorkerArc {
pub fn new<F, Fut>(task_name: impl Into<String>, f: F) -> Self
where
F: Fn(Arc<Task>) -> Fut + Send + Sync + 'static,
Fut: Future<Output = Result<WorkerOutput>> + Send + 'static,
{
let task_name = task_name.into();
let identity = format!(
"{}-{}",
hostname::get().map_or_else(
|_| "unknown".to_owned(),
|h| h.to_string_lossy().into_owned()
),
std::process::id()
);
Self {
task_name,
execute_fn: Arc::new(move |task| Box::pin(f(task))),
identity,
domain: None,
poll_interval_millis: 100,
thread_count: 1,
input_schema: None,
output_schema: None,
}
}
#[must_use]
pub fn with_domain(mut self, domain: impl Into<String>) -> Self {
self.domain = Some(domain.into());
self
}
#[must_use]
pub fn with_poll_interval_millis(mut self, millis: u64) -> Self {
self.poll_interval_millis = millis;
self
}
#[must_use]
pub fn with_thread_count(mut self, count: usize) -> Self {
self.thread_count = count;
self
}
#[must_use]
pub fn with_identity(mut self, identity: impl Into<String>) -> Self {
self.identity = identity.into();
self
}
#[must_use]
pub fn with_input_schema(mut self, schema: serde_json::Value) -> Self {
self.input_schema = Some(schema);
self
}
#[must_use]
pub fn with_output_schema(mut self, schema: serde_json::Value) -> Self {
self.output_schema = Some(schema);
self
}
#[must_use]
pub fn with_input_schema_from<T>(mut self, strict: bool) -> Self
where
T: schemars::JsonSchema,
{
self.input_schema = Some(crate::schema::generate_schema::<T>(strict));
self
}
#[must_use]
pub fn with_output_schema_from<T>(mut self, strict: bool) -> Self
where
T: schemars::JsonSchema,
{
self.output_schema = Some(crate::schema::generate_schema::<T>(strict));
self
}
pub async fn execute_arc(&self, task: Arc<Task>) -> Result<WorkerOutput> {
(self.execute_fn)(task).await
}
}
#[async_trait]
impl Worker for FnWorkerArc {
fn task_definition_name(&self) -> &str {
&self.task_name
}
async fn execute(&self, task: &Task) -> Result<WorkerOutput> {
(self.execute_fn)(Arc::new(task.clone())).await
}
fn identity(&self) -> String {
self.identity.clone()
}
fn domain(&self) -> Option<&str> {
self.domain.as_deref()
}
fn poll_interval_millis(&self) -> u64 {
self.poll_interval_millis
}
fn thread_count(&self) -> usize {
self.thread_count
}
fn input_schema(&self) -> Option<serde_json::Value> {
self.input_schema.clone()
}
fn output_schema(&self) -> Option<serde_json::Value> {
self.output_schema.clone()
}
}
#[macro_export]
macro_rules! worker_fn {
($task_name:expr, $handler:expr) => {
$crate::worker::FnWorker::new($task_name, $handler)
};
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_fn_worker() {
let worker = FnWorker::new("test_task", |task: Task| async move {
let name = task
.get_input_string("name")
.unwrap_or_else(|| "World".to_owned());
Ok(WorkerOutput::completed_with_result(format!(
"Hello, {name}!"
)))
})
.with_thread_count(5)
.with_poll_interval_millis(500);
assert_eq!(worker.task_definition_name(), "test_task");
assert_eq!(worker.thread_count(), 5);
assert_eq!(worker.poll_interval_millis(), 500);
let mut task = Task::default();
task.input_data
.insert("name".to_owned(), serde_json::json!("Rust"));
let result = worker.execute(&task).await.unwrap();
match result {
WorkerOutput::Completed(output) => {
assert!(output.contains_key("result"));
}
_ => panic!("Expected completed output"),
}
}
#[test]
fn test_worker_output_conversion() {
let task = Task {
task_id: "task-1".to_owned(),
workflow_instance_id: "wf-1".to_owned(),
..Default::default()
};
let output = WorkerOutput::completed_with_result("success");
let result = output.into_task_result(&task, "worker-1");
assert_eq!(result.task_id, "task-1");
assert_eq!(result.status, TaskResultStatus::Completed);
assert_eq!(result.worker_id, Some("worker-1".to_owned()));
}
#[test]
fn test_worker_output_failed_is_retryable_status() {
let task = Task::default();
let result =
WorkerOutput::Failed("transient error".to_owned()).into_task_result(&task, "worker-1");
assert_eq!(result.status, TaskResultStatus::Failed);
assert_eq!(
result.reason_for_incompletion,
Some("transient error".to_owned())
);
}
#[test]
fn test_worker_output_failed_with_terminal_error_conversion() {
let task = Task {
task_id: "task-1".to_owned(),
workflow_instance_id: "wf-1".to_owned(),
..Default::default()
};
let output = WorkerOutput::FailedWithTerminalError("not retryable".to_owned());
let result = output.into_task_result(&task, "worker-1");
assert_eq!(result.task_id, "task-1");
assert_eq!(result.status, TaskResultStatus::FailedWithTerminalError);
assert_eq!(
result.reason_for_incompletion,
Some("not retryable".to_owned())
);
assert_eq!(result.worker_id, Some("worker-1".to_owned()));
}
}