Current section

Files

Jump to
step_flow lib step_flow controllers jobs.ex
Raw

lib/step_flow/controllers/jobs.ex

defmodule StepFlow.Controllers.Jobs do
@moduledoc false
alias StepFlow.Jobs.Job
alias StepFlow.Jobs.Status
@doc """
Returns a formatted message for AMQP orders.
## Examples
iex> get_message(job)
%{job_id: 123, parameters: [{id: "input", type: "string", value: "/path/to/input"}]}
"""
def get_message(%Job{} = job) do
%{
job_id: job.id,
parameters: job.parameters
}
end
def get_last_status(%Job{last_status_id: last_status_id}) when last_status_id == nil, do: nil
def get_last_status(%Job{last_status_id: last_status_id, status: statuses}) do
case statuses do
nil ->
StepFlow.Repo.get(Status, last_status_id)
statuses ->
statuses
|> Enum.find(fn status -> status.id == last_status_id end)
end
end
def get_last_status(job_id) when is_integer(job_id) do
StepFlow.Jobs.get_job(job_id)
|> StepFlow.Repo.preload([:status])
|> get_last_status()
end
def get_last_status(%Status{} = status), do: status
def get_last_status(_status), do: nil
@doc """
Returns the last status id of a list of status.
"""
def get_last_status_id(status) when is_list(status) do
status
|> Enum.sort(fn state_1, state_2 ->
state_1.id < state_2.id
end)
|> List.last()
end
def get_last_status_id(%Status{} = status), do: status
def get_last_status_id(_status), do: nil
@doc """
Returns action linked to status
"""
def get_action(status) do
case status.state do
:queued -> "create"
:ready_to_init -> "init_process"
:ready_to_start -> "start_process"
:update -> "update_process"
:stopped -> "delete"
:error -> "delete"
_ -> "none"
end
end
@doc """
Returns action linked to status as parameter
"""
def get_action_parameter(status) do
action = get_action(status)
[%{"id" => "action", "type" => "string", "value" => action}]
end
end