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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate;
use crateSignature;
use Value;
/// Creates a conditional execution chain
///
/// Executes a predicate task, and based on its result, executes either
/// the success chain or the fallback chain.
///
/// # Arguments
///
/// * `predicate_task` - Task that returns a boolean condition
/// * `predicate_args` - Arguments for the predicate task
/// * `success_chain` - Tasks to execute if predicate is true
/// * `fallback_chain` - Optional tasks to execute if predicate is false
///
/// # Example
///
/// ```rust,ignore
/// use celers::task_composition::conditional_chain;
/// use serde_json::json;
///
/// let workflow = conditional_chain(
/// "check_balance",
/// vec![json!({"account_id": 123})],
/// vec![("process_payment", vec![]), ("send_receipt", vec![])],
/// Some(("send_insufficient_funds_email", vec![]))
/// );
/// ```
/// Creates a retry wrapper with exponential backoff
///
/// Wraps a task with automatic retry logic using exponential backoff.
///
/// # Arguments
///
/// * `task_name` - Name of the task to wrap
/// * `args` - Task arguments
/// * `max_retries` - Maximum number of retry attempts
/// * `initial_delay` - Initial delay in seconds (doubles each retry)
///
/// # Example
///
/// ```rust,ignore
/// use celers::task_composition::retry_wrapper;
/// use serde_json::json;
///
/// let sig = retry_wrapper(
/// "fetch_external_api",
/// vec![json!({"url": "https://api.example.com"})],
/// 5, // Retry up to 5 times
/// 10 // Start with 10 second delay
/// );
/// ```
/// Creates a timeout-protected task
///
/// Wraps a task with a timeout, ensuring it doesn't run indefinitely.
///
/// # Arguments
///
/// * `task_name` - Name of the task to protect
/// * `args` - Task arguments
/// * `timeout_seconds` - Maximum execution time in seconds
///
/// # Example
///
/// ```rust,ignore
/// use celers::task_composition::timeout_wrapper;
/// use serde_json::json;
///
/// let sig = timeout_wrapper(
/// "long_running_task",
/// vec![json!({"process": "large_dataset"})],
/// 300 // 5 minute timeout
/// );
/// ```
/// Creates a task group with circuit breaker pattern
///
/// Groups tasks and adds circuit breaker semantics to prevent cascading
/// failures.
///
/// # Arguments
///
/// * `tasks` - List of (task_name, args) tuples
/// * `max_failures` - Maximum failures before circuit opens
///
/// # Example
///
/// ```rust,ignore
/// use celers::task_composition::circuit_breaker_group;
/// use serde_json::json;
///
/// let tasks = vec![
/// ("call_service_a", vec![json!(1)]),
/// ("call_service_b", vec![json!(2)]),
/// ("call_service_c", vec![json!(3)]),
/// ];
///
/// let group = circuit_breaker_group(tasks, 2);
/// ```
/// Creates a rate-limited workflow
///
/// Spaces out task execution to respect rate limits.
///
/// # Arguments
///
/// * `task_name` - Name of the task
/// * `items` - Items to process
/// * `delay_between_tasks` - Delay in seconds between each task
///
/// # Example
///
/// ```rust,ignore
/// use celers::task_composition::rate_limited_workflow;
/// use serde_json::json;
///
/// let items = vec![json!(1), json!(2), json!(3)];
/// let workflow = rate_limited_workflow(
/// "call_rate_limited_api",
/// items,
/// 5 // 5 seconds between each call
/// );
/// ```