A platform for community discussion. Free, open, simple.
UX: Explain rate-limited workflow executions
Régis Hanol14450dec375c
diff --git a/plugins/discourse-workflows/config/locales/server.en.yml b/plugins/discourse-workflows/config/locales/server.en.ymlindex 6bf9ee2412b..dbdc9fff8c6 100644--- a/plugins/discourse-workflows/config/locales/server.en.yml+++ b/plugins/discourse-workflows/config/locales/server.en.yml@@ -48,6 +48,13 @@ en:tag_too_long: "Tags can have at most %{max} characters."tag_contains_comma: "Tags cannot contain commas."approval_timed_out: "Approval timed out"+ rate_limited:+ global:+ one: "This workflow wasn't run because %{count} workflow execution already started in the last minute across all workflows, which is the most allowed. Runs will go through again once fewer workflow executions have started in the last minute."+ other: "This workflow wasn't run because %{count} workflow executions already started in the last minute across all workflows, which is the most allowed. Runs will go through again once fewer workflow executions have started in the last minute."+ per_workflow:+ one: "This workflow wasn't run because it already started %{count} execution in the last minute, which is the most allowed for a single workflow. Later runs will go through again once the last minute has fewer executions. If it's triggered this often on purpose, reduce how often its trigger fires, for example by filtering events earlier."+ other: "This workflow wasn't run because it already started %{count} executions in the last minute, which is the most allowed for a single workflow. Later runs will go through again once the last minute has fewer executions. If it's triggered this often on purpose, reduce how often its trigger fires, for example by filtering events earlier."step_execution:not_executable: "This node cannot be executed as a single step."waiting_not_supported: "Nodes that wait for a response cannot run during a step execution. Run the workflow or pin data past them first."diff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor.rbindex 672132a3b23..acd60c2d45e 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor.rb@@ -994,8 +994,8 @@ module DiscourseWorkflowsreturn falseend- unless rate_limiter.within_limits?- @store.create_rate_limited_execution+ if (rate_limit_message = rate_limiter.exceeded_limit_message)+ @store.create_rate_limited_execution(error: rate_limit_message)return falseenddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_rate_limiter.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_rate_limiter.rbindex 0c9eee2a21c..709c25b01e7 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_rate_limiter.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_rate_limiter.rb@@ -7,28 +7,35 @@ module DiscourseWorkflows@workflow = workflowend- def within_limits?- per_workflow_limiter.performed!(raise_error: false) &&- global_limiter.performed!(raise_error: false)+ # Records an execution attempt and returns a translated explanation when+ # it exceeds a limit, or nil when it is allowed.+ def exceeded_limit_message+ if !per_workflow_limiter.performed!(raise_error: false)+ I18n.t("discourse_workflows.errors.rate_limited.per_workflow", count: per_workflow_max)+ elsif !global_limiter.performed!(raise_error: false)+ I18n.t("discourse_workflows.errors.rate_limited.global", count: global_max)+ endendprivate+ def global_max+ SiteSetting.discourse_workflows_max_executions_per_minute+ end++ def per_workflow_max+ SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow+ end+def global_limiter- RateLimiter.new(- nil,- "discourse_workflows_executions",- SiteSetting.discourse_workflows_max_executions_per_minute,- 1.minute,- global: true,- )+ RateLimiter.new(nil, "discourse_workflows_executions", global_max, 1.minute, global: true)enddef per_workflow_limiterRateLimiter.new(nil,"discourse_workflows_workflow_#{@workflow.id}",- SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow,+ per_workflow_max,1.minute,)enddiff --git a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_store.rb b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_store.rbindex 00e713da917..bd64d2d6e98 100644--- a/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_store.rb+++ b/plugins/discourse-workflows/lib/discourse_workflows/executor/execution_store.rb@@ -106,12 +106,18 @@ module DiscourseWorkflowsexecutionend- def create_execution_with_status(status, trigger_data: self.trigger_data)- persist_execution!(status:, trigger_data:, finished_at: Time.current)+ def create_execution_with_status(status, trigger_data: self.trigger_data, error: nil)+ persist_execution!(status:, trigger_data:, finished_at: Time.current, error:)end- def create_rate_limited_execution- create_execution_with_status(:rate_limited, trigger_data: { "rate_limited" => true })+ def create_rate_limited_execution(error:)+ create_execution_with_status(+ :rate_limited,+ trigger_data: {+ "rate_limited" => true,+ },+ error:,+ )enddef pause_waiting_execution!(node:, waiting_until: nil, timeout_action: nil, steps: [])@@ -147,7 +153,7 @@ module DiscourseWorkflowsprivate- def persist_execution!(status:, trigger_data:, finished_at: nil)+ def persist_execution!(status:, trigger_data:, finished_at: nil, error: nil)@execution = @options.existing_execution || Execution.newcreated = @execution.new_record?@execution_context.execution = @execution if @options.existing_execution@@ -160,6 +166,7 @@ module DiscourseWorkflowsexecution_mode: @execution_mode,started_at: @execution.started_at || Time.current,finished_at:,+ **{ error: }.compact,}if @options.job_id.present?diff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/execution_rate_limiter_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/execution_rate_limiter_spec.rbindex 5f1e4f1a007..562a84a1965 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/execution_rate_limiter_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor/execution_rate_limiter_spec.rb@@ -14,28 +14,27 @@ RSpec.describe DiscourseWorkflows::Executor::ExecutionRateLimiter dolet(:limiter) { described_class.new(workflow) }- describe "#within_limits?" do- it "returns true when under limits" do- SiteSetting.discourse_workflows_max_executions_per_minute = 10- SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow = 5-- expect(limiter.within_limits?).to be(true)- end-- it "returns false when per-workflow limit is exceeded" do+ describe "#exceeded_limit_message" do+ it "explains when the per-workflow limit is exceeded" doSiteSetting.discourse_workflows_max_executions_per_minute = 100- SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow = 1+ SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow = 2++ 2.times { expect(limiter.exceeded_limit_message).to be_nil }- expect(limiter.within_limits?).to be(true)- expect(limiter.within_limits?).to be(false)+ expect(limiter.exceeded_limit_message).to eq(+ I18n.t("discourse_workflows.errors.rate_limited.per_workflow", count: 2),+ )end- it "returns false when global limit is exceeded" do- SiteSetting.discourse_workflows_max_executions_per_minute = 1+ it "explains when the global limit is exceeded" do+ SiteSetting.discourse_workflows_max_executions_per_minute = 2SiteSetting.discourse_workflows_max_executions_per_minute_per_workflow = 100- expect(limiter.within_limits?).to be(true)- expect(limiter.within_limits?).to be(false)+ 2.times { expect(limiter.exceeded_limit_message).to be_nil }++ expect(limiter.exceeded_limit_message).to eq(+ I18n.t("discourse_workflows.errors.rate_limited.global", count: 2),+ )endit "tracks limits independently per workflow" do@@ -44,9 +43,9 @@ RSpec.describe DiscourseWorkflows::Executor::ExecutionRateLimiter doother_limiter = described_class.new(other_workflow)- expect(limiter.within_limits?).to be(true)- expect(limiter.within_limits?).to be(false)- expect(other_limiter.within_limits?).to be(true)+ expect(limiter.exceeded_limit_message).to be_nil+ expect(limiter.exceeded_limit_message).to be_present+ expect(other_limiter.exceeded_limit_message).to be_nilendendenddiff --git a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_spec.rb b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_spec.rbindex 5d96691f30b..6c89ebe96c6 100644--- a/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_spec.rb+++ b/plugins/discourse-workflows/spec/lib/discourse_workflows/executor_spec.rb@@ -640,6 +640,9 @@ RSpec.describe DiscourseWorkflows::Executor dosecond_execution = described_class.new(workflow, "trigger-1", trigger_data).runexpect(second_execution.status).to eq("rate_limited")expect(second_execution.trigger_data).to eq("rate_limited" => true)+ expect(second_execution.error).to eq(+ I18n.t("discourse_workflows.errors.rate_limited.per_workflow", count: 1),+ )endit "updates an existing execution when limits are exceeded" do@@ -667,6 +670,7 @@ RSpec.describe DiscourseWorkflows::Executor doexisting_execution.reloadexpect(existing_execution.status).to eq("rate_limited")expect(existing_execution.trigger_data).to eq("rate_limited" => true)+ expect(existing_execution.error).to be_presentendenddiff --git a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/executions/detail-test.gjs b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/executions/detail-test.gjsindex 79274502043..206da3d05d4 100644--- a/plugins/discourse-workflows/test/javascripts/integration/components/workflows/executions/detail-test.gjs+++ b/plugins/discourse-workflows/test/javascripts/integration/components/workflows/executions/detail-test.gjs@@ -179,6 +179,25 @@ module();});+ test("explains why an execution was rate limited", async function (assert) {+ const error =+ "This workflow wasn't run because it already started 10 executions in the last minute.";+ this.execution = {+ ...executionWithOutput([]),+ status: "rate_limited",+ error,+ steps: [],+ };++ await render(+ <template><ExecutionDetail @execution={{this.execution}} /></template>+ );++ assert+ .dom(".workflows-execution-detail__error.alert-warning")+ .hasText(error, "the rate limit explanation is shown as a warning");+ });+test("renders execution errors that no step already shows", async function (assert) {this.execution = {...executionWithOutput([{ json: { value: 1 } }]),