Module: AcidicJob::Mixin

Extended by:
ActiveSupport::Concern
Included in:
ActiveKiq, Base
Defined in:
lib/acidic_job/mixin.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.wire_up(other) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/acidic_job/mixin.rb', line 9

def self.wire_up(other)
  # Ensure our `perform` method always runs first to gather parameters
  # and run perform callbacks for Sidekiq workers
  other.prepend PerformWrapper
  # Ensure both configured and base jobs can be performed acidicly
  other.include PerformAcidicly

  # By default, we unique job runs by the `job_id`
  other.instance_variable_set(:@acidic_identifier, :job_id)
  # However, you can customize this behavior on a per job class level
  other.define_singleton_method(:acidic_by_job_identifier) { @acidic_identifier = :job_id }
  # You could unique job runs by the arguments passed to the job (e.g. memoization)
  other.define_singleton_method(:acidic_by_job_arguments) { @acidic_identifier = :job_arguments }
  # Or, you could unique jobs run by any logic you'd like using a block
  other.define_singleton_method(:acidic_by) { |proc = nil, &block| @acidic_identifier = proc || block }

  # We add a callback to ensure that staged, non-workflow jobs are "finished" after they are "performed".
  # This allows us to ensure that we can always inspect whether a run is finished and get correct data
  other.set_callback :perform, :after, :finish_staged_job, if: -> { was_staged_job? && !was_workflow_job? }
  # We also allow you to write any of your own callbacks keyed to the "finish" event.
  # The "finish" event is notably different from the "perform" event for acidic jobs,
  # as any acidic job can have one or more "perform" event (retries or a resume after awaiting jobs),
  # but will only ever have one "finish" event (when the run successfully completes)
  other.define_callbacks :finish
end

Instance Method Details

#idempotency_keyObject



60
61
62
# File 'lib/acidic_job/mixin.rb', line 60

def idempotency_key
  IdempotencyKey.new(self).value(acidic_by: acidic_identifier)
end

#set(options = {}) ⇒ Object

Configures the job with the given options.



65
66
67
68
69
70
71
72
# File 'lib/acidic_job/mixin.rb', line 65

def set(options = {}) # :nodoc:
  self.scheduled_at = options[:wait].seconds.from_now.to_f if options[:wait]
  self.scheduled_at = options[:wait_until].to_f if options[:wait_until]
  self.queue_name   = self.class.queue_name_from_part(options[:queue]) if options[:queue]
  self.priority     = options[:priority].to_i if options[:priority]

  self
end