Packages
journey
0.0.2
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/execution_store.ex
defmodule Journey.ExecutionStore do
use Agent
@moduledoc false
@doc """
Starts a new execution store.
"""
def start_link(_opts) do
Agent.start_link(fn -> %{} end, name: __MODULE__)
end
@spec get(String.t()) :: Journey.Execution.t() | nil
@doc """
Gets an execution by id.
"""
def get(execution_id) do
Agent.get(__MODULE__, fn state ->
# IO.inspect(state, label: "get/current state")
Map.get(state, execution_id)
end)
end
@spec put(%Journey.Execution{}) :: Journey.Execution.t()
@doc """
Stores an execution.
"""
def put(execution) do
:ok =
Agent.update(__MODULE__, fn storage ->
# execution = %{execution | save_version: execution.save_version + 1}
Map.put(storage, execution.execution_id, execution)
end)
get(execution.execution_id)
end
@spec update_value(String.t(), atom(), any, any) :: {atom(), Journey.Execution.t()}
@doc """
Updates a value in an execution.
"""
def update_value(execution_id, value_name, expected_status, value) do
Agent.get_and_update(__MODULE__, fn storage ->
case storage[execution_id] do
nil ->
{{:unknown_execution_id, nil}, storage}
execution ->
old_values = execution.values
case old_values[value_name] do
nil ->
{{:unknown_step, execution}, storage}
current_value ->
# credo:disable-for-next-line Credo.Check.Refactor.Nesting
if expected_status in [:any, current_value.status] do
new_values = Map.put(old_values, value_name, value)
new_execution = Map.put(execution, :values, new_values)
new_execution = %{new_execution | save_version: new_execution.save_version + 1}
new_storage = Map.put(storage, execution_id, new_execution)
{{:ok, new_execution}, new_storage}
else
{{:not_updated_due_to_current_status, execution}, storage}
end
end
end
end)
end
end