diff --git a/app/javascript/solid_queue_web/application.js b/app/javascript/solid_queue_web/application.js index ad1349d..7e08a27 100644 --- a/app/javascript/solid_queue_web/application.js +++ b/app/javascript/solid_queue_web/application.js @@ -1,6 +1,8 @@ import "@hotwired/turbo" import { Application } from "@hotwired/stimulus" import SearchController from "solid_queue_web/search_controller" +import RefreshController from "solid_queue_web/refresh_controller" const application = Application.start() application.register("search", SearchController) +application.register("refresh", RefreshController) diff --git a/app/javascript/solid_queue_web/refresh_controller.js b/app/javascript/solid_queue_web/refresh_controller.js new file mode 100644 index 0000000..f0eedf6 --- /dev/null +++ b/app/javascript/solid_queue_web/refresh_controller.js @@ -0,0 +1,51 @@ +import { Controller } from "@hotwired/stimulus" + +export default class extends Controller { + static values = { interval: { type: Number, default: 5000 } } + + initialize() { + this._onVisibilityChange = this._onVisibilityChange.bind(this) + } + + connect() { + document.addEventListener("visibilitychange", this._onVisibilityChange) + this._schedule() + } + + disconnect() { + clearTimeout(this._timer) + document.removeEventListener("visibilitychange", this._onVisibilityChange) + } + + _schedule() { + this._timer = setTimeout(() => this._reload(), this.intervalValue) + } + + async _reload() { + clearTimeout(this._timer) + if (!document.hidden) { + try { + const response = await fetch(window.location.href, { + headers: { "Turbo-Frame": this.element.id, Accept: "text/html" } + }) + if (response.ok) { + const html = await response.text() + const doc = new DOMParser().parseFromString(html, "text/html") + const frame = doc.querySelector(`turbo-frame#${this.element.id}`) + if (frame && this.element.isConnected) this.element.innerHTML = frame.innerHTML + } + } catch { + // network error — skip this tick + } + } + if (this.element.isConnected) this._schedule() + } + + _onVisibilityChange() { + if (document.hidden) { + clearTimeout(this._timer) + } else { + this._reload() + } + } +} \ No newline at end of file diff --git a/app/views/solid_queue_web/dashboard/index.html.erb b/app/views/solid_queue_web/dashboard/index.html.erb index 56d5a38..96d3934 100644 --- a/app/views/solid_queue_web/dashboard/index.html.erb +++ b/app/views/solid_queue_web/dashboard/index.html.erb @@ -1,3 +1,4 @@ +<%= turbo_frame_tag "dashboard", target: "_top", data: { controller: "refresh", refresh_interval_value: 5000 } do %>

Dashboard

@@ -62,4 +63,5 @@
<% end %> - \ No newline at end of file + +<% end %> \ No newline at end of file diff --git a/app/views/solid_queue_web/jobs/index.html.erb b/app/views/solid_queue_web/jobs/index.html.erb index 44c64c0..c095a4d 100644 --- a/app/views/solid_queue_web/jobs/index.html.erb +++ b/app/views/solid_queue_web/jobs/index.html.erb @@ -1,6 +1,6 @@

Jobs

-<%= turbo_frame_tag "jobs-table", data: { turbo_action: "advance" } do %> +<%= turbo_frame_tag "jobs-table", data: { turbo_action: "advance", controller: "refresh", refresh_interval_value: 10000 } do %> <% discardable = SolidQueueWeb::Job::DISCARDABLE.include?(@status) %>
diff --git a/app/views/solid_queue_web/processes/index.html.erb b/app/views/solid_queue_web/processes/index.html.erb index 7d1f0ba..6d5f8e0 100644 --- a/app/views/solid_queue_web/processes/index.html.erb +++ b/app/views/solid_queue_web/processes/index.html.erb @@ -1,3 +1,4 @@ +<%= turbo_frame_tag "processes", target: "_top", data: { controller: "refresh", refresh_interval_value: 10000 } do %>

Processes

@@ -51,4 +52,5 @@ <% end %> -
\ No newline at end of file +
+<% end %> \ No newline at end of file diff --git a/config/importmap.rb b/config/importmap.rb index 75bc253..ca0bd21 100644 --- a/config/importmap.rb +++ b/config/importmap.rb @@ -1,2 +1,3 @@ pin "solid_queue_web", to: "solid_queue_web/application.js" pin "solid_queue_web/search_controller", to: "solid_queue_web/search_controller.js" +pin "solid_queue_web/refresh_controller", to: "solid_queue_web/refresh_controller.js" diff --git a/spec/dummy/bin/rails b/spec/dummy/bin/rails new file mode 100755 index 0000000..efc0377 --- /dev/null +++ b/spec/dummy/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path("../config/application", __dir__) +require_relative "../config/boot" +require "rails/commands"