Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/javascript/controllers/drawer_controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ export default class extends Controller {
static targets = ["panel", "scrim", "button", "content"]

connect() {
this.closeOnVisit = () => this.close()
// A refresh of the current page (the poll controller) is a visit too; it must not close the menu.
this.closeOnVisit = (event) => { if (event.detail?.url !== window.location.href) this.close() }
document.addEventListener("turbo:before-visit", this.closeOnVisit)
// Past 64rem the CSS shows the panel in place, so an open drawer must
// let go of the page (inert, scrim) when the viewport grows past it.
Expand Down
3 changes: 3 additions & 0 deletions app/javascript/controllers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,7 @@ application.register("drawer", DrawerController)
import FlashController from "./flash_controller"
application.register("flash", FlashController)

import PollController from "./poll_controller"
application.register("poll", PollController)

export { application }
71 changes: 71 additions & 0 deletions app/javascript/controllers/poll_controller.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { Controller } from "@hotwired/stimulus"
import { Turbo } from "@hotwired/turbo-rails"

// Refreshes the page every `interval` ms while the element is on it, through Turbo's page
// refresh with morphing: the new page is merged into the old one, scroll and focus stay. The
// archive page renders the element only while captures run, so the first page without it ends
// the polling: the morph removes the element and this controller disconnects.
//
// What the refresh must not do: cancel a visit or a form the person started (Turbo.session.refresh
// skips while a visit is current; forms are checked here), wipe what they typed or close a
// <details> they opened (the morph attribute hook below), replace the page with an error page
// when the server hiccups (the fetch response hook), or run in a hidden tab.
export default class extends Controller {
static values = { interval: { type: Number, default: 5000 } }

connect() {
this.onVisibility = () => { if (!document.hidden) this.refresh() }
this.onMorphAttribute = (event) => this.keepLocalState(event)
this.onFetchResponse = (event) => this.skipErrorResponse(event)
document.addEventListener("visibilitychange", this.onVisibility)
document.addEventListener("turbo:before-morph-attribute", this.onMorphAttribute)
document.addEventListener("turbo:before-fetch-response", this.onFetchResponse)
this.schedule()
}

disconnect() {
document.removeEventListener("visibilitychange", this.onVisibility)
document.removeEventListener("turbo:before-morph-attribute", this.onMorphAttribute)
document.removeEventListener("turbo:before-fetch-response", this.onFetchResponse)
clearTimeout(this.timer)
}

schedule() {
clearTimeout(this.timer)
if (document.hidden) return
this.timer = setTimeout(() => this.refresh(), this.intervalValue)
}

refresh() {
const busy = document.hidden || Turbo.navigator.currentVisit || Turbo.navigator.formSubmission
if (!busy) {
this.refreshing = true
Turbo.session.refresh(document.baseURI, { method: "morph", scroll: "preserve" })
}
// The morph keeps this element, and this controller with it, while captures still run, so
// the next tick is set here; connect() does not run again. When the new page has no poll
// element, the morph removes it and disconnect() clears the timer.
this.schedule()
}

// A value the person typed, and a <details> they opened, are theirs, not the server's.
keepLocalState(event) {
const { attributeName } = event.detail
const target = event.target
const typed = attributeName === "value" && (target instanceof HTMLInputElement || target instanceof HTMLTextAreaElement)
const opened = attributeName === "open" && target instanceof HTMLDetailsElement
if (typed || opened) event.preventDefault()
}

// A refresh that gets a 502 from a proxy or a 500 from the app must not replace the page with
// the error page: drop that visit, keep what is shown, and try again at the next tick. The
// visit has to be stopped as well, or it stays current and blocks every later refresh.
skipErrorResponse(event) {
const response = event.detail.fetchResponse
if (this.refreshing && response && !response.succeeded) {
event.preventDefault()
Turbo.navigator.stop()
}
this.refreshing = false
}
}
14 changes: 12 additions & 2 deletions app/views/archives/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,23 @@
</div>
<% end %>

<%#
A page refresh (the poll controller, while captures run) morphs this page and keeps the scroll.
Both metas stay on the finished page too: Turbo reads them from the page it refreshes.
%>

<% content_for :head do %>
<meta name="turbo-refresh-method" content="morph">
<meta name="turbo-refresh-scroll" content="preserve">
<% end %>

<% unless @archive.finished? %>
<div class="callout callout-info" role="status">
<div class="callout callout-info" role="status" data-controller="poll" data-poll-interval-value="5000">
<%= icon(:loader_circle, size: 18, class: "spinner") %>

<div class="callout-body">
<p class="callout-title">Captures are running</p>
<p>Reload this page in a moment.</p>
<p>This page updates itself as they finish.</p>
</div>
</div>
<% end %>
Expand Down
2 changes: 2 additions & 0 deletions docs/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,8 @@ Stash uses `patchwork-central` (not the restricted database), schema `stash`. Ar

## Jobs

While an archive runs, its page refreshes itself every five seconds (the `poll` Stimulus controller, rendered only on an unfinished archive) through Turbo's page refresh with morphing, so the captures fill in without a flash and scroll stays put. The first finished page carries no controller, and the polling ends with it. The refresh never cancels a visit or a form the person started, keeps what they typed and the `<details>` they opened, leaves the page alone when the server answers an error, and pauses in a hidden tab.

`ArchiveJob` makes the capture rows from the router and enqueues one `CaptureJob` per capture. `CaptureJob` runs one capturer, stores its artifacts, and updates the archive's status. The jobs queue is `captures`, with a small concurrency (`GOOD_JOB_MAX_THREADS`, default 2), because Chromium and yt-dlp are heavy.

## Environments
Expand Down
16 changes: 16 additions & 0 deletions test/controllers/archives_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,22 @@ class ArchivesControllerTest < ActionDispatch::IntegrationTest
assert_response :success
assert_includes response.body, archive.public_id
assert_not_includes response.body, "Delete this archive"
# Finished: nothing polls. The morph metas stay, so the last refresh of a running page morphs too.
assert_select "[data-controller=poll]", count: 0
assert_select "meta[name=turbo-refresh-method][content=morph]", count: 1
end

test "an archive that is still running polls for its result and morphs the page" do
archive = create_archive_with_captures({ "page" => "running", "git" => "queued" })
sign_in_as(:viewer)

get archive_path(archive)

assert_response :success
assert_select "[data-controller=poll][data-poll-interval-value='5000']", count: 1
assert_select "meta[name=turbo-refresh-method][content=morph]", count: 1
assert_select "meta[name=turbo-refresh-scroll][content=preserve]", count: 1
assert_includes response.body, "This page updates itself as they finish"
end

test "the list searches by URL, host and note" do
Expand Down
Loading