Module: Gouda::ActiveJobExtensions::Concurrency

Extended by:
ActiveSupport::Concern
Defined in:
lib/gouda/active_job_extensions/concurrency.rb

Constant Summary collapse

VALID_TYPES =
[String, Symbol, Numeric, Date, Time, TrueClass, FalseClass, NilClass].freeze

Instance Method Summary collapse

Instance Method Details

#_gouda_concurrency_extension_automatic_key_from_class_and_argsObject

Generates automatic serialized sha1 key



47
48
49
50
51
52
53
54
55
56
# File 'lib/gouda/active_job_extensions/concurrency.rb', line 47

def _gouda_concurrency_extension_automatic_key_from_class_and_args
  # To have a stable serialization of an ActiveJob we can re-use the method defined by
  # ActiveJob itself. We need to have the job class name and all the arguments, and for arguments
  # which are ActiveRecords or derivatives - we want them converted into global IDs. This also avoids
  # having attributes of the argument ActiveModels contribute to the concurrency key.
  # Add "cursor_position" from job-iteration so that different offsets of the same job can run
  # concurrently.
  pertinent_job_attributes = serialize.slice("job_class", "arguments", "priority", "cursor_position")
  Digest::SHA1.hexdigest(JSON.dump(pertinent_job_attributes))
end

#_gouda_concurrency_extension_key_via_configObject

Generates the concurrency key from the configuration

Raises:

  • (TypeError)


59
60
61
62
63
64
65
66
67
# File 'lib/gouda/active_job_extensions/concurrency.rb', line 59

def _gouda_concurrency_extension_key_via_config
  key = self.class.gouda_concurrency_config[:key]
  return if key.blank?

  key = key.respond_to?(:call) ? instance_exec(&key) : key
  raise TypeError, "Concurrency key must be a String; was a #{key.class}" unless VALID_TYPES.any? { |type| key.is_a?(type) }

  key
end

#enqueue_concurrency_keyObject

This method will be tried by the Gouda adapter



29
30
31
32
33
34
35
# File 'lib/gouda/active_job_extensions/concurrency.rb', line 29

def enqueue_concurrency_key
  job_config = self.class.try(:gouda_concurrency_config)
  return unless job_config
  return unless job_config[:enqueue_limit]

  _gouda_concurrency_extension_key_via_config || _gouda_concurrency_extension_automatic_key_from_class_and_args
end

#execution_concurrency_keyObject

This method will be tried by the Gouda adapter



38
39
40
41
42
43
44
# File 'lib/gouda/active_job_extensions/concurrency.rb', line 38

def execution_concurrency_key
  job_config = self.class.try(:gouda_concurrency_config)
  return unless job_config
  return unless job_config[:perform_limit]

  _gouda_concurrency_extension_key_via_config || _gouda_concurrency_extension_automatic_key_from_class_and_args
end