Class: Activity::ChainRunnerHandler

Inherits:
ApplicationJob show all
Includes:
RailsEventStore::AsyncHandler
Defined in:
app/subscribers/activity/chain_runner_handler.rb

Overview

Runs Activity::ChainRunner asynchronously when an activity's result type changes
but the synchronous chain was intentionally deferred by the caller.

WHY: In batch contexts (e.g. SearchResourceUpdateWorker mass-updating 500+
activities) running ChainRunner synchronously inside each activity.save blocks
the worker for minutes and risks being killed by WorkerKiller. Instead, the
batch worker sets activity.halt_chain = true before saving. SynchronousEffectsHandler
sees halt_chain and skips the chain. The after-commit Events::ActivityUpdated
payload carries chain_deferred: true so this handler picks it up and runs the
chain in a separate Sidekiq job per activity.

For normal single-activity saves (controller path), halt_chain is not set,
chain_deferred is false, and this handler is a no-op — the chain already ran
synchronously inside SynchronousEffectsHandler.

Subscribes to: Events::ActivityUpdated

Instance Method Summary collapse

Instance Method Details

#perform(event) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'app/subscribers/activity/chain_runner_handler.rb', line 23

def perform(event)
  return unless event.data[:activity_result_type_id_changed]
  return unless event.data[:chain_deferred]

  activity = Activity.find_by(id: event.data[:activity_id])
  return unless activity

  Activity::ChainRunner.initialize_and_execute_chains(activity)
rescue StandardError => e
  ErrorReporting.error(e, activity_id: event.data[:activity_id])
  raise
end