-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel.rs
243 lines (202 loc) · 6.52 KB
/
parallel.rs
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
//! 并行任务
//! 可以走多个分支
//! 多分支全部走完才能走下一个
//! TODO: 分支如果出现错误 是否需要同步上下文 暂时同步
use std::collections::HashMap;
use futures::{
future::{join_all, select_all},
FutureExt,
};
use pipeline::{AspectContext, PipelineError, PipelineResult};
use super::{sequence::SequenceTask, IChildrenFlow, IContext, ITask, StrategicTask};
#[derive(Clone)]
pub struct ParallelTask<ContextT> {
nexts: HashMap<String, SequenceTask<ContextT>>,
condition: fn(&AspectContext<ContextT>) -> Vec<String>,
await_next_all: bool,
}
impl<ContextT> ParallelTask<ContextT> {
pub fn new(
condition: fn(&AspectContext<ContextT>) -> Vec<String>,
await_next_all: bool,
) -> Self {
ParallelTask {
nexts: HashMap::new(),
condition,
await_next_all,
}
}
}
impl<T> IChildrenFlow<T, StrategicTask<T>> for ParallelTask<T>
where
T: Clone + Send + 'static,
{
fn children_as_mut(&mut self) -> Vec<&mut StrategicTask<T>> {
self.nexts
.values_mut()
.flat_map(|x| x.children_as_mut())
.collect()
}
}
pub trait ParallelTaskEx<ContextT> {
fn branch(&mut self, branch: String, next: SequenceTask<ContextT>) -> &mut Self;
}
impl<ContextT> ParallelTaskEx<ContextT> for ParallelTask<ContextT> {
fn branch(&mut self, branch: String, next: SequenceTask<ContextT>) -> &mut Self {
self.nexts.insert(branch, next);
self
}
}
async fn process_line<ContextT>(
line: &mut SequenceTask<ContextT>,
mut context: AspectContext<ContextT>,
) -> (PipelineResult<()>, AspectContext<ContextT>)
where
ContextT: Send + Clone + 'static,
{
let result = line.invoke(&mut context).await;
(result, context)
}
#[async_trait]
impl<ContextT> ITask<ContextT> for ParallelTask<ContextT>
where
ContextT: IContext + Send + Clone + 'static,
{
async fn invoke(&self, context: &mut AspectContext<ContextT>) -> PipelineResult<()> {
let result = (self.condition)(context);
let mut lines: HashMap<String, SequenceTask<ContextT>> = result
.iter()
.filter_map(|x| self.nexts.get(x).map(|line| (x.clone(), line.clone())))
.collect();
let all = lines
.iter_mut()
.map(|(_, line)| process_line(line, context.clone()).boxed());
match self.await_next_all {
true => {
let results = join_all(all).await;
let mut error_results = Vec::new();
for (result, c) in results {
context.current.merge(&c.current);
match result {
Ok(_) => {}
Err(e) => {
error_results.push(e);
}
}
}
if error_results.len() > 0 {
Err(PipelineError::Aggregated(error_results))
} else {
Ok(())
}
}
false => {
let ((result, c), _, _) = select_all(all).await;
context.current.merge(&c.current);
result?;
Ok(())
}
}?;
Ok(())
}
}
#[cfg(test)]
mod tests {
use anyhow::anyhow;
use pipeline::{AspectContext, PipelineError, PipelineResult};
use crate::tasks::{
parallel::ParallelTaskEx, sequence::SequenceTask, IStrategicTaskFlow, ITask, ParallelTask,
StrategicTaskFlow,
};
#[derive(Debug, Clone)]
struct Data {
tag: String,
}
impl super::IContext for Data {
fn merge(&mut self, other: &Self) {
self.tag = other.tag.clone();
}
}
#[derive(Clone)]
struct TaskA {}
#[async_trait]
impl ITask<Data> for TaskA {
async fn invoke(&self, _: &mut AspectContext<Data>) -> PipelineResult<()> {
println!("start a");
println!("end a");
Ok(())
}
async fn rollback(&self, _: Option<&PipelineError>, _: &mut AspectContext<Data>) {
println!("fallback a");
}
}
#[derive(Clone)]
struct TaskB {}
#[async_trait]
impl ITask<Data> for TaskB {
async fn invoke(&self, _: &mut AspectContext<Data>) -> PipelineResult<()> {
println!("start b");
println!("end b");
Ok(())
}
async fn rollback(&self, _: Option<&PipelineError>, _: &mut AspectContext<Data>) {
println!("fallback b");
}
}
#[derive(Clone)]
struct TaskC1 {}
#[async_trait]
impl ITask<Data> for TaskC1 {
async fn invoke(&self, _: &mut AspectContext<Data>) -> PipelineResult<()> {
println!("start c1");
println!("end c1");
Ok(())
}
async fn rollback(&self, _: Option<&PipelineError>, _: &mut AspectContext<Data>) {
println!("fallback c1");
}
}
#[derive(Clone)]
struct TaskC2 {}
#[async_trait]
impl ITask<Data> for TaskC2 {
async fn invoke(&self, context: &mut AspectContext<Data>) -> PipelineResult<()> {
println!("start c2");
context.current.tag = "c".to_owned();
println!("end c2");
Err(anyhow!("error").into())
}
async fn rollback(&self, _: Option<&PipelineError>, _: &mut AspectContext<Data>) {
println!("fallback c2");
}
}
#[derive(Clone)]
struct TaskD {}
#[async_trait]
impl ITask<Data> for TaskD {
async fn invoke(&self, _: &mut AspectContext<Data>) -> PipelineResult<()> {
println!("start d");
println!("end d");
Ok(())
}
}
#[tokio::test]
async fn test() -> anyhow::Result<()> {
let mut context = AspectContext::new(Data { tag: "".to_owned() });
let mut line = StrategicTaskFlow::simple();
line.then(TaskA {})?;
let mut line_b = SequenceTask::new();
line_b.then_builder_f(TaskB {}, |x| {
x.retry(1, |x| (x * 100).into());
})?;
let mut line_c = SequenceTask::new();
line_c.then(TaskC1 {})?.then(TaskC2 {})?;
let mut parallel = ParallelTask::new(|_| vec!["c".to_string()], true);
parallel.branch("b".to_string(), line_b);
parallel.branch("c".to_string(), line_c);
line.then(parallel)?.then(TaskD {})?;
_ = line.invoke(&mut context).await;
assert_eq!(context.current.tag, "c".to_owned());
Ok(())
}
}