Skip to content

A platform for community discussion. Free, open, simple.

FIX: Keep workflow nodes usable after code reloads (#43160)

Martin Brennan704277e1063d
diff --git a/plugins/chat/plugin.rb b/plugins/chat/plugin.rb
index 01a65cd35f5..e8db23b28ae 100644
--- a/plugins/chat/plugin.rb
+++ b/plugins/chat/plugin.rb
@@ -112,12 +112,6 @@ after_initialize do
if respond_to?(:register_discourse_workflows_node)
register_discourse_workflows_node do
- require_relative "lib/discourse_workflows/nodes/chat_channel_selection"
- require_relative "lib/discourse_workflows/nodes/send_chat_message/v1"
- require_relative "lib/discourse_workflows/nodes/chat_approval/v1"
- require_relative "lib/discourse_workflows/nodes/chat_approval/v2"
- require_relative "lib/discourse_workflows/nodes/chat_message_created/v1"
-
[
DiscourseWorkflows::Nodes::SendChatMessage::V1,
DiscourseWorkflows::Nodes::ChatApproval::V1,
diff --git a/plugins/discourse-ai/plugin.rb b/plugins/discourse-ai/plugin.rb
index 950076f0955..5e9e3a81359 100644
--- a/plugins/discourse-ai/plugin.rb
+++ b/plugins/discourse-ai/plugin.rb
@@ -116,10 +116,7 @@ after_initialize do
require_relative "discourse_automation/llm_tagger"
if respond_to?(:register_discourse_workflows_node)
- register_discourse_workflows_node do
- require_relative "discourse_workflows/nodes/ai_agent/v1"
- DiscourseWorkflows::Nodes::AiAgent::V1
- end
+ register_discourse_workflows_node { DiscourseWorkflows::Nodes::AiAgent::V1 }
end
add_admin_route("discourse_ai.title", "discourse-ai", { use_new_show_route: true })
diff --git a/plugins/discourse-assign/plugin.rb b/plugins/discourse-assign/plugin.rb
index 5debe1864c7..1a8cef41868 100644
--- a/plugins/discourse-assign/plugin.rb
+++ b/plugins/discourse-assign/plugin.rb
@@ -27,10 +27,6 @@ require_relative "lib/validators/assign_statuses_validator"
after_initialize do
if respond_to?(:register_discourse_workflows_node)
register_discourse_workflows_node do
- require_relative "lib/discourse_workflows/nodes/assign_topic/v1"
- require_relative "lib/discourse_workflows/nodes/assigned/v1"
- require_relative "lib/discourse_workflows/nodes/unassigned/v1"
- require_relative "lib/discourse_workflows/nodes/check_assignment/v1"
[
DiscourseWorkflows::Nodes::AssignTopic::V1,
DiscourseWorkflows::Nodes::Assigned::V1,
diff --git a/plugins/discourse-chat-integration/lib/discourse_workflows/nodes/channel_selection.rb b/plugins/discourse-chat-integration/lib/discourse_workflows/nodes/chat_integration_channel_selection.rb
similarity index 100%
rename from plugins/discourse-chat-integration/lib/discourse_workflows/nodes/channel_selection.rb
rename to plugins/discourse-chat-integration/lib/discourse_workflows/nodes/chat_integration_channel_selection.rb
diff --git a/plugins/discourse-chat-integration/plugin.rb b/plugins/discourse-chat-integration/plugin.rb
index 3340dcb9e68..24b3543af69 100644
--- a/plugins/discourse-chat-integration/plugin.rb
+++ b/plugins/discourse-chat-integration/plugin.rb
@@ -46,9 +46,6 @@ after_initialize do
if respond_to?(:register_discourse_workflows_node)
register_discourse_workflows_node do
- require_relative "lib/discourse_workflows/nodes/channel_selection"
- require_relative "lib/discourse_workflows/nodes/send_chat_integration_message/v1"
-
[DiscourseWorkflows::Nodes::SendChatIntegrationMessage::V1]
end
end
diff --git a/plugins/discourse-events/lib/discourse_workflows/nodes/event_ended/v1.rb b/plugins/discourse-events/lib/discourse_workflows/nodes/event_ended/v1.rb
index 454be08259a..c7027da6bf3 100644
--- a/plugins/discourse-events/lib/discourse_workflows/nodes/event_ended/v1.rb
+++ b/plugins/discourse-events/lib/discourse_workflows/nodes/event_ended/v1.rb
@@ -18,9 +18,7 @@ if defined?(DiscourseWorkflows)
event: :discourse_post_event_event_ended,
available: -> { SiteSetting.discourse_post_event_enabled },
unavailable_reason_key: "discourse_workflows.node_unavailable.requires_post_event",
- output_contracts: [
- { schema: DiscourseEvents::Events::Workflows::Schema::EVENT_ENDED_OUTPUT_SCHEMA },
- ],
+ output_contracts: [{ schema: PostEventSchema::EVENT_ENDED_OUTPUT_SCHEMA }],
properties: PostEventScoping::SCOPE_PROPERTIES,
i18n_scope: PostEventScoping::I18N_SCOPE,
)
diff --git a/plugins/discourse-events/lib/discourse_workflows/nodes/event_participation_changed/v1.rb b/plugins/discourse-events/lib/discourse_workflows/nodes/event_participation_changed/v1.rb
index d3f8b7fbf40..7bbf14a8fc9 100644
--- a/plugins/discourse-events/lib/discourse_workflows/nodes/event_participation_changed/v1.rb
+++ b/plugins/discourse-events/lib/discourse_workflows/nodes/event_participation_changed/v1.rb
@@ -19,10 +19,7 @@ if defined?(DiscourseWorkflows)
available: -> { SiteSetting.discourse_post_event_enabled },
unavailable_reason_key: "discourse_workflows.node_unavailable.requires_post_event",
output_contracts: [
- {
- schema:
- DiscourseEvents::Events::Workflows::Schema::EVENT_PARTICIPATION_CHANGED_OUTPUT_SCHEMA,
- },
+ { schema: PostEventSchema::EVENT_PARTICIPATION_CHANGED_OUTPUT_SCHEMA },
],
properties: PostEventScoping::SCOPE_PROPERTIES,
i18n_scope: PostEventScoping::I18N_SCOPE,
diff --git a/plugins/discourse-events/lib/discourse_events/events/workflows/schema.rb b/plugins/discourse-events/lib/discourse_workflows/nodes/post_event_schema.rb
similarity index 97%
rename from plugins/discourse-events/lib/discourse_events/events/workflows/schema.rb
rename to plugins/discourse-events/lib/discourse_workflows/nodes/post_event_schema.rb
index 5c2af49eee6..ca81b1d8777 100644
--- a/plugins/discourse-events/lib/discourse_events/events/workflows/schema.rb
+++ b/plugins/discourse-events/lib/discourse_workflows/nodes/post_event_schema.rb
@@ -1,9 +1,9 @@
# frozen_string_literal: true
-module DiscourseEvents
- module Events
- module Workflows
- module Schema
+if defined?(DiscourseWorkflows)
+ module DiscourseWorkflows
+ module Nodes
+ module PostEventSchema
EVENT_PROPERTIES = JSON.parse(<<~JSON).freeze
{
"id": { "type": "integer" },
diff --git a/plugins/discourse-events/plugin.rb b/plugins/discourse-events/plugin.rb
index 86a3a251c9d..c5ac21b3e00 100644
--- a/plugins/discourse-events/plugin.rb
+++ b/plugins/discourse-events/plugin.rb
@@ -234,11 +234,6 @@ Dir
after_initialize do
if respond_to?(:register_discourse_workflows_node)
register_discourse_workflows_node do
- require_relative "lib/discourse_events/events/workflows/schema"
- require_relative "lib/discourse_workflows/nodes/post_event_scoping"
- require_relative "lib/discourse_workflows/nodes/event_ended/v1"
- require_relative "lib/discourse_workflows/nodes/event_participation_changed/v1"
-
[
DiscourseWorkflows::Nodes::EventEnded::V1,
DiscourseWorkflows::Nodes::EventParticipationChanged::V1,
diff --git a/plugins/discourse-gamification/plugin.rb b/plugins/discourse-gamification/plugin.rb
index cb5f0c5c2d6..a42fe3e0277 100644
--- a/plugins/discourse-gamification/plugin.rb
+++ b/plugins/discourse-gamification/plugin.rb
@@ -73,10 +73,7 @@ after_initialize do
end
if respond_to?(:register_discourse_workflows_node)
- register_discourse_workflows_node do
- require_relative "lib/discourse_workflows/nodes/gamification_score/v1"
- DiscourseWorkflows::Nodes::GamificationScore::V1
- end
+ register_discourse_workflows_node { DiscourseWorkflows::Nodes::GamificationScore::V1 }
end
add_directory_column(
diff --git a/plugins/discourse-topic-voting/plugin.rb b/plugins/discourse-topic-voting/plugin.rb
index 7503de462b7..c075003c296 100755
--- a/plugins/discourse-topic-voting/plugin.rb
+++ b/plugins/discourse-topic-voting/plugin.rb
@@ -35,10 +35,7 @@ after_initialize do
SeedFu.fixture_paths << Rails.root.join("plugins/discourse-topic-voting/db/fixtures").to_s
if respond_to?(:register_discourse_workflows_node)
- register_discourse_workflows_node do
- require_relative "lib/discourse_workflows/nodes/topic_received_vote/v1"
- DiscourseWorkflows::Nodes::TopicReceivedVote::V1
- end
+ register_discourse_workflows_node { DiscourseWorkflows::Nodes::TopicReceivedVote::V1 }
end
reloadable_patch do
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/engine.rb b/plugins/discourse-workflows/lib/discourse_workflows/engine.rb
index 8a47e56f4b8..ca0cb67edca 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/engine.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/engine.rb
@@ -5,6 +5,19 @@ module DiscourseWorkflows
engine_name PLUGIN_NAME
isolate_namespace DiscourseWorkflows
config.autoload_paths << File.join(config.root, "lib")
+ initializer "discourse_workflows.plugin_autoloading", before: :setup_main_autoloader do
+ Discourse.plugins.each do |plugin|
+ next if plugin.name == PLUGIN_NAME
+
+ %w[lib/discourse_workflows discourse_workflows].each do |relative_path|
+ directory = File.join(plugin.directory, relative_path)
+ if Dir.exist?(directory)
+ Rails.autoloaders.main.push_dir(directory, namespace: DiscourseWorkflows)
+ end
+ end
+ end
+ end
+
scheduled_job_dir = "#{config.root}/app/jobs/scheduled"
config.to_prepare do
Rails.autoloaders.main.eager_load_dir(scheduled_job_dir) if Dir.exist?(scheduled_job_dir)
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/plugin_node_registration.rb b/plugins/discourse-workflows/lib/discourse_workflows/plugin_node_registration.rb
index d1ed12e8c60..6f85a644491 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/plugin_node_registration.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/plugin_node_registration.rb
@@ -15,7 +15,10 @@ module DiscourseWorkflows
def register_node(node_class, plugin)
return if node_registered?(node_class)
- DiscoursePluginRegistry.register_discourse_workflows_node(node_class, plugin)
+ DiscoursePluginRegistry.register_discourse_workflows_node(
+ node_class.name || node_class,
+ plugin,
+ )
subscribe_node_event(node_class, plugin)
end
@@ -45,7 +48,7 @@ module DiscourseWorkflows
def node_registered?(node_class)
DiscoursePluginRegistry._raw_discourse_workflows_nodes.any? do |entry|
- entry[:value] == node_class
+ entry[:value] == (node_class.name || node_class)
end
end
@@ -55,7 +58,12 @@ module DiscourseWorkflows
event_name = node_class.event_name if node_class.respond_to?(:event_name)
return if event_name.blank?
- plugin.on(event_name) { |*args| EventListener.handle(node_class, *args) }
+ # Event handlers outlive Zeitwerk reloads, so resolve named classes at dispatch.
+ node_reference = node_class.name || node_class
+ plugin.on(event_name) do |*args|
+ current_class = node_reference.is_a?(String) ? node_reference.constantize : node_reference
+ EventListener.handle(current_class, *args)
+ end
end
end
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/registry.rb b/plugins/discourse-workflows/lib/discourse_workflows/registry.rb
index d849ab172a2..34b8277b1a9 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/registry.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/registry.rb
@@ -8,11 +8,13 @@ module DiscourseWorkflows
class << self
def nodes(include_disabled_plugins: false)
- if include_disabled_plugins
- DiscoursePluginRegistry._raw_discourse_workflows_nodes.map { |entry| entry[:value] }.uniq
- else
- DiscoursePluginRegistry.discourse_workflows_nodes
- end
+ references =
+ if include_disabled_plugins
+ DiscoursePluginRegistry._raw_discourse_workflows_nodes.map { |entry| entry[:value] }
+ else
+ DiscoursePluginRegistry.discourse_workflows_nodes
+ end
+ references.map { |reference| resolve_class(reference) }.uniq
end
def triggers
@@ -32,11 +34,13 @@ module DiscourseWorkflows
end
def credential_types
- DiscoursePluginRegistry.discourse_workflows_credential_types
+ DiscoursePluginRegistry.discourse_workflows_credential_types.map do |reference|
+ resolve_class(reference)
+ end
end
def find_credential_type(identifier)
- credential_type_index[identifier]
+ resolve_class(credential_type_index[identifier])
end
def find_node_type(identifier, version: nil, include_disabled_plugins: false)
@@ -58,6 +62,10 @@ module DiscourseWorkflows
private
+ def resolve_class(reference)
+ reference.is_a?(String) ? reference.constantize : reference
+ end
+
def node_type_index(include_disabled_plugins: false)
nodes(include_disabled_plugins: include_disabled_plugins).each_with_object({}) do |klass, h|
version = klass.respond_to?(:version) ? klass.version : DEFAULT_VERSION
@@ -79,7 +87,8 @@ module DiscourseWorkflows
end
def credential_type_index
- @credential_type_index ||= credential_types.index_by(&:identifier)
+ @credential_type_index ||=
+ credential_types.to_h { |klass| [klass.identifier, klass.name || klass] }
end
end
end
diff --git a/plugins/discourse-workflows/plugin.rb b/plugins/discourse-workflows/plugin.rb
index 38686a0a9f7..10be13bf565 100644
--- a/plugins/discourse-workflows/plugin.rb
+++ b/plugins/discourse-workflows/plugin.rb
@@ -81,15 +81,15 @@ after_initialize do
DiscourseWorkflows::Registry.reset_indexes!
DiscoursePluginRegistry.register_discourse_workflows_credential_type(
- DiscourseWorkflows::CredentialTypes::BasicAuth,
+ "DiscourseWorkflows::CredentialTypes::BasicAuth",
self,
)
DiscoursePluginRegistry.register_discourse_workflows_credential_type(
- DiscourseWorkflows::CredentialTypes::BearerToken,
+ "DiscourseWorkflows::CredentialTypes::BearerToken",
self,
)
DiscoursePluginRegistry.register_discourse_workflows_credential_type(
- DiscourseWorkflows::CredentialTypes::HeaderAuth,
+ "DiscourseWorkflows::CredentialTypes::HeaderAuth",
self,
)
diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/registry_reload_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/registry_reload_spec.rb
new file mode 100644
index 00000000000..b11bf0b408e
--- /dev/null
+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/registry_reload_spec.rb
@@ -0,0 +1,147 @@
+# frozen_string_literal: true
+
+RSpec.describe DiscourseWorkflows::Registry do
+ let(:loader) { Zeitwerk::Loader.new }
+ let(:directory) { Dir.mktmpdir("workflow-reload") }
+ let(:plugin) { Plugin::Instance.new }
+ let(:handled) { [] }
+
+ before do
+ @registered_nodes = DiscoursePluginRegistry._raw_discourse_workflows_nodes.dup
+ @credential_types = DiscoursePluginRegistry._raw_discourse_workflows_credential_types.dup
+ @node_classes = DiscourseWorkflows::NodeType.registered_nodes.dup
+ FileUtils.mkdir_p("#{directory}/workflow_reload_spec")
+ write_helper("orange")
+ File.write("#{directory}/workflow_reload_spec/node.rb", <<~RUBY)
+ module WorkflowReloadSpec
+ class Node < DiscourseWorkflows::NodeType
+ include Helper
+
+ description(
+ name: "trigger:reload_test",
+ version: "1.0",
+ defaults: { color: Helper::COLOR },
+ event: :workflow_reload_test,
+ properties: {
+ channel_id: {
+ type: :integer,
+ type_options: { load_options_method: "channels" },
+ },
+ },
+ )
+
+ def self.load_options_context(context)
+ Helper.options
+ end
+ end
+ end
+ RUBY
+ File.write("#{directory}/workflow_reload_spec/credential.rb", <<~RUBY)
+ module WorkflowReloadSpec
+ class Credential
+ def self.identifier
+ "reload_test"
+ end
+
+ def self.display_name
+ Helper::COLOR
+ end
+ end
+ end
+ RUBY
+ loader.push_dir(directory)
+ loader.enable_reloading
+ loader.setup
+ DiscourseWorkflows.node_registration_ready = true
+ plugin.register_discourse_workflows_node(WorkflowReloadSpec::Node)
+ DiscoursePluginRegistry.register_discourse_workflows_credential_type(
+ WorkflowReloadSpec::Credential.name,
+ plugin,
+ )
+ described_class.reset_indexes!
+ allow(DiscourseWorkflows::EventListener).to receive(:handle) do |klass, *args|
+ handled << [klass, args]
+ end
+ end
+
+ after do
+ DiscourseEvent.all_off(:workflow_reload_test)
+ DiscoursePluginRegistry._raw_discourse_workflows_nodes.replace(@registered_nodes)
+ DiscoursePluginRegistry._raw_discourse_workflows_credential_types.replace(@credential_types)
+ DiscourseWorkflows::NodeType.registered_nodes.replace(@node_classes)
+ described_class.reset_indexes!
+ loader.unload
+ loader.unregister
+ FileUtils.remove_entry(directory)
+ end
+
+ def write_helper(color)
+ File.write("#{directory}/workflow_reload_spec/helper.rb", <<~RUBY)
+ module WorkflowReloadSpec
+ module Helper
+ COLOR = #{color.inspect}
+
+ def self.options
+ [{ id: 1, name: COLOR }]
+ end
+ end
+ end
+ RUBY
+ end
+
+ it "lists current node metadata and dispatches once after repeated namespace reloads" do
+ previous = described_class.find_node_type("trigger:reload_test")
+ expect(previous.color).to eq("orange")
+
+ %w[blue green].each do |color|
+ write_helper(color)
+ loader.reload
+ current = described_class.find_node_type("trigger:reload_test")
+ plugin.register_discourse_workflows_node(current)
+ Plugin::Instance.new.register_discourse_workflows_node(current)
+
+ expect(current).not_to equal(previous)
+ expect(current).to equal(WorkflowReloadSpec::Node)
+ serialized =
+ DiscourseWorkflows::NodeTypeSerializer.new(
+ identifier: "trigger:reload_test",
+ available_versions: described_class.available_versions("trigger:reload_test"),
+ ).to_h
+ expect(serialized[:color]).to eq(color)
+ expect(serialized[:metadata]).to eq("channels" => [{ id: 1, name: color }])
+
+ DiscourseEvent.trigger(:workflow_reload_test, color)
+ expect(handled.last).to eq([current, [color]])
+ previous = current
+ end
+
+ expect(handled.size).to eq(2)
+ entries =
+ DiscoursePluginRegistry._raw_discourse_workflows_nodes.select do |entry|
+ entry[:value] == "WorkflowReloadSpec::Node"
+ end
+ expect(entries.map { |entry| entry[:plugin] }).to eq([plugin])
+
+ allow(plugin).to receive(:enabled?).and_return(false)
+ expect(described_class.find_node_type("trigger:reload_test")).to be_nil
+ expect(
+ described_class.find_node_type("trigger:reload_test", include_disabled_plugins: true),
+ ).to equal(WorkflowReloadSpec::Node)
+ DiscourseEvent.trigger(:workflow_reload_test, "disabled")
+ expect(handled.size).to eq(2)
+ end
+
+ it "resolves the current credential class after warming the identifier cache" do
+ previous = described_class.find_credential_type("reload_test")
+ expect(previous.display_name).to eq("orange")
+
+ write_helper("blue")
+ loader.reload
+
+ current = described_class.find_credential_type("reload_test")
+ expect(current).not_to equal(previous)
+ expect(current).to equal(WorkflowReloadSpec::Credential)
+ expect(current.display_name).to eq("blue")
+ expect(described_class.credential_types).to include(current)
+ end
+end
diff --git a/plugins/discourse-workflows/spec/support/workflow_node_registry_helpers.rb b/plugins/discourse-workflows/spec/support/workflow_node_registry_helpers.rb
index 271d956ed01..fba3aa89d2e 100644
--- a/plugins/discourse-workflows/spec/support/workflow_node_registry_helpers.rb
+++ b/plugins/discourse-workflows/spec/support/workflow_node_registry_helpers.rb
@@ -3,8 +3,9 @@
module DiscourseWorkflows
module WorkflowNodeRegistryHelpers
def unregister_workflow_nodes(*node_classes)
+ references = node_classes.compact.flat_map { |node_class| [node_class, node_class.name] }
DiscoursePluginRegistry._raw_discourse_workflows_nodes.reject! do |entry|
- node_classes.include?(entry[:value])
+ references.include?(entry[:value])
end
NodeType.registered_nodes.reject! { |node_class| node_classes.include?(node_class) }
Registry.reset_indexes!