|
| 1 | +//! This module defines composite steps that can be used to compose multiple |
| 2 | +//! steps into a single step. |
| 3 | +
|
| 4 | +use { |
| 5 | + crate::{ |
| 6 | + pipelines::step::StepInstance, platform::types::BuiltPayload, prelude::*, |
| 7 | + }, |
| 8 | + std::sync::Arc, |
| 9 | +}; |
| 10 | + |
| 11 | +/// A composite step with a list of steps. |
| 12 | +/// The associated mode defines the specific behavior of executing steps |
| 13 | +pub struct CompositeStep<P: Platform, M> { |
| 14 | + steps: Vec<Arc<StepInstance<P>>>, |
| 15 | + mode: M, |
| 16 | +} |
| 17 | + |
| 18 | +impl<P: Platform, M> CompositeStep<P, M> { |
| 19 | + pub fn new(mode: M) -> Self { |
| 20 | + Self { |
| 21 | + steps: Vec::new(), |
| 22 | + mode, |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn append_step(&mut self, step: impl Step<P>) { |
| 27 | + self.steps.push(Arc::new(StepInstance::new(step))); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +/// A composite step mode defines the specific behavior of executing steps |
| 32 | +/// It takes the list of steps, initial payload and context |
| 33 | +trait CompositeStepMode<P: Platform>: Send + Sync { |
| 34 | + fn steps( |
| 35 | + &self, |
| 36 | + steps: &[Arc<StepInstance<P>>], |
| 37 | + payload: Checkpoint<P>, |
| 38 | + ctx: StepContext<P>, |
| 39 | + ) -> impl Future<Output = ControlFlow<P>> + Send; |
| 40 | +} |
| 41 | + |
| 42 | +/// `CompositeStep` will execute all before/after/setup functions of each step in |
| 43 | +/// order The step implementation is delegated to the composite mode |
| 44 | +impl<P, M> Step<P> for CompositeStep<P, M> |
| 45 | +where |
| 46 | + P: Platform, |
| 47 | + M: CompositeStepMode<P> + Send + 'static, |
| 48 | +{ |
| 49 | + async fn step( |
| 50 | + self: Arc<Self>, |
| 51 | + payload: Checkpoint<P>, |
| 52 | + ctx: StepContext<P>, |
| 53 | + ) -> ControlFlow<P> { |
| 54 | + self.mode.steps(&self.steps, payload, ctx).await |
| 55 | + } |
| 56 | + |
| 57 | + async fn before_job( |
| 58 | + self: Arc<Self>, |
| 59 | + ctx: StepContext<P>, |
| 60 | + ) -> Result<(), PayloadBuilderError> { |
| 61 | + for step in &self.steps { |
| 62 | + step.before_job(ctx.clone()).await?; |
| 63 | + } |
| 64 | + Ok(()) |
| 65 | + } |
| 66 | + |
| 67 | + async fn after_job( |
| 68 | + self: Arc<Self>, |
| 69 | + ctx: StepContext<P>, |
| 70 | + result: Arc<Result<BuiltPayload<P>, PayloadBuilderError>>, |
| 71 | + ) -> Result<(), PayloadBuilderError> { |
| 72 | + for step in &self.steps { |
| 73 | + step.after_job(ctx.clone(), result.clone()).await?; |
| 74 | + } |
| 75 | + Ok(()) |
| 76 | + } |
| 77 | + |
| 78 | + async fn setup( |
| 79 | + &mut self, |
| 80 | + init: InitContext<P>, |
| 81 | + ) -> Result<(), PayloadBuilderError> { |
| 82 | + for step in &self.steps { |
| 83 | + step.setup(init.clone()).await?; |
| 84 | + } |
| 85 | + Ok(()) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +#[cfg(test)] |
| 90 | +mod tests { |
| 91 | + use {super::*, crate::test_utils::*}; |
| 92 | + |
| 93 | + // Composite step mode that tries to execute all steps and ignore all failures |
| 94 | + // and breaks Will return the latest successful result |
| 95 | + struct TryAllMode; |
| 96 | + impl<P: Platform> CompositeStepMode<P> for TryAllMode { |
| 97 | + async fn steps( |
| 98 | + &self, |
| 99 | + steps: &[Arc<StepInstance<P>>], |
| 100 | + payload: Checkpoint<P>, |
| 101 | + ctx: StepContext<P>, |
| 102 | + ) -> ControlFlow<P> { |
| 103 | + let mut current = payload.clone(); |
| 104 | + for step in steps { |
| 105 | + if let ControlFlow::Ok(res) = |
| 106 | + step.step(current.clone(), ctx.clone()).await |
| 107 | + { |
| 108 | + current = res; |
| 109 | + } |
| 110 | + } |
| 111 | + ControlFlow::Ok(current) |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + #[rblib_test(Ethereum)] |
| 116 | + async fn composite_empty_steps<P: TestablePlatform>() -> eyre::Result<()> { |
| 117 | + let composite = CompositeStep::<P, _>::new(TryAllMode); |
| 118 | + let result = OneStep::<P>::new(composite).run().await?; |
| 119 | + assert!(matches!(result, ControlFlow::Ok(_))); |
| 120 | + Ok(()) |
| 121 | + } |
| 122 | + |
| 123 | + #[rblib_test(Ethereum)] |
| 124 | + async fn composite_single_step_ok<P: TestablePlatform>() -> eyre::Result<()> { |
| 125 | + let mut composite = CompositeStep::<P, _>::new(TryAllMode); |
| 126 | + composite.append_step(AlwaysOkStep); |
| 127 | + |
| 128 | + let result = OneStep::<P>::new(composite).run().await?; |
| 129 | + assert!(matches!(result, ControlFlow::Ok(_))); |
| 130 | + Ok(()) |
| 131 | + } |
| 132 | + |
| 133 | + #[rblib_test(Ethereum)] |
| 134 | + async fn composite_multiple_steps_ok<P: TestablePlatform>() -> eyre::Result<()> |
| 135 | + { |
| 136 | + let mut composite = CompositeStep::<P, _>::new(TryAllMode); |
| 137 | + composite.append_step(AlwaysOkStep); |
| 138 | + composite.append_step(AlwaysOkStep); |
| 139 | + composite.append_step(AlwaysOkStep); |
| 140 | + |
| 141 | + let result = OneStep::<P>::new(composite).run().await?; |
| 142 | + assert!(matches!(result, ControlFlow::Ok(_))); |
| 143 | + |
| 144 | + Ok(()) |
| 145 | + } |
| 146 | +} |
0 commit comments