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
use crate::{
Act, ActTask, Error, Result,
model::Step,
scheduler::{Context, TaskState},
utils::consts,
};
impl ActTask for Step {
fn init(&self, ctx: &Context) -> Result<()> {
let task = ctx.task();
if let Some(expr) = &self.r#if {
let cond = ctx.eval::<bool>(expr)?;
if !cond {
task.set_state(TaskState::Skipped);
return Ok(());
}
}
Ok(())
}
fn run(&self, ctx: &Context) -> Result<()> {
let task = ctx.task();
if let Some(uses) = &self.uses {
ctx.dispatch_act(
&Act {
uses: uses.to_string(),
params: task.params(),
..Default::default()
},
self.vars(),
)?;
}
let children = task.node.children();
if !children.is_empty() {
for child in &children {
ctx.sched_task(child)?;
}
}
Ok(())
}
fn next(&self, ctx: &Context) -> Result<bool> {
let task = ctx.task();
let state = task.state();
let mut is_next: bool = false;
if state.is_running() {
let tasks = task.children();
let mut count = 0;
for task in tasks.iter() {
if task.state().is_none() || task.state().is_running() {
is_next = true;
} else if task.state().is_pending() && task.is_ready() {
// resume task
task.set_state(TaskState::Running);
ctx.runtime.emitter().emit_task_event(task)?;
task.exec(ctx)?;
is_next = true;
}
if task.state().is_completed() {
count += 1;
}
}
if count == tasks.len() {
if !task.state().is_completed() {
task.set_state(TaskState::Completed);
}
if let Some(next) = &task.node.next().upgrade() {
ctx.sched_task(next)?;
return Ok(true);
}
}
} else if state.is_skip() {
// if the step is skipped, still find the next to run
if let Some(next) = task.node.next().upgrade() {
ctx.sched_task(&next)?;
return Ok(true);
}
}
Ok(is_next)
}
fn review(&self, ctx: &Context) -> Result<bool> {
let task = ctx.task();
let state = task.state();
if state.is_running() {
let tasks = task.children();
let mut count = 0;
for task in tasks.iter() {
if task.state().is_pending() && task.is_ready() {
// resume task
task.set_state(TaskState::Running);
ctx.runtime.emitter().emit_task_event(task)?;
task.exec(ctx)?;
return Ok(false);
}
if task.state().is_completed() {
count += 1;
}
}
if count == tasks.len() {
if !task.state().is_completed() {
// check if the task is error catched
let is_empty_catched = tasks
.iter()
.filter(|t| t.is_sign(consts::TASK_SIGN_CATCH))
.all(|t| t.state().is_skip());
if task.is_sign(consts::TASK_SIGN_ERR) && is_empty_catched {
// no any action to match
// resume the task error state
let err = task.with_data(|data| {
Error::new(
&data
.get::<String>(consts::ACT_ERR_MESSAGE)
.unwrap_or_default(),
&data.get::<String>(consts::ACT_ERR_CODE).unwrap_or_default(),
)
});
task.set_err(&err);
ctx.emit_error()?;
return Ok(false);
} else {
// if the task is originally error, clean the error data
// and set the task state to completed
if task.is_sign(consts::TASK_SIGN_ERR) {
task.set_data_with(|data| {
data.remove(consts::ACT_ERR_MESSAGE);
data.remove(consts::ACT_ERR_CODE);
});
}
task.set_state(TaskState::Completed);
}
}
if let Some(next) = &task.node.next().upgrade() {
ctx.sched_task(next)?;
return Ok(false);
}
return Ok(true);
}
} else if state.is_skip() {
// if the step is skipped, still find the next to run
if let Some(next) = task.node.next().upgrade() {
ctx.sched_task(&next)?;
return Ok(false);
}
return Ok(true);
}
Ok(false)
}
}