Skip to content

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

FIX: Prevent duplicate workflow jobs from repeating actions (#43724)

Régis Hanol9a8173708635
diff --git a/db/structure.sql b/db/structure.sql
index b53d2cdb89a..e72b41e22ef 100644
--- a/db/structure.sql
+++ b/db/structure.sql
@@ -5370,7 +5370,8 @@ CREATE TABLE public.discourse_workflows_executions (
started_at timestamp(6) without time zone,
finished_at timestamp(6) without time zone,
created_at timestamp(6) without time zone NOT NULL,
- updated_at timestamp(6) without time zone NOT NULL
+ updated_at timestamp(6) without time zone NOT NULL,
+ job_id character varying
);
@@ -19657,6 +19658,13 @@ CREATE UNIQUE INDEX idx_dwf_execution_data_on_execution_id ON public.discourse_w
CREATE UNIQUE INDEX idx_dwf_execution_stats_on_workflow_id_and_date ON public.discourse_workflows_execution_stats USING btree (workflow_id, date);
+--
+-- Name: idx_dwf_executions_on_job_id; Type: INDEX; Schema: public; Owner: -
+--
+
+CREATE UNIQUE INDEX idx_dwf_executions_on_job_id ON public.discourse_workflows_executions USING btree (job_id) WHERE (job_id IS NOT NULL);
+
+
--
-- Name: idx_dwf_executions_on_resume_token; Type: INDEX; Schema: public; Owner: -
--
@@ -25596,6 +25604,8 @@ INSERT INTO "schema_migrations" (version) VALUES
('20260918062145'),
('20260918061735'),
('20260917145657'),
+('20260916113851'),
+('20260916113806'),
('20260915204557'),
('20260915191328'),
('20260914213908'),
diff --git a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/execute_workflow.rb b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/execute_workflow.rb
index 9d1ff60ac5d..8d073346276 100644
--- a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/execute_workflow.rb
+++ b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/execute_workflow.rb
@@ -6,14 +6,17 @@ module Jobs
def execute(args)
::DiscourseWorkflows::Workflow::Execute.call(
params:
- args.slice(
- :workflow_id,
- :workflow_version_id,
- :trigger_node_id,
- :trigger_data,
- :execution_mode,
- :user_id,
- ).compact,
+ args
+ .slice(
+ :workflow_id,
+ :workflow_version_id,
+ :trigger_node_id,
+ :trigger_data,
+ :execution_mode,
+ :user_id,
+ )
+ .merge(job_id: jid)
+ .compact,
)
end
end
diff --git a/plugins/discourse-workflows/app/models/discourse_workflows/execution.rb b/plugins/discourse-workflows/app/models/discourse_workflows/execution.rb
index 881adb06e6d..a49fe221b75 100644
--- a/plugins/discourse-workflows/app/models/discourse_workflows/execution.rb
+++ b/plugins/discourse-workflows/app/models/discourse_workflows/execution.rb
@@ -297,6 +297,7 @@ end
# waiting_until :datetime
# created_at :datetime not null
# updated_at :datetime not null
+# job_id :string
# trigger_node_id :string(100)
# waiting_node_id :string(100)
# workflow_id :bigint not null
@@ -304,6 +305,7 @@ end
#
# Indexes
#
+# idx_dwf_executions_on_job_id (job_id) UNIQUE WHERE (job_id IS NOT NULL)
# idx_dwf_executions_on_resume_token (resume_token) WHERE (resume_token IS NOT NULL)
# idx_dwf_executions_on_retention (created_at) WHERE (status = ANY (ARRAY[2, 3, 5, 6]))
# idx_dwf_executions_on_status_waiting_until (status,waiting_until)
diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/workflow/execute.rb b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/execute.rb
index f27207ded08..b8792b0d988 100644
--- a/plugins/discourse-workflows/app/services/discourse_workflows/workflow/execute.rb
+++ b/plugins/discourse-workflows/app/services/discourse_workflows/workflow/execute.rb
@@ -11,6 +11,7 @@ module DiscourseWorkflows
attribute :trigger_data, default: -> { {} }
attribute :execution_mode, :string, default: "normal"
attribute :user_id, :integer
+ attribute :job_id, :string
validates :trigger_node_id, presence: true
end
@@ -63,6 +64,7 @@ module DiscourseWorkflows
user: user,
execution_mode: params.execution_mode.to_sym,
workflow_version: workflow_version,
+ job_id: params.job_id,
)
executor =
DiscourseWorkflows::Executor.new(workflow, trigger_node["id"], params.trigger_data, options)
diff --git a/plugins/discourse-workflows/db/migrate/20260916113806_add_job_id_to_workflow_executions.rb b/plugins/discourse-workflows/db/migrate/20260916113806_add_job_id_to_workflow_executions.rb
new file mode 100644
index 00000000000..4580374136d
--- /dev/null
+++ b/plugins/discourse-workflows/db/migrate/20260916113806_add_job_id_to_workflow_executions.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+class AddJobIdToWorkflowExecutions < ActiveRecord::Migration[8.0]
+ def change
+ add_column :discourse_workflows_executions, :job_id, :string
+ end
+end
diff --git a/plugins/discourse-workflows/db/migrate/20260916113851_add_unique_index_to_workflow_execution_job_id.rb b/plugins/discourse-workflows/db/migrate/20260916113851_add_unique_index_to_workflow_execution_job_id.rb
new file mode 100644
index 00000000000..f94ef519d06
--- /dev/null
+++ b/plugins/discourse-workflows/db/migrate/20260916113851_add_unique_index_to_workflow_execution_job_id.rb
@@ -0,0 +1,24 @@
+# frozen_string_literal: true
+
+class AddUniqueIndexToWorkflowExecutionJobId < ActiveRecord::Migration[8.0]
+ INDEX_NAME = "idx_dwf_executions_on_job_id"
+
+ disable_ddl_transaction!
+
+ def up
+ remove_index :discourse_workflows_executions,
+ name: INDEX_NAME,
+ algorithm: :concurrently,
+ if_exists: true
+ add_index :discourse_workflows_executions,
+ :job_id,
+ unique: true,
+ where: "job_id IS NOT NULL",
+ name: INDEX_NAME,
+ algorithm: :concurrently
+ end
+
+ def down
+ remove_index :discourse_workflows_executions, name: INDEX_NAME, algorithm: :concurrently
+ end
+end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
index 6192ed331e2..3873b1f9e43 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
@@ -120,11 +120,7 @@ module DiscourseWorkflows
private_class_method :build_resume_snapshot!
def run
- unless @workflow.published? || @options.draft_execution || @options.workflow_version ||
- @options.workflow_snapshot
- return @store.create_execution_with_status(:skipped)
- end
- return @store.create_rate_limited_execution unless rate_limiter.within_limits?
+ return execution if @store.find_job_execution
execute_flow(:start_execution!) do
next prepare_step_flow! if step_mode?
@@ -251,13 +247,15 @@ module DiscourseWorkflows
end
def execute_flow(setup_method, *setup_args, &block)
- send(setup_method, *setup_args)
+ return execution if send(setup_method, *setup_args) == false
yield
process_queue
@store.finish!(steps: @steps)
rescue ExecutionPaused => e
begin_wait!(e.wait_request)
rescue => e
+ raise unless @store.owns_execution?
+
@store.fail!(error: e, steps: @steps)
ensure
commit_static_data!
@@ -986,7 +984,16 @@ module DiscourseWorkflows
end
def start_execution!
- @store.start!
+ started =
+ if @options.job_id.present?
+ DistributedMutex.synchronize("discourse_workflows_job_#{@options.job_id}") do
+ create_execution!
+ end
+ else
+ create_execution!
+ end
+ return false unless started
+
@snapshot = @store.workflow_snapshot
@steps = []
@queue = []
@@ -997,6 +1004,23 @@ module DiscourseWorkflows
@input_wait_requirements = {}
end
+ def create_execution!
+ return false if @store.find_job_execution
+
+ unless @workflow.published? || @options.draft_execution || @options.workflow_version ||
+ @options.workflow_snapshot
+ @store.create_execution_with_status(:skipped)
+ return false
+ end
+
+ unless rate_limiter.within_limits?
+ @store.create_rate_limited_execution
+ return false
+ end
+
+ @store.start!
+ end
+
def resume_execution!(execution)
@store.resume!(execution)
@snapshot = @store.workflow_snapshot
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_options.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_options.rb
index aeeb2791132..34aa2e5e1e2 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_options.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_options.rb
@@ -16,6 +16,7 @@ module DiscourseWorkflows
:workflow_call_run_id,
:workflow_call_child,
:step_node_id,
+ :job_id,
) do
def initialize(
user: nil,
@@ -29,7 +30,8 @@ module DiscourseWorkflows
workflow_call_caller: nil,
workflow_call_run_id: nil,
workflow_call_child: false,
- step_node_id: nil
+ step_node_id: nil,
+ job_id: nil
)
super
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_store.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_store.rb
index 22c95d26a58..afc6f9803d6 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_store.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_store.rb
@@ -19,6 +19,7 @@ module DiscourseWorkflows
@execution_mode = execution_mode
@options = options
@workflow_snapshot = nil
+ @duplicate_job = false
@existing_run_data = {}
@last_execution_run_data = {}
end
@@ -37,11 +38,25 @@ module DiscourseWorkflows
workflow_name: @workflow_snapshot.workflow_name,
)
@execution = create_execution!
+ return false if @duplicate_job
+
reset_collaborators!
restore_seeded_run_data!
@execution
end
+ def find_job_execution
+ return if @options.job_id.blank?
+
+ @execution = Execution.find_by(job_id: @options.job_id)
+ @duplicate_job = @execution.present?
+ @execution
+ end
+
+ def owns_execution?
+ execution&.persisted? && !@duplicate_job
+ end
+
def resume!(execution)
@execution = execution
@execution_context.execution = execution
@@ -140,7 +155,7 @@ module DiscourseWorkflows
@execution = @options.existing_execution || DiscourseWorkflows::Execution.new
created = @execution.new_record?
@execution_context.execution = @execution if @options.existing_execution
- @execution.update!(
+ attributes = {
workflow_id: workflow.id,
workflow_version_id: execution_workflow_version_id,
trigger_node_id: @trigger_node_id,
@@ -149,7 +164,19 @@ module DiscourseWorkflows
execution_mode: @execution_mode,
started_at: @execution.started_at || Time.current,
finished_at: finished_at,
- )
+ }
+
+ if @options.job_id.present?
+ @execution =
+ Execution.create_or_find_by!(job_id: @options.job_id) do |execution|
+ execution.assign_attributes(attributes)
+ end
+ @duplicate_job = [email protected]_new_record?
+ return @execution if @duplicate_job
+ else
+ @execution.update!(attributes)
+ end
+
attach_workflow_call_run!
if created
ExecutionProgressPublisher.publish_created(@execution, workflow_name: workflow.name)
diff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/execute_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/execute_spec.rb
index a115b0d5b95..4bc4701024c 100644
--- a/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/execute_spec.rb
+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/workflow/execute_spec.rb
@@ -89,6 +89,249 @@ RSpec.describe DiscourseWorkflows::Workflow::Execute do
end
end
+ context "when no job_id is provided" do
+ it "creates separate executions for repeated calls without a job ID" do
+ first_execution = result[:execution]
+ repeated_result = described_class.call(params:)
+
+ expect([result, repeated_result]).to all(run_successfully)
+ expect(workflow.executions).to contain_exactly(first_execution, repeated_result[:execution])
+ expect(workflow.executions).to all(be_success)
+ end
+ end
+
+ context "when job_id is provided" do
+ let(:params) { super().merge(job_id: "workflow-service-job") }
+ let(:effect_url) { "https://example.com/workflow-service-effect" }
+ let(:http_response) do
+ {
+ status: Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(:ok),
+ body: "{}",
+ headers: {
+ "content-type" => "application/json",
+ },
+ }
+ end
+
+ before do
+ graph =
+ build_workflow_graph do |workflow_graph|
+ workflow_graph.node "trigger-1", "trigger:manual"
+ workflow_graph.node "effect-1",
+ "action:http_request",
+ configuration: {
+ "method" => "POST",
+ "url" => effect_url,
+ }
+ workflow_graph.chain "trigger-1", "effect-1"
+ end
+ workflow.update!(**graph)
+ publish_workflow!(workflow)
+ stub_request(:post, effect_url).to_return(http_response)
+ end
+
+ it "returns a running execution when the same job is delivered during its HTTP request" do
+ repeated_result = nil
+ repeated_status = nil
+ repeated_delivery_started = false
+ stub_request(:post, effect_url).to_return do
+ unless repeated_delivery_started
+ repeated_delivery_started = true
+ repeated_result = described_class.call(params:)
+ repeated_status = repeated_result[:execution].status
+ end
+
+ http_response
+ end
+
+ expect(result).to run_successfully
+
+ expect(repeated_result).to run_successfully
+ expect(repeated_result[:execution].id).to eq(result[:execution].id)
+ expect(repeated_status).to eq("running")
+ expect(result[:execution]).to be_success
+ expect(a_request(:post, effect_url)).to have_been_made.once
+ end
+
+ it "preserves the original Redis failure when unlocking a duplicate delivery" do
+ first_execution = result[:execution]
+ checkpoint = first_execution.execution_data.attributes.deep_dup
+ unlock_error = Redis::CannotConnectError.new
+ DiscourseWorkflows::Execution
+ .stubs(:find_by)
+ .with(job_id: params[:job_id])
+ .returns(nil, first_execution)
+ allow(DistributedMutex::UNLOCK_SCRIPT).to receive(
+ :eval,
+ ).and_wrap_original do |original, *arguments|
+ original.call(*arguments)
+ raise unlock_error
+ end
+
+ repeated_result = described_class.call(params:)
+
+ expect(repeated_result).to fail_to_find_a_model(:execution)
+ expect(repeated_result["result.model.execution"].exception).to equal(unlock_error)
+ expect(first_execution.reload).to be_success
+ expect(first_execution.execution_data.reload.attributes).to eq(checkpoint)
+ end
+
+ it "propagates shutdown when unlocking a duplicate delivery" do
+ first_execution = result[:execution]
+ checkpoint = first_execution.execution_data.attributes.deep_dup
+ shutdown = Sidekiq::Shutdown.new
+ DiscourseWorkflows::Execution
+ .stubs(:find_by)
+ .with(job_id: params[:job_id])
+ .returns(nil, first_execution)
+ allow(DistributedMutex::UNLOCK_SCRIPT).to receive(
+ :eval,
+ ).and_wrap_original do |original, *arguments|
+ original.call(*arguments)
+ raise shutdown
+ end
+
+ expect { described_class.call(params:) }.to raise_error(Sidekiq::Shutdown) do |error|
+ expect(error).to equal(shutdown)
+ end
+
+ expect(first_execution.reload).to be_success
+ expect(first_execution.execution_data.reload.attributes).to eq(checkpoint)
+ end
+
+ it "deduplicates completed jobs while preserving creation events and rate-limit capacity" do
+ RateLimiter.enable
+ SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow = 2
+ repeated_result = nil
+ messages =
+ MessageBus.track_publish(
+ DiscourseWorkflows::ExecutionProgressPublisher::EXECUTIONS_CHANNEL,
+ ) do
+ result
+ repeated_result = described_class.call(params:)
+ end
+ distinct_result =
+ described_class.call(params: params.merge(job_id: "distinct-workflow-job"))
+
+ expect([result, repeated_result, distinct_result]).to all(run_successfully)
+ expect(repeated_result[:execution]).to eq(result[:execution])
+ expect(workflow.executions).to contain_exactly(
+ result[:execution],
+ distinct_result[:execution],
+ )
+ expect(workflow.executions).to all(be_success)
+ expect(
+ messages.map(&:data).select { |message| message[:type] == "execution_created" },
+ ).to contain_exactly(include(execution: include(id: result[:execution].id)))
+ expect(a_request(:post, effect_url)).to have_been_made.twice
+ ensure
+ RateLimiter.disable
+ end
+
+ it "excludes rate-limited jobs from execution statistics", :aggregate_failures do
+ RateLimiter.enable
+ SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow = 1
+ expect(result[:execution]).to be_success
+ limited_result = nil
+
+ expect {
+ limited_result =
+ described_class.call(params: params.merge(job_id: "limited-workflow-job"))
+ }.not_to change {
+ DiscourseWorkflows::ExecutionStat.where(workflow_id: workflow.id).sum(:total_runs)
+ }
+
+ expect(limited_result).to run_successfully
+ expect(limited_result[:execution]).to be_rate_limited
+ expect(a_request(:post, effect_url)).to have_been_made.once
+ ensure
+ RateLimiter.disable
+ end
+
+ it "executes on redelivery after Redis fails before node execution" do
+ RateLimiter.enable
+ redis = Discourse.redis.without_namespace
+ redis.stubs(:evalsha).raises(Redis::CannotConnectError)
+
+ expect(result).to fail_to_find_a_model(:execution)
+ redis.unstub(:evalsha)
+ retried_result = described_class.call(params:)
+
+ expect(retried_result).to run_successfully
+ expect(retried_result[:execution]).to be_success
+ expect(a_request(:post, effect_url)).to have_been_made.once
+ ensure
+ RateLimiter.disable
+ end
+
+ it "executes once when duplicate deliveries compete for the final rate-limit slot" do
+ RateLimiter.enable
+ SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow = 1
+ delivery_params = params.dup
+ workflow_limit_key = "discourse_workflows_workflow_#{workflow.id}"
+ first_checked_quota = Queue.new
+ continue_first = Queue.new
+ duplicate_progress = Queue.new
+ workers = []
+ first_paused = false
+ thread_timeout = 5.seconds
+
+ %i[evalsha eval].each do |command|
+ allow(Discourse.redis.without_namespace).to receive(
+ command,
+ ).and_wrap_original do |original, *arguments|
+ response = original.call(*arguments)
+ delivery = Thread.current[:workflow_delivery]
+
+ if delivery == :first && !first_paused &&
+ Array(arguments[1]).any? { |key| key.end_with?(workflow_limit_key) }
+ first_paused = true
+ first_checked_quota << :paused
+ continue_first.pop
+ elsif delivery == :duplicate && response.nil?
+ duplicate_progress << :blocked
+ end
+
+ response
+ end
+ end
+
+ workers << Thread.new do
+ Thread.current[:workflow_delivery] = :first
+ described_class.call(params: delivery_params)
+ end
+ expect(first_checked_quota.pop(timeout: thread_timeout)).to eq(:paused)
+
+ workers << Thread.new do
+ Thread.current[:workflow_delivery] = :duplicate
+ described_class.call(params: delivery_params)
+ ensure
+ duplicate_progress << :finished
+ end
+ expect(duplicate_progress.pop(timeout: thread_timeout)).to be_in(%i[blocked finished])
+ continue_first << :continue
+ results =
+ workers.map do |worker|
+ expect(worker.join(thread_timeout)).to eq(worker)
+ worker.value
+ end
+
+ expect(results).to all(run_successfully)
+ expect(results.map { |delivery_result| delivery_result[:execution].id }.uniq).to eq(
+ [workflow.executions.sole.id],
+ )
+ expect(workflow.executions.sole).to be_success
+ expect(a_request(:post, effect_url)).to have_been_made.once
+ ensure
+ continue_first << :continue if continue_first
+ workers&.each do |worker|
+ worker.kill if worker.alive?
+ worker.join
+ end
+ RateLimiter.disable
+ end
+ end
+
context "when user_id is provided" do
fab!(:execution_user, :user)
let(:params) do