Class: Olyx::Guardrails::Rails::ActiveJobHandler

Inherits:
Object
  • Object
show all
Defined in:
lib/olyx/guardrails/rails/active_job_handler.rb

Overview

Enqueues sanitized notification events through an Active Job class.

A String or Symbol job name resolves at delivery time, making it safe for Rails code reloading.

Instance Method Summary collapse

Constructor Details

#initialize(job:, queue: nil) ⇒ ActiveJobHandler

:call-seq:

ActiveJobHandler.new(job:, queue: nil) -> ActiveJobHandler

Builds and freezes a handler. job is an Active Job class or a String/Symbol constant name. queue is an optional non-empty String or Symbol.



20
21
22
23
24
# File 'lib/olyx/guardrails/rails/active_job_handler.rb', line 20

def initialize(job:, queue: nil)
  @job = JobReference.new(job)
  @queue = QueueName.call(queue)
  freeze
end

Instance Method Details

#call(event) ⇒ Object

:call-seq:

handler.call(event)

Enqueues event with perform_later. event must be a Hash. The resolved job target must respond to perform_later.

Raises:

  • (ArgumentError)


31
32
33
34
35
36
37
# File 'lib/olyx/guardrails/rails/active_job_handler.rb', line 31

def call(event)
  raise ArgumentError, 'Active Job notification event must be a Hash' unless event.is_a?(Hash)

  target = @job.resolve
  target = target.set(queue: @queue) if @queue
  target.perform_later(event)
end