Skip to content

Commit

Permalink
refactor: add preexecute/1 to join controller
Browse files Browse the repository at this point in the history
to lock tokens
  • Loading branch information
fahchen committed Mar 9, 2022
1 parent 62b3f8a commit cc87db0
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 40 deletions.
16 changes: 16 additions & 0 deletions lib/workflow_metal/controller/join.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ defmodule WorkflowMetal.Controller.Join do
@type task_data :: WorkflowMetal.Task.Task.t()

@type on_task_enablement :: :ok | {:error, :task_not_enabled}
@type on_preexecute :: {:ok, nonempty_list(token_id)} | {:error, :task_not_enabled}

@doc false
@callback task_enablement(task_data) :: on_task_enablement

@doc false
@callback preexecute(task_data) :: on_preexecute

@doc false
@spec task_enablement(task_data) :: on_task_enablement
def task_enablement(task_data) do
Expand All @@ -25,6 +29,18 @@ defmodule WorkflowMetal.Controller.Join do
controller(join_type).task_enablement(task_data)
end

@doc false
@spec preexecute(task_data) :: on_preexecute
def preexecute(task_data) do
%{
transition_schema: %Schema.Transition{
join_type: join_type
}
} = task_data

controller(join_type).preexecute(task_data)
end

defp controller(:none), do: WorkflowMetal.Controller.Join.None
defp controller(:and), do: WorkflowMetal.Controller.Join.And
defp controller(controller_module) when is_atom(controller_module), do: controller_module
Expand Down
18 changes: 15 additions & 3 deletions lib/workflow_metal/controller/join/and.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ defmodule WorkflowMetal.Controller.Join.And do

@impl WorkflowMetal.Controller.Join
def task_enablement(task_data) do
case get_tokens(task_data) do
{:error, _reason} = error -> error
{:ok, _token_ids} -> :ok
end
end

@impl WorkflowMetal.Controller.Join
def preexecute(task_data) do
get_tokens(task_data)
end

defp get_tokens(task_data) do
%{
application: application,
transition_schema: transition_schema,
Expand Down Expand Up @@ -36,9 +48,9 @@ defmodule WorkflowMetal.Controller.Join.And do
end
end)
|> case do
[_ | _] -> :ok
[] -> {:error, :task_not_enabled}
error -> error
{:error, _reason} = error -> error
[] -> {:error, :tokens_not_available}
[_ | _] = token_ids -> {:ok, token_ids}
end
end
end
16 changes: 14 additions & 2 deletions lib/workflow_metal/controller/join/none.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@ defmodule WorkflowMetal.Controller.Join.None do

@impl WorkflowMetal.Controller.Join
def task_enablement(task_data) do
case get_token(task_data) do
{:error, _reason} = error -> error
{:ok, _token_ids} -> :ok
end
end

@impl WorkflowMetal.Controller.Join
def preexecute(task_data) do
get_token(task_data)
end

defp get_token(task_data) do
%{
application: application,
transition_schema: transition_schema,
Expand All @@ -29,8 +41,8 @@ defmodule WorkflowMetal.Controller.Join.None do
]

case :ets.select(token_table, match_spec) do
[_token_id | _rest] ->
:ok
[token_id | _rest] ->
{:ok, [token_id]}

_ ->
{:error, :task_not_enabled}
Expand Down
36 changes: 1 addition & 35 deletions lib/workflow_metal/task/task.ex
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ defmodule WorkflowMetal.Task.Task do

defp do_lock_tokens(%__MODULE__{} = data) do
with(
{:ok, token_ids} <- get_tokens(data),
{:ok, token_ids} <- JoinController.preexecute(data),
%{
application: application,
task_schema: %Schema.Task{id: task_id, case_id: case_id}
Expand All @@ -689,40 +689,6 @@ defmodule WorkflowMetal.Task.Task do
end
end

defp get_tokens(%__MODULE__{} = data) do
%{
application: application,
transition_schema: transition_schema,
token_table: token_table
} = data

{:ok, places} = WorkflowMetal.Storage.fetch_places(application, transition_schema.id, :in)

places
|> Enum.reduce_while([], fn %Schema.Place{} = place, token_ids ->
match_spec = [
{
{:"$1", %{place_id: place.id}, :_},
[],
[:"$1"]
}
]

case :ets.select(token_table, match_spec) do
[token_id | _rest] ->
{:cont, [token_id | token_ids]}

_ ->
{:halt, {:error, :task_not_enabled}}
end
end)
|> case do
[] -> {:error, :tokens_not_available}
[_ | _] = token_ids -> {:ok, token_ids}
error -> error
end
end

defp task_completion(%__MODULE__{} = data) do
data
|> Map.fetch!(:workitem_table)
Expand Down

0 comments on commit cc87db0

Please sign in to comment.