taskflow_rs/task/
definition.rs1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use std::collections::HashMap;
4use uuid::Uuid;
5
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct TaskDefinition {
8 pub id: String,
9 pub name: String,
10 pub task_type: String,
11 pub payload: HashMap<String, serde_json::Value>,
12 pub priority: i32,
13 pub max_retries: u32,
14 pub timeout_seconds: u64,
15 pub dependencies: Vec<String>,
16 pub tags: Vec<String>,
17 pub created_at: DateTime<Utc>,
18 pub scheduled_at: Option<DateTime<Utc>>,
19}
20
21impl TaskDefinition {
22 pub fn new(name: &str, task_type: &str) -> Self {
23 Self {
24 id: Uuid::new_v4().to_string(),
25 name: name.to_string(),
26 task_type: task_type.to_string(),
27 payload: HashMap::new(),
28 priority: 0,
29 max_retries: 3,
30 timeout_seconds: 300,
31 dependencies: Vec::new(),
32 tags: Vec::new(),
33 created_at: Utc::now(),
34 scheduled_at: None,
35 }
36 }
37
38 pub fn with_payload(mut self, key: &str, value: serde_json::Value) -> Self {
39 self.payload.insert(key.to_string(), value);
40 self
41 }
42
43 pub fn with_priority(mut self, priority: i32) -> Self {
44 self.priority = priority;
45 self
46 }
47
48 pub fn with_timeout(mut self, timeout_seconds: u64) -> Self {
49 self.timeout_seconds = timeout_seconds;
50 self
51 }
52
53 pub fn with_dependencies(mut self, dependencies: Vec<String>) -> Self {
54 self.dependencies = dependencies;
55 self
56 }
57
58 pub fn with_tags(mut self, tags: Vec<String>) -> Self {
59 self.tags = tags;
60 self
61 }
62
63 pub fn schedule_at(mut self, scheduled_at: DateTime<Utc>) -> Self {
64 self.scheduled_at = Some(scheduled_at);
65 self
66 }
67}