Packages
journey
0.10.34
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/conditions.ex
defmodule Journey.Node.Conditions do
@moduledoc """
This module contains helper functions for use when defining upstream dependencies for `compute` modules.
"""
@doc """
This is a helper function provided for use in `unblocked_when` conditions.
This function checks if the supplied node has a value.
For "scheduled" types of nodes (`schedule_once`, `schedule_recurring`) it also checks that the scheduled time has come).
## Examples
```elixir
iex> import Journey.Node
iex> import Journey.Node.Conditions
iex> import Journey.Node.UpstreamDependencies
iex> graph = Journey.new_graph(
...> "greeting workflow, doctest for provided?",
...> "v1.0.0",
...> [
...> input(:name),
...> compute(
...> :greeting,
...> unblocked_when(:name, &provided?/1),
...> fn %{name: name} -> {:ok, "Hello, \#{name}!"} end
...> )
...> ]
...> )
iex> execution = Journey.start_execution(graph)
iex> execution = Journey.set(execution, :name, "Alice")
iex> Journey.get_value(execution, :greeting, wait_any: true)
{:ok, "Hello, Alice!"}
```
"""
def provided?(%{node_type: node_type} = value_node) when node_type in [:schedule_once, :schedule_recurring] do
now = System.system_time(:second)
due_in_seconds = if value_node.node_value == nil, do: nil, else: value_node.node_value - now
value_node.set_time != nil and due_in_seconds != nil and due_in_seconds <= 0
end
def provided?(value_node), do: value_node.set_time != nil
@doc """
This is a helper function provided for use in `unblocked_when` conditions.
This function checks if the upstream node's value is `true`.
## Examples
```elixir
iex> import Journey.Node
iex> import Journey.Node.Conditions
iex> import Journey.Node.UpstreamDependencies
iex> graph = Journey.new_graph(
...> "umbrella forecast graph, doctest for true?",
...> "v1.0.0",
...> [
...> input(:it_will_rain_tomorrow),
...> compute(
...> :umbrella,
...> unblocked_when(:it_will_rain_tomorrow, &true?/1),
...> fn %{it_will_rain_tomorrow: true} -> {:ok, "need to pack my umbrella"} end
...> )
...> ]
...> )
iex> execution = Journey.start_execution(graph)
iex> execution = Journey.set(execution, :it_will_rain_tomorrow, true)
iex> Journey.get_value(execution, :umbrella, wait_any: true)
{:ok, "need to pack my umbrella"}
iex> execution = Journey.set(execution, :it_will_rain_tomorrow, false)
iex> Journey.get_value(execution, :umbrella)
{:error, :not_set}
```
"""
def true?(value_node), do: value_node.set_time != nil and value_node.node_value == true
@doc """
This is a helper function provided for use in `unblocked_when` conditions.
This function checks if the upstream node's value is `false`.
## Examples
```elixir
iex> import Journey.Node
iex> import Journey.Node.Conditions
iex> import Journey.Node.UpstreamDependencies
iex> graph = Journey.new_graph(
...> "umbrella forecast graph, doctest for false?",
...> "v1.0.0",
...> [
...> input(:it_will_rain_tomorrow),
...> compute(
...> :todays_preparation,
...> unblocked_when(:it_will_rain_tomorrow, &false?/1),
...> fn %{it_will_rain_tomorrow: false} -> {:ok, "prepare my bike"} end
...> )
...> ]
...> )
iex> execution = Journey.start_execution(graph)
iex> execution = Journey.set(execution, :it_will_rain_tomorrow, false)
iex> Journey.get_value(execution, :todays_preparation, wait_any: true)
{:ok, "prepare my bike"}
iex> execution = Journey.set(execution, :it_will_rain_tomorrow, true)
iex> Journey.get_value(execution, :todays_preparation)
{:error, :not_set}
```
"""
def false?(value_node), do: value_node.set_time != nil and value_node.node_value == false
end