@@ -15,69 +15,68 @@ defmodule Opus.Pipeline.Stage do
1515
1616 alias Opus . { Safe , PipelineError }
1717
18- def maybe_run ( { module , type , name , % { if: :anonymous , stage_id: id } = opts } , input ) do
19- callback = ( module . _opus_callbacks [ id ] |> Enum . find ( fn % { type: t } -> t == :if end ) ) . name
20- maybe_run ( { module , type , name , % { opts | if: { module , callback , [ input ] } } } , input )
21- end
22-
23- def maybe_run ( { module , type , name , % { if: fun } = opts } = _stage , input )
24- when is_atom ( fun ) ,
25- do: maybe_run ( { module , type , name , % { opts | if: { module , fun , [ input ] } } } , input )
26-
27- def maybe_run ( { module , :skip , name , % { if: { _m , _f , _a } = condition } } , input ) do
28- case Safe . apply ( condition ) do
29- true ->
30- module . instrument ( :pipeline_skipped , % { stage: % { pipeline: module , name: name } } , % {
31- stage: name ,
32- input: input
33- } )
34-
35- # Stop the pipeline execution
36- :pipeline_skipped
18+ @ doc false
19+ def maybe_run (
20+ { module , type , name , % { conditional: { cond_type , :anonymous } , stage_id: id } = opts } ,
21+ input
22+ ) do
23+ callback =
24+ ( module . _opus_callbacks [ id ] |> Enum . find ( fn % { type: t } -> t == :conditional end ) ) . name
3725
38- _ ->
39- nil
40- end
26+ maybe_run (
27+ { module , type , name , % { opts | conditional: { cond_type , { module , callback , [ input ] } } } } ,
28+ input
29+ )
4130 end
4231
43- def maybe_run ( { module , _type , name , % { if: { _m , _f , _a } = condition } = opts } = stage , input ) do
44- case Safe . apply ( condition ) do
45- true ->
46- with_retries ( { module , opts } , fn -> do_run ( stage , input ) end )
47-
48- _ ->
49- module . instrument ( :stage_skipped , % { stage: % { pipeline: module , name: name } } , % {
50- stage: name ,
51- input: input
52- } )
32+ def maybe_run (
33+ { module , :skip , name , % { conditional: { cond_type , { _m , _f , _a } = condition } } } ,
34+ input
35+ ) do
36+ if eval_condition ( cond_type , condition ) do
37+ module . instrument ( :pipeline_skipped , % { stage: % { pipeline: module , name: name } } , % {
38+ stage: name ,
39+ input: input
40+ } )
5341
54- # Ignore this stage
55- :stage_skipped
42+ # Stop the pipeline execution
43+ :pipeline_skipped
44+ else
45+ nil
5646 end
5747 end
5848
59- def maybe_run ( { module , type , name , % { unless: :anonymous , stage_id: id } = opts } , input ) do
60- callback = ( module . _opus_callbacks [ id ] |> Enum . find ( fn % { type: t } -> t == :unless end ) ) . name
61- maybe_run ( { module , type , name , % { opts | unless: { module , callback , [ input ] } } } , input )
49+ def maybe_run (
50+ {
51+ module ,
52+ type ,
53+ name ,
54+ % { conditional: { cond_type , fun } } = opts
55+ } = _stage ,
56+ input
57+ )
58+ when is_atom ( fun ) do
59+ maybe_run (
60+ { module , type , name , % { opts | conditional: { cond_type , { module , fun , [ input ] } } } } ,
61+ input
62+ )
6263 end
6364
64- def maybe_run ( { module , type , name , % { unless: fun } = opts } = _stage , input )
65- when is_atom ( fun ) ,
66- do: maybe_run ( { module , type , name , % { opts | unless: { module , fun , [ input ] } } } , input )
67-
68- def maybe_run ( { module , _type , name , % { unless: { _m , _f , _a } = condition } = opts } = stage , input ) do
69- case Safe . apply ( condition ) do
70- false ->
71- with_retries ( { module , opts } , fn -> do_run ( stage , input ) end )
72-
73- _ ->
74- module . instrument ( :stage_skipped , % { stage: % { pipeline: module , name: name } } , % {
75- stage: name ,
76- input: input
77- } )
65+ def maybe_run (
66+ { module , _type , name , % { conditional: { cond_type , { _m , _f , _a } = condition } } = opts } =
67+ stage ,
68+ input
69+ ) do
70+ if eval_condition ( cond_type , condition ) do
71+ with_retries ( { module , opts } , fn -> do_run ( stage , input ) end )
72+ else
73+ module . instrument ( :stage_skipped , % { stage: % { pipeline: module , name: name } } , % {
74+ stage: name ,
75+ input: input
76+ } )
7877
79- # Ignore this stage
80- :stage_skipped
78+ # Ignore this stage
79+ :stage_skipped
8180 end
8281 end
8382
@@ -201,4 +200,12 @@ defmodule Opus.Pipeline.Stage do
201200
202201 defp do_run ( { module , type , name , % { } = opts } , input ) ,
203202 do: do_run ( { module , type , name , Map . merge ( opts , % { with: { module , name , [ input ] } } ) } , input )
203+
204+ defp eval_condition ( cond_type , condition ) do
205+ case { cond_type , Safe . apply ( condition ) } do
206+ { :if , true } -> true
207+ { :unless , false } -> true
208+ _ -> false
209+ end
210+ end
204211end
0 commit comments