Class: ScoutApm::BackgroundJobIntegrations::Sneakers

Inherits:
Object
  • Object
show all
Defined in:
lib/scout_apm/background_job_integrations/sneakers.rb

Constant Summary collapse

ACTIVE_JOB_KLASS =
'ActiveJob::QueueAdapters::SneakersAdapter::JobWrapper'.freeze
UNKNOWN_CLASS_PLACEHOLDER =
'UnknownJob'.freeze
UNKNOWN_QUEUE_PLACEHOLDER =
'default'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 4

def logger
  @logger
end

Instance Method Details

#forking?Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 14

def forking?
  false
end

#installObject



18
19
20
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 18

def install
  install_worker_override
end

#install_worker_overrideObject



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 22

def install_worker_override
  ::Sneakers::Worker.module_eval do
    def initialize_with_scout(*args)
      agent = ::ScoutApm::Agent.instance
      agent.start
      initialize_without_scout(*args)
    end

    alias_method :initialize_without_scout, :initialize
    alias_method :initialize, :initialize_with_scout

    def process_work_with_scout(*args)
      delivery_info, , msg, _handler = args

      queue = delivery_info[:routing_key] || UNKNOWN_QUEUE_PLACEHOLDER

      job_class = begin
        if self.class == ActiveJob::QueueAdapters::SneakersAdapter::JobWrapper
          msg["job_class"] || UNKNOWN_CLASS_PLACEHOLDER
        else
          self.class.name
        end
      rescue => e
        UNKNOWN_CLASS_PLACEHOLDER
      end

      req = ScoutApm::RequestManager.lookup

      # RabbitMQ does not store a created-at timestamp
      # req.annotate_request(:queue_latency => latency(msg))

      begin
        req.start_layer(ScoutApm::Layer.new('Queue', queue))
        started_queue = true
        req.start_layer(ScoutApm::Layer.new('Job', job_class))
        started_job = true

        process_work_without_scout(*args)
      rescue Exception => e
        req.error!
        raise
      ensure
        req.stop_layer if started_job
        req.stop_layer if started_queue
      end
    end

    alias_method :process_work_without_scout, :process_work
    alias_method :process_work, :process_work_with_scout
  end

  # msg = {
  #   "job_class":"DummyWorker",
  #   "job_id":"ea23ba1c-3022-4e05-870b-c3bcb1c4f328",
  #   "queue_name":"default",
  #   "arguments":["fjdkl"],
  #   "locale":"en"
  # }
end

#nameObject



6
7
8
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 6

def name
  :sneakers
end

#present?Boolean

Returns:

  • (Boolean)


10
11
12
# File 'lib/scout_apm/background_job_integrations/sneakers.rb', line 10

def present?
  defined?(::Sneakers)
end