From 0ecc7dc10c9f269f0a03e5afae81b29f7f9203ae Mon Sep 17 00:00:00 2001 From: Alteras1 Date: Fri, 18 Sep 2026 20:48:04 +0000 Subject: [PATCH] FIX: Reset the BBCode engine in every process Previously `PrettyText.reset_content` only resets the V8 in the worker that the controller was in. Now it publishes a message via messagebus, allowing all workers to reset. It also notifies open sessions with a dismissible banner that the site should be reloaded. --- .../bb_code/admin/refresh_controller.rb | 2 ++ .../discourse/api-initializers/engine-reset.js | 16 ++++++++++++++++ config/locales/client.en.yml | 3 ++- plugin.rb | 6 ++++++ 4 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 assets/javascripts/discourse/api-initializers/engine-reset.js diff --git a/app/controllers/bb_code/admin/refresh_controller.rb b/app/controllers/bb_code/admin/refresh_controller.rb index 9f063a1..a84ff03 100644 --- a/app/controllers/bb_code/admin/refresh_controller.rb +++ b/app/controllers/bb_code/admin/refresh_controller.rb @@ -5,6 +5,8 @@ module Admin class RefreshController < ::Admin::AdminController requires_plugin PLUGIN_NAME def index + MessageBus.publish(::BbCode::ENGINE_RESET_CHANNEL, {}) + PrettyText.reset_context begin # Skip warmup in development mode - it makes boot take ~2s longer diff --git a/assets/javascripts/discourse/api-initializers/engine-reset.js b/assets/javascripts/discourse/api-initializers/engine-reset.js new file mode 100644 index 0000000..006d895 --- /dev/null +++ b/assets/javascripts/discourse/api-initializers/engine-reset.js @@ -0,0 +1,16 @@ +import { apiInitializer } from "discourse/lib/api"; +import { i18n } from "discourse-i18n"; + +const RESET_CHANNEL = "/bbcode/engine-reset"; + +export default apiInitializer((api) => { + api.container.lookup("service:message-bus").subscribe(RESET_CHANNEL, () => { + // The markdown-it feature is baked into the app bundle, so a reload is the + // only way to pick it up + api.addGlobalNotice(i18n("bbcode.reload_notice"), "bbcode-engine-reset", { + dismissable: true, + level: "info", + persistentDismiss: false, + }); + }); +}); diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index f7e79e1..129fc5f 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -2,7 +2,8 @@ en: js: bbcode: title: "BBCode" - resetDesc: "The following resets the Markdown-It engine in the backend. This allows the engine to load in the latest code from the custom parser." + resetDesc: "Loads the latest custom parser code into every server process, without a restart. Open browser sessions are shown a banner asking them to refresh, since previews run the parser in the browser." + reload_notice: "The BBCode parser was updated. Refresh the page to use the latest version." reset: "Reset BBCode Engine" success: "Successfully reset." error: "Failed to reset. Please check logs." diff --git a/plugin.rb b/plugin.rb index c316a0c..48e8462 100644 --- a/plugin.rb +++ b/plugin.rb @@ -22,6 +22,8 @@ module ::BbCode PLUGIN_NAME = "BbCode" + + ENGINE_RESET_CHANNEL = "/bbcode/engine-reset" end require_relative "lib/bb_code/engine" @@ -31,6 +33,10 @@ module ::BbCode # should clear out the context so the initial setup logic for bbcode parser runs PrettyText.reset_context() + unless Rails.env.test? + MessageBus.subscribe(::BbCode::ENGINE_RESET_CHANNEL) { PrettyText.reset_context } + end + # overrides the default normalize_whitespaces function in discourse/lib/text_cleaner.rb # adds discourse_normalize_whitespace setting (defaults to false) # when true, normalize_whitespace runs as normal