Skip to content

Commit

Permalink
Preload and keep the attempts workorder and project (#1560)
Browse files Browse the repository at this point in the history
When manually running a job, the output pane would flat
(white-out/rerender).

This was caused by the URLs not having the required preloads
(`...workflow.project` in this case).

What was happening is that we _were_ preloading correctly, however when
the attempt is updated a new version of it (without preloads) replaces
it and the LiveView crashes.

It settles down when the Attempt is complete and it no longer updated.

Fixes #1550
  • Loading branch information
stuartc authored Dec 12, 2023
1 parent 1d6c09e commit 3b6c811
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 16 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ and this project adheres to
[#1487](https://github.com/OpenFn/Lightning/issues/1487)
- Initial credential creation `changes` show `after` as `null` rather a value
[#1118](https://github.com/OpenFn/Lightning/issues/1118)
- AttemptViewer flashing/rerendering when Jobs are running
[#1550](https://github.com/OpenFn/Lightning/issues/1550)

## [v0.11.0] - 2023-12-06

Expand Down Expand Up @@ -103,7 +105,6 @@ and this project adheres to
- Fix for missing data in 'created' audit trail events for webhook auth methods
[#1500](https://github.com/OpenFn/Lightning/issues/1500)


## [v0.10.4] - 2023-11-30

### Added
Expand Down
15 changes: 5 additions & 10 deletions lib/lightning_web/live/attempt_live/attempt_viewer_live.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ defmodule LightningWeb.AttemptLive.AttemptViewerLive do

import LightningWeb.AttemptLive.Components
alias LightningWeb.Components.Viewers
alias Lightning.Attempts

alias Phoenix.LiveView.AsyncResult
alias LightningWeb.AttemptLive.Streaming

use Streaming, chunk_size: 100

@impl true
def render(assigns) do
~H"""
Expand All @@ -28,7 +29,7 @@ defmodule LightningWeb.AttemptLive.AttemptViewerLive do
<:value>
<.link
navigate={
~p"/projects/#{attempt.work_order.workflow.project}/runs?#{%{filters: %{workorder_id: attempt.work_order.id}}}"
~p"/projects/#{@project}/runs?#{%{filters: %{workorder_id: attempt.work_order_id}}}"
}
class="hover:underline hover:text-primary-900"
>
Expand All @@ -43,9 +44,7 @@ defmodule LightningWeb.AttemptLive.AttemptViewerLive do
<:label>Attempt</:label>
<:value>
<.link
navigate={
~p"/projects/#{attempt.work_order.workflow.project}/attempts/#{attempt}"
}
navigate={~p"/projects/#{@project}/attempts/#{attempt}"}
class="hover:underline hover:text-primary-900 whitespace-nowrap text-ellipsis"
>
<span class="whitespace-nowrap text-ellipsis">
Expand Down Expand Up @@ -168,18 +167,14 @@ defmodule LightningWeb.AttemptLive.AttemptViewerLive do
|> assign(:output_dataclip, false)
|> assign(:attempt, AsyncResult.loading())
|> assign(:log_lines, AsyncResult.loading())
|> start_async(:attempt, fn ->
Attempts.get(attempt_id, include: [runs: :job, workflow: :project])
end), layout: false}
|> get_attempt_async(attempt_id), layout: false}
end

@impl true
def handle_event("select_run", %{"id" => id}, socket) do
{:noreply, socket |> apply_selected_run_id(id)}
end

use Streaming, chunk_size: 100

def handle_runs_change(socket) do
# either a job_id or a run_id is passed in
# if a run_id is passed in, we can hightlight the log lines immediately
Expand Down
7 changes: 3 additions & 4 deletions lib/lightning_web/live/attempt_live/show.ex
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
defmodule LightningWeb.AttemptLive.Show do
use LightningWeb, :live_view

alias Lightning.Attempts
alias Phoenix.LiveView.AsyncResult

import LightningWeb.AttemptLive.Components
alias LightningWeb.Components.Viewers
alias LightningWeb.AttemptLive.Streaming

use Streaming, chunk_size: 100

on_mount {LightningWeb.Hooks, :project_scope}

@impl true
Expand Down Expand Up @@ -159,8 +160,6 @@ defmodule LightningWeb.AttemptLive.Show do
"""
end

use Streaming, chunk_size: 100

@impl true
def mount(%{"id" => id}, _session, socket) do
{:ok,
Expand All @@ -178,7 +177,7 @@ defmodule LightningWeb.AttemptLive.Show do
|> assign(:output_dataclip, false)
|> assign(:attempt, AsyncResult.loading())
|> assign(:log_lines, AsyncResult.loading())
|> start_async(:attempt, fn -> Attempts.get(id, include: [runs: :job]) end)}
|> get_attempt_async(id)}
end

def handle_runs_change(socket) do
Expand Down
16 changes: 15 additions & 1 deletion lib/lightning_web/live/attempt_live/streaming.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ defmodule LightningWeb.AttemptLive.Streaming do
alias Lightning.Attempts
alias Phoenix.LiveView.AsyncResult

@doc """
Starts an async process that will fetch the attempt with the given ID.
"""
@spec get_attempt_async(Phoenix.LiveView.Socket.t(), Ecto.UUID.t()) ::
Phoenix.LiveView.Socket.t()
def get_attempt_async(socket, attempt_id) do
socket
|> start_async(:attempt, fn ->
Attempts.get(attempt_id, include: [runs: [:job], workflow: [:project]])
end)
end

def add_or_update_run(socket, run) do
%{runs: runs} = socket.assigns

Expand Down Expand Up @@ -223,7 +235,9 @@ defmodule LightningWeb.AttemptLive.Streaming do
|> assign(
attempt: AsyncResult.ok(attempt, updated_attempt),
# set the initial set of runs
runs: updated_attempt.runs |> sort_runs()
runs: updated_attempt.runs |> sort_runs(),
project: updated_attempt.workflow.project,
workflow: updated_attempt.workflow
)
|> assign_async(
:log_lines,
Expand Down

0 comments on commit 3b6c811

Please sign in to comment.