module Stenotype::Frameworks::Rails::ActiveJobExtension

An extension for ActiveJob to enable adding a hook before performing an instance of [ActiveJob::Base] subclass

Public Class Methods

extended(base) click to toggle source

@!visibility private

Calls superclass method
# File lib/stenotype/frameworks/rails/active_job.rb, line 17
def self.extended(base)
  base.const_set(:JobExt, Module.new)
  super
end

Public Instance Methods

trackable_job!() click to toggle source

@example

class MyJob < ApplicationJob
  trackable_job! # => will prepend a perform action with event recorder

  def perform(data)
    # do_something
  end
end
Calls superclass method
# File lib/stenotype/frameworks/rails/active_job.rb, line 33
def trackable_job!
  proxy = const_get(:JobExt)
  proxy.module_eval do
    define_method(:perform) do |*args, **rest_args, &block|
      Stenotype::Event.emit!(
        "active_job_#{self.class.name}",
        { type: "active_job" },
        { eval_context: { active_job: self }},
      )
      super(*args, **rest_args, &block)
    end
  end

  # Prepend an instance of module so that
  # super() can be chained down the ancestors
  # without changing existing ActiveJob interface
  #
  public_send(:prepend, proxy)
end