From 1e57cff70a0eebabe143e13c7865045b164dccb4 Mon Sep 17 00:00:00 2001 From: Tanmay Deep Sharma Date: Wed, 15 Jul 2026 16:44:01 +0530 Subject: [PATCH] fix(automations): exclude paused accounts from the sweep so they can't starve others Now that a flag-off account's due rows stay pending (paused), order(:due_at).limit would keep re-selecting that backlog every sweep, starving enabled accounts with later due_at. Filter the sweep to accounts with delayed_automations enabled via a for_enabled_accounts scope, so paused rows sit out of the limit until re-enabled. --- app/jobs/automation_rules/trigger_pending_executions_job.rb | 2 +- app/models/automation_rule_pending_execution.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/jobs/automation_rules/trigger_pending_executions_job.rb b/app/jobs/automation_rules/trigger_pending_executions_job.rb index 90b16d9ab..1e6bf4956 100644 --- a/app/jobs/automation_rules/trigger_pending_executions_job.rb +++ b/app/jobs/automation_rules/trigger_pending_executions_job.rb @@ -9,7 +9,7 @@ class AutomationRules::TriggerPendingExecutionsJob < ApplicationJob started_at = Time.current purged = AutomationRulePendingExecution.purge_terminal! - rows = AutomationRulePendingExecution.sweepable.order(:due_at).limit(sweep_limit).to_a + rows = AutomationRulePendingExecution.sweepable.for_enabled_accounts.order(:due_at).limit(sweep_limit).to_a rows.each { |row| AutomationRules::ProcessPendingExecutionJob.perform_later(row) } log_summary(enqueued: rows.size, capped: rows.size >= sweep_limit, purged: purged, started_at: started_at) diff --git a/app/models/automation_rule_pending_execution.rb b/app/models/automation_rule_pending_execution.rb index 372cef7f6..7be0d8e17 100644 --- a/app/models/automation_rule_pending_execution.rb +++ b/app/models/automation_rule_pending_execution.rb @@ -45,6 +45,10 @@ class AutomationRulePendingExecution < ApplicationRecord # Non-terminal rows still bound to fire (a stale processing row is reclaimed by the sweep). scope :armed, -> { where(status: [statuses[:pending], statuses[:processing]]) } + # Excludes rows whose account paused delayed automations, so one disabled account's backlog + # can't fill the sweep limit and starve enabled accounts (paused rows resume on re-enable). + scope :for_enabled_accounts, -> { joins(:account).merge(Account.feature_delayed_automations) } + def self.schedule(rule:, conversation:, message: nil) key = arm_episode_key_for(conversation, message) create!(