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.
This commit is contained in:
Tanmay Deep Sharma
2026-07-15 16:44:01 +05:30
parent a921a38db9
commit 1e57cff70a
2 changed files with 5 additions and 1 deletions
@@ -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)
@@ -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!(