Skip to content

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

FIX: Preserve newer workflow waits when old jobs run (#43721)

Régis Hanolbacd714d9209
diff --git a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/resume_waiting_execution.rb b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/resume_waiting_execution.rb
index a8d4de109e1..4d4904365e3 100644
--- a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/resume_waiting_execution.rb
+++ b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/resume_waiting_execution.rb
@@ -5,10 +5,15 @@ module Jobs
class ResumeWaitingExecution < ::Jobs::Base
def execute(args)
return unless SiteSetting.enable_discourse_workflows
+ return if args[:resume_token].blank?
- execution = ::DiscourseWorkflows::Execution.find_by(id: args[:execution_id])
+ execution =
+ ::DiscourseWorkflows::Execution.find_by(
+ id: args[:execution_id],
+ resume_token: args[:resume_token],
+ )
return if execution.nil? || !execution.waiting?
- return if execution.waiting_until.present? && execution.waiting_until > Time.current
+ return if execution.waiting_until.nil? || execution.waiting_until > Time.current
if execution.timeout_action == "fail"
execution.fail_with_timeout!
diff --git a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/resume_webhook_waiting.rb b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/resume_webhook_waiting.rb
index dc625b5e0a4..2c3eb42e495 100644
--- a/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/resume_webhook_waiting.rb
+++ b/plugins/discourse-workflows/app/jobs/regular/discourse_workflows/resume_webhook_waiting.rb
@@ -5,9 +5,14 @@ module Jobs
class ResumeWebhookWaiting < ::Jobs::Base
def execute(args)
return unless SiteSetting.enable_discourse_workflows
+ return if args[:resume_token].blank?
execution =
- ::DiscourseWorkflows::Execution.find_by(id: args[:execution_id], status: :waiting)
+ ::DiscourseWorkflows::Execution.find_by(
+ id: args[:execution_id],
+ status: :waiting,
+ resume_token: args[:resume_token],
+ )
return if execution.nil?
claimed = ::DiscourseWorkflows::Execution.claim_for_resume(execution)
diff --git a/plugins/discourse-workflows/app/models/discourse_workflows/execution.rb b/plugins/discourse-workflows/app/models/discourse_workflows/execution.rb
index bb6f4c8f4e6..0db30c4dc93 100644
--- a/plugins/discourse-workflows/app/models/discourse_workflows/execution.rb
+++ b/plugins/discourse-workflows/app/models/discourse_workflows/execution.rb
@@ -108,9 +108,8 @@ module DiscourseWorkflows
end
end
- def self.claim_for_resume(execution, resume_token: nil)
- scope = where(id: execution.id, status: :waiting)
- scope = scope.where(resume_token: resume_token) if resume_token
+ def self.claim_for_resume(execution, resume_token: execution.resume_token)
+ scope = where(id: execution.id, status: :waiting, resume_token: resume_token)
now = Time.current
affected = scope.update_all(status: statuses[:running], updated_at: now)
@@ -170,7 +169,13 @@ module DiscourseWorkflows
affected =
self
.class
- .where(id: id, status: :waiting)
+ .where(
+ id: id,
+ status: :waiting,
+ resume_token: resume_token,
+ waiting_until: waiting_until,
+ timeout_action: timeout_action,
+ )
.update_all(
status: self.class.statuses[:error],
error: message,
diff --git a/plugins/discourse-workflows/app/services/discourse_workflows/webhook/receive.rb b/plugins/discourse-workflows/app/services/discourse_workflows/webhook/receive.rb
index 9d1d8e65f6a..263885e4c51 100644
--- a/plugins/discourse-workflows/app/services/discourse_workflows/webhook/receive.rb
+++ b/plugins/discourse-workflows/app/services/discourse_workflows/webhook/receive.rb
@@ -185,6 +185,7 @@ module DiscourseWorkflows
Jobs.enqueue(
Jobs::DiscourseWorkflows::ResumeWebhookWaiting,
execution_id: waiting_execution.id,
+ resume_token: waiting_execution.resume_token,
response_items: webhook_context.resume_items,
)
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
index fd9952db531..6192ed331e2 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb
@@ -969,6 +969,7 @@ module DiscourseWorkflows
[resolved - now, 0].max,
Jobs::DiscourseWorkflows::ResumeWaitingExecution,
execution_id: @store.execution.id,
+ resume_token: execution.resume_token,
)
execution
end
diff --git a/plugins/discourse-workflows/lib/discourse_workflows/plugin_enable_handler.rb b/plugins/discourse-workflows/lib/discourse_workflows/plugin_enable_handler.rb
index 63e48c154f3..2aac03367e0 100644
--- a/plugins/discourse-workflows/lib/discourse_workflows/plugin_enable_handler.rb
+++ b/plugins/discourse-workflows/lib/discourse_workflows/plugin_enable_handler.rb
@@ -19,6 +19,7 @@ module DiscourseWorkflows
duration,
Jobs::DiscourseWorkflows::ResumeWaitingExecution,
execution_id: execution.id,
+ resume_token: execution.resume_token,
)
end
end
diff --git a/plugins/discourse-workflows/spec/integration/ai_agent_review_attribution_spec.rb b/plugins/discourse-workflows/spec/integration/ai_agent_review_attribution_spec.rb
index 8b1e8575cca..1ceedfec9bd 100644
--- a/plugins/discourse-workflows/spec/integration/ai_agent_review_attribution_spec.rb
+++ b/plugins/discourse-workflows/spec/integration/ai_agent_review_attribution_spec.rb
@@ -64,7 +64,10 @@ RSpec.describe "Agent attribution on workflow flags" do
expect(execution.status).to eq("waiting")
freeze_time(execution.waiting_until + 1.second) do
- Jobs::DiscourseWorkflows::ResumeWaitingExecution.new.execute(execution_id: execution.id)
+ Jobs::DiscourseWorkflows::ResumeWaitingExecution.new.execute(
+ execution_id: execution.id,
+ resume_token: execution.resume_token,
+ )
end
expect(execution.reload.status).to eq("success")
diff --git a/plugins/discourse-workflows/spec/integration/execution_lifecycle_spec.rb b/plugins/discourse-workflows/spec/integration/execution_lifecycle_spec.rb
new file mode 100644
index 00000000000..7876b32cbcb
--- /dev/null
+++ b/plugins/discourse-workflows/spec/integration/execution_lifecycle_spec.rb
@@ -0,0 +1,98 @@
+# frozen_string_literal: true
+
+RSpec.describe "Workflow execution lifecycle" do
+ let(:effect_url) { "https://workflow.example.test/effect" }
+ let(:checkpoint_url) { "https://workflow.example.test/checkpoint" }
+ let(:http_success) do
+ {
+ status: Rack::Utils::SYMBOL_TO_STATUS_CODE.fetch(:ok),
+ body: "{}",
+ headers: {
+ "content-type" => "application/json",
+ },
+ }
+ end
+ let(:workflow) { build_workflow }
+
+ def build_workflow
+ graph =
+ build_workflow_graph do |builder|
+ builder.node "trigger-1", "trigger:manual"
+ yield builder if block_given?
+ builder.node "effect-1",
+ "action:http_request",
+ parameters: {
+ method: "POST",
+ url: effect_url,
+ }
+ builder.node "checkpoint-1",
+ "action:http_request",
+ parameters: {
+ method: "GET",
+ url: checkpoint_url,
+ }
+ builder.chain(*builder.to_h[:nodes].map { |node| node["id"] })
+ end
+ Fabricate(:discourse_workflows_workflow, published: true, **graph)
+ end
+
+ before do
+ stub_request(:post, effect_url).to_return(http_success)
+ stub_request(:get, checkpoint_url).to_return(http_success)
+ end
+
+ describe "wait job ownership" do
+ it "keeps a second webhook wait pending when the first callback is redelivered",
+ :aggregate_failures do
+ waiting_workflow =
+ build_workflow do |builder|
+ builder.node "wait-1", "flow:wait", parameters: { resume: "webhook" }
+ builder.node "wait-2", "flow:wait", parameters: { resume: "webhook" }
+ end
+ execution = DiscourseWorkflows::Executor.new(waiting_workflow, "trigger-1", {}).run
+ job = Jobs::DiscourseWorkflows::ResumeWebhookWaiting.new
+ args = {
+ execution_id: execution.id,
+ resume_token: execution.resume_token,
+ response_items: [{ "json" => { "approved" => true } }],
+ }
+
+ job.execute(args)
+ expect(execution.reload.waiting_node_id).to eq("wait-2")
+ job.execute(args)
+
+ expect(execution.reload).to have_attributes(status: "waiting", waiting_node_id: "wait-2")
+ expect(a_request(:post, effect_url)).not_to have_been_made
+ end
+
+ it "keeps a child call pending when an earlier timer is redelivered", :aggregate_failures do
+ freeze_time
+
+ target_graph =
+ build_workflow_graph { |builder| builder.node "call-trigger", "trigger:workflow_call" }
+ target = Fabricate(:discourse_workflows_workflow, published: true, **target_graph)
+ parent =
+ build_workflow do |builder|
+ builder.node "wait-1",
+ "flow:wait",
+ parameters: {
+ resume: "time_interval",
+ wait_amount: 1,
+ wait_unit: "minutes",
+ }
+ builder.node "call-1", "action:workflow_call", parameters: { workflow_id: target.id }
+ end
+ execution = DiscourseWorkflows::Executor.new(parent, "trigger-1", {}).run
+ freeze_time(2.minutes.from_now)
+ job = Jobs::DiscourseWorkflows::ResumeWaitingExecution.new
+ args = { execution_id: execution.id, resume_token: execution.resume_token }
+
+ job.execute(args)
+ expect(execution.reload).to have_attributes(waiting_node_id: "call-1", waiting_until: nil)
+ job.execute(args)
+
+ expect(execution.reload).to have_attributes(status: "waiting", waiting_node_id: "call-1")
+ expect(a_request(:post, effect_url)).not_to have_been_made
+ end
+ end
+end
diff --git a/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/resume_waiting_execution_spec.rb b/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/resume_waiting_execution_spec.rb
index 636823e1efe..def77757edc 100644
--- a/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/resume_waiting_execution_spec.rb
+++ b/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/resume_waiting_execution_spec.rb
@@ -8,6 +8,7 @@ RSpec.describe Jobs::DiscourseWorkflows::ResumeWaitingExecution do
workflow: workflow,
status: :waiting,
waiting_until: 1.minute.ago,
+ resume_token: "wait-token",
)
end
@@ -15,7 +16,7 @@ RSpec.describe Jobs::DiscourseWorkflows::ResumeWaitingExecution do
SiteSetting.enable_discourse_workflows = false
before_updated_at = execution.updated_at
- described_class.new.execute(execution_id: execution.id)
+ described_class.new.execute(execution_id: execution.id, resume_token: execution.resume_token)
execution.reload
expect(execution.status).to eq("waiting")
@@ -26,7 +27,7 @@ RSpec.describe Jobs::DiscourseWorkflows::ResumeWaitingExecution do
allow(::DiscourseWorkflows::Execution).to receive(:claim_for_resume).and_return(nil)
before_updated_at = execution.updated_at
- described_class.new.execute(execution_id: execution.id)
+ described_class.new.execute(execution_id: execution.id, resume_token: execution.resume_token)
execution.reload
expect(execution.status).to eq("waiting")
@@ -37,7 +38,7 @@ RSpec.describe Jobs::DiscourseWorkflows::ResumeWaitingExecution do
execution.update!(status: :running)
before_updated_at = execution.updated_at
- described_class.new.execute(execution_id: execution.id)
+ described_class.new.execute(execution_id: execution.id, resume_token: execution.resume_token)
execution.reload
expect(execution.status).to eq("running")
@@ -48,7 +49,7 @@ RSpec.describe Jobs::DiscourseWorkflows::ResumeWaitingExecution do
before { execution.update!(timeout_action: "fail") }
it "fails the execution with the existing timeout error and clears its wait state" do
- described_class.new.execute(execution_id: execution.id)
+ described_class.new.execute(execution_id: execution.id, resume_token: execution.resume_token)
expect(execution.reload).to have_attributes(
status: "error",
diff --git a/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/resume_webhook_waiting_spec.rb b/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/resume_webhook_waiting_spec.rb
index c334384c778..45f82613a78 100644
--- a/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/resume_webhook_waiting_spec.rb
+++ b/plugins/discourse-workflows/spec/jobs/regular/discourse_workflows/resume_webhook_waiting_spec.rb
@@ -3,14 +3,19 @@
RSpec.describe Jobs::DiscourseWorkflows::ResumeWebhookWaiting do
fab!(:workflow) { Fabricate(:discourse_workflows_workflow, published: true) }
fab!(:execution) do
- Fabricate(:discourse_workflows_execution, workflow: workflow, status: :waiting)
+ Fabricate(
+ :discourse_workflows_execution,
+ workflow: workflow,
+ status: :waiting,
+ resume_token: "wait-token",
+ )
end
it "leaves the execution waiting when the plugin is disabled" do
SiteSetting.enable_discourse_workflows = false
before_updated_at = execution.updated_at
- described_class.new.execute(execution_id: execution.id)
+ described_class.new.execute(execution_id: execution.id, resume_token: execution.resume_token)
execution.reload
expect(execution.status).to eq("waiting")
@@ -21,7 +26,7 @@ RSpec.describe Jobs::DiscourseWorkflows::ResumeWebhookWaiting do
allow(::DiscourseWorkflows::Execution).to receive(:claim_for_resume).and_return(nil)
before_updated_at = execution.updated_at
- described_class.new.execute(execution_id: execution.id)
+ described_class.new.execute(execution_id: execution.id, resume_token: execution.resume_token)
execution.reload
expect(execution.status).to eq("waiting")
diff --git a/plugins/discourse-workflows/spec/models/discourse_workflows/execution_spec.rb b/plugins/discourse-workflows/spec/models/discourse_workflows/execution_spec.rb
index 276f5a8dcfc..35678f439fb 100644
--- a/plugins/discourse-workflows/spec/models/discourse_workflows/execution_spec.rb
+++ b/plugins/discourse-workflows/spec/models/discourse_workflows/execution_spec.rb
@@ -51,6 +51,14 @@ RSpec.describe DiscourseWorkflows::Execution do
expect(first).to be_present
expect(second).to be_nil
end
+
+ it "preserves a newer wait when the caller holds a stale execution" do
+ stale_execution = described_class.find(execution.id)
+ execution.update!(resume_token: "new-wait-token")
+
+ expect(described_class.claim_for_resume(stale_execution)).to be_nil
+ expect(execution.reload).to have_attributes(status: "waiting", resume_token: "new-wait-token")
+ end
end
describe ".claim_pending" do
@@ -115,6 +123,23 @@ RSpec.describe DiscourseWorkflows::Execution do
expect(execution.fail_with_timeout!).to eq(false)
expect(execution.reload.status).to eq("running")
end
+
+ it "preserves a newer wait when an expired wait is processed late" do
+ stale_execution = described_class.find(execution.id)
+ next_wait = {
+ waiting_node_id: "node-2",
+ waiting_until: 1.minute.from_now,
+ resume_token: "next-wait-token",
+ }
+ execution.update!(next_wait)
+
+ expect(stale_execution.fail_with_timeout!).to eq(false)
+ expect(execution.reload).to have_attributes(
+ status: "waiting",
+ **next_wait,
+ waiting_until: eq_time(next_wait[:waiting_until]),
+ )
+ end
end
describe ".compute_run_time_ms" do
diff --git a/plugins/discourse-workflows/spec/services/discourse_workflows/webhook/receive_spec.rb b/plugins/discourse-workflows/spec/services/discourse_workflows/webhook/receive_spec.rb
index e26190cba2f..0341ae91826 100644
--- a/plugins/discourse-workflows/spec/services/discourse_workflows/webhook/receive_spec.rb
+++ b/plugins/discourse-workflows/spec/services/discourse_workflows/webhook/receive_spec.rb
@@ -112,7 +112,10 @@ RSpec.describe DiscourseWorkflows::Webhook::Receive do
it "enqueues a ResumeWebhookWaiting job" do
result
job = Jobs::DiscourseWorkflows::ResumeWebhookWaiting.jobs.last
- expect(job["args"].first).to include("execution_id" => waiting_execution.id)
+ expect(job["args"].first).to include(
+ "execution_id" => waiting_execution.id,
+ "resume_token" => waiting_execution.resume_token,
+ )
expect(job["args"].first["response_items"].first["json"]).to include(
"body" => {
"foo" => "bar",