Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable actions for deleted workflows #2671

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ and this project adheres to

- Error when the logger receives a boolean
[#2666](https://github.com/OpenFn/lightning/issues/2666)
- Disable save and run actions on deleted workflows
[#2170](https://github.com/OpenFn/lightning/issues/2170)

## [v2.10.1] - 2024-11-13

Expand Down
26 changes: 23 additions & 3 deletions lib/lightning/work_orders.ex
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ defmodule Lightning.WorkOrders do
create_for(job, workflow: workflow, dataclip: dataclip, user: user)
"""
@spec create_for(Trigger.t() | Job.t(), Multi.t(), [work_order_option()]) ::
{:ok, WorkOrder.t()} | {:error, Ecto.Changeset.t(WorkOrder.t())}
{:ok, WorkOrder.t()}
| {:error, Ecto.Changeset.t(WorkOrder.t()) | :workflow_deleted}
def create_for(target, multi \\ Multi.new(), opts)

def create_for(%Trigger{} = trigger, multi, opts) do
Expand Down Expand Up @@ -118,6 +119,13 @@ defmodule Lightning.WorkOrders do

def create_for(%Manual{} = manual) do
Multi.new()
|> Multi.run(:workflow_deleted?, fn _repo, _changes ->
if manual.workflow.deleted_at do
{:error, :workflow_deleted}
else
{:ok, false}
end
end)
|> get_or_insert_dataclip(manual)
|> Multi.put(:workflow, manual.workflow)
|> get_or_create_snapshot()
Expand Down Expand Up @@ -283,7 +291,7 @@ defmodule Lightning.WorkOrders do
Step.t() | Ecto.UUID.t(),
[work_order_option(), ...]
) ::
{:ok, Run.t()} | {:error, Ecto.Changeset.t()}
{:ok, Run.t()} | {:error, Ecto.Changeset.t() | :workflow_deleted}
def retry(%Run{id: run_id}, %Step{id: step_id}, opts) do
retry(run_id, step_id, opts)
end
Expand All @@ -310,6 +318,13 @@ defmodule Lightning.WorkOrders do
preload: [:job]
)
end)
|> Multi.run(:workflow_deleted?, fn _repo, %{run: run} ->
if run.work_order.workflow.deleted_at do
{:error, :workflow_deleted}
else
{:ok, false}
end
end)
|> Multi.run(:input_dataclip_id, fn
_repo, %{step: %Step{input_dataclip_id: input_dataclip_id}} ->
{:ok, input_dataclip_id}
Expand Down Expand Up @@ -501,7 +516,10 @@ defmodule Lightning.WorkOrders do
retry(run_step.run_id, run_step.step_id, opts)
end)

{:ok, Enum.count(results, fn result -> match?({:ok, _}, result) end), 0}
success_count =
Enum.count(results, fn result -> match?({:ok, _}, result) end)

{:ok, success_count, Enum.count(results) - success_count}
end
end

Expand Down Expand Up @@ -672,6 +690,8 @@ defmodule Lightning.WorkOrders do
defp fetch_retriable_workorders(workorder_ids) do
workorder_ids
|> workorders_with_dataclips_query()
|> join(:inner, [wo], wf in assoc(wo, :workflow), as: :workflow)
|> where([workflow: wf], is_nil(wf.deleted_at))
|> Repo.all()
end

Expand Down
14 changes: 13 additions & 1 deletion lib/lightning/workflows.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,18 @@ defmodule Lightning.Workflows do
def get_workflow(id), do: Repo.get(Workflow, id)

@spec save_workflow(Ecto.Changeset.t(Workflow.t()) | map()) ::
{:ok, Workflow.t()} | {:error, Ecto.Changeset.t(Workflow.t())}
{:ok, Workflow.t()}
| {:error, Ecto.Changeset.t(Workflow.t())}
| {:error, :workflow_deleted}
def save_workflow(%Ecto.Changeset{data: %Workflow{}} = changeset) do
Multi.new()
|> Multi.run(:validate, fn _repo, _changes ->
if is_nil(changeset.data.deleted_at) do
{:ok, true}
else
{:error, :workflow_deleted}
end
end)
|> Multi.insert_or_update(:workflow, changeset)
|> then(fn multi ->
if changeset.changes == %{} do
Expand Down Expand Up @@ -86,6 +95,9 @@ defmodule Lightning.Workflows do
end)

{:error, false}

{:error, _action, reason, _changes} ->
{:error, reason}
end
end

Expand Down
8 changes: 7 additions & 1 deletion lib/lightning_web/live/run_live/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@
socket
|> put_flash(:error, error_text)}

{:error, :workflow_deleted} ->
{:noreply,
socket
|> put_flash(:error, "Runs for deleted workflows cannot be retried")}

{:error, _changeset} ->
{:noreply,
socket
Expand All @@ -359,7 +364,8 @@
"New run#{if count > 1, do: "s", else: ""} enqueued for #{count} workorder#{if count > 1, do: "s", else: ""}"
|> then(fn msg ->
if discarded_count > 0 do
msg <> " (#{discarded_count} were discarded due to wiped dataclip)"
msg <>
" (#{discarded_count} were discarded due to wiped dataclip/workflow being deleted)"

Check warning on line 368 in lib/lightning_web/live/run_live/index.ex

View check run for this annotation

Codecov / codecov/patch

lib/lightning_web/live/run_live/index.ex#L367-L368

Added lines #L367 - L368 were not covered by tests
else
msg
end
Expand Down
Loading