Class: ScatterGather::GatherJob

Inherits:
ActiveJob::Base
  • Object
show all
Includes:
ScatterGather
Defined in:
lib/scatter_gather/gather_job.rb

Overview

Internal job class for polling and coordinating gather operations

Constant Summary

Constants included from ScatterGather

DEFAULT_GATHER_CONFIG, VERSION

Instance Method Summary collapse

Methods included from ScatterGather

#register_completion_for_gathering

Instance Method Details

#loggerObject



8
# File 'lib/scatter_gather/gather_job.rb', line 8

def logger = ActiveSupport::TaggedLogging.new(super).tagged("ScatterGather")

#perform(wait_for_active_job_ids:, target_job:, gather_config:, remaining_attempts:) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/scatter_gather/gather_job.rb', line 10

def perform(wait_for_active_job_ids:, target_job:, gather_config:, remaining_attempts:)
  dependency_statuses = ScatterGather::Completion.collect_statuses(wait_for_active_job_ids)
  logger.info { "Gathered completions #{tally_in_logger_format(dependency_statuses)}" }

  if ScatterGather::Completion.all_dependencies_completed?(dependency_statuses)
    logger.info { "Dependencies done, enqueueing #{target_job.fetch(:cn)}" }
    perform_target_later_from_args(target_job)
    ScatterGather::Completion.where(active_job_id: wait_for_active_job_ids).delete_all
  elsif remaining_attempts < 1
    max_attempts = gather_config.fetch(:max_attempts)
    error = ScatterGather::DependencyTimeoutError.new(max_attempts, dependency_statuses)
    logger.warn { "Failed to gather dependencies after #{max_attempts} attempts" }
    ScatterGather::Completion.where(active_job_id: wait_for_active_job_ids).delete_all

    # We configure our job to discard on timeout, and discard does not report the error by default
    Rails.error.report(error)
    raise error
  else
    # Re-enqueue with delay. We could poll only for dependencies which are still remaining,
    # but for debugging this is actually worse because for hanging stuff there will be one
    # job that hangs in the end. Knowing which jobs were part of the batch is useful!
    args = {
      wait_for_active_job_ids:,
      target_job:,
      gather_config:,
      remaining_attempts: remaining_attempts - 1
    }
    wait = gather_config.fetch(:poll_interval).seconds
    self.class.set(wait:).perform_later(**args)
  end
end