Packages
journey
0.10.26
0.10.58
0.10.57
0.10.56
0.10.55
0.10.54
0.10.53
0.10.52
0.10.51
0.10.50
0.10.49
0.10.48
0.10.47
0.10.46
0.10.45
0.10.44
0.10.43
0.10.41
0.10.40
0.10.39
0.10.38
0.10.37
0.10.36
0.10.35
0.10.34
0.10.33
0.10.32
0.10.31
0.10.30
0.10.29
0.10.28
0.10.27
0.10.26
0.10.25
0.10.24
0.10.23
0.10.22
0.0.9
retired
0.0.8
0.0.7
0.0.6
0.0.5
0.0.3
0.0.2
Journey is a library for defining and running durable workflows with persistence, reliability, and scalability.
Current section
Files
Jump to
Current section
Files
lib/journey/node/upstream_dependencies.ex
defmodule Journey.Node.UpstreamDependencies do
require Logger
import Journey.Node.Conditions
def unblocked_when({:not, {upstream_node_name, f_condition}} = r)
when is_atom(upstream_node_name) and is_function(f_condition, 1) do
r
end
def unblocked_when(list_of_required_upstream_nodes)
when is_list(list_of_required_upstream_nodes) do
{:and, Enum.map(list_of_required_upstream_nodes, fn node_name -> {node_name, &provided?/1} end)}
end
@doc """
"""
def unblocked_when({upstream_node_name, f_condition} = r)
when is_atom(upstream_node_name) and is_function(f_condition, 1) do
r
end
def unblocked_when({operation, conditions})
when operation in [:and, :or] and is_list(conditions) do
{operation, conditions}
end
def unblocked_when(invalid) do
raise ArgumentError, """
Invalid unblocked_when expression: #{inspect(invalid)}.
"""
end
@doc """
This function is used to define the conditions under which a node is unblocked. It is intended to be used in the `gated_by` option of a node. The function takes a list of required upstream nodes and returns a predicate tree that can be used to check if the node is unblocked.
The predicate tree can be a single node name, a function that takes a value node and returns a boolean, or a combination of these using `:and`, `:or`, and `:not` operations.
The function also supports nested predicate trees, allowing for complex conditions to be defined.
Examples:
```elixir
iex> import Journey.Node
iex> import Journey.Node.Conditions
iex> import Journey.Node.UpstreamDependencies
iex> _graph =
...> Journey.new_graph(
...> "horoscope workflow - unblocked_when doctest",
...> "v1.0.0",
...> [
...> input(:first_name),
...> input(:birth_day),
...> input(:birth_month),
...> input(:suspended),
...> compute(
...> :zodiac_sign,
...> # Computes itself once :birth_month and :birth_day have been provided:
...> [:birth_month, :birth_day],
...> fn %{birth_month: _birth_month, birth_day: _birth_day} ->
...> # Everyone is a Taurus. ;)
...> {:ok, "Taurus"}
...> end
...> ),
...> compute(
...> :horoscope,
...> # Computes itself once :first_name and :zodiac_sign are in place, and if not suspended.
...> unblocked_when({
...> :and,
...> [
...> {:first_name, &provided?/1},
...> {:zodiac_sign, &provided?/1},
...> {:suspended, fn suspended -> suspended.node_value != true end}
...> ]
...> }),
...> fn %{first_name: name, zodiac_sign: zodiac_sign} ->
...> {:ok, "🍪s await, \#{zodiac_sign} \#{name}!"}
...> end
...> )
...> ]
...> )
iex>
```
"""
def unblocked_when(upstream_node_name, f_condition) do
unblocked_when({upstream_node_name, f_condition})
end
end