Class: ScoutApm::BackgroundJobIntegrations::Que

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

Constant Summary collapse

UNKNOWN_CLASS_PLACEHOLDER =
'UnknownJob'.freeze
ACTIVE_JOB_KLASS =
'ActiveJob::QueueAdapters::QueAdapter::JobWrapper'.freeze
UNKNOWN_QUEUE_PLACEHOLDER =
'UnknownQueue'.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



9
10
11
# File 'lib/scout_apm/background_job_integrations/que.rb', line 9

def logger
  @logger
end

Instance Method Details

#forking?Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/scout_apm/background_job_integrations/que.rb', line 23

def forking?
  false
end

#installObject



27
28
29
30
31
# File 'lib/scout_apm/background_job_integrations/que.rb', line 27

def install
  install_tracer
  install_worker
  install_job
end

#install_jobObject



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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/scout_apm/background_job_integrations/que.rb', line 52

def install_job
  ::Que::Job.class_eval do
    # attrs = {
    #   "queue"=>"",
    #   "priority"=>100,
    #   "run_at"=>2018-12-19 15:12:32 -0700,
    #   "job_id"=>4,
    #   "job_class"=>"ExampleJob",
    #   "args"=>[{"key"=>"value"}],
    #   "error_count"=>0
    # }
    #
    # With ActiveJob, v0.14.3:
    # attrs = {
    #   "queue"=>"",
    #   "priority"=>100,
    #   "run_at"=>2018-12-19 15:29:18 -0700,
    #   "job_id"=>6,
    #   "job_class"=>"ActiveJob::QueueAdapters::QueAdapter::JobWrapper",
    #   "args"=> [{
    #     "job_class"=>"ExampleJob",
    #     "job_id"=>"60798b45-4b55-436e-806d-693939266c97",
    #     "provider_job_id"=>nil,
    #     "queue_name"=>"default",
    #     "priority"=>nil,
    #     "arguments"=>[],
    #     "executions"=>0,
    #     "locale"=>"en"
    #   }],
    #   "error_count"=>0
    # }
    #
    # With ActiveJob, v1.0:
    #   There are changes here to make Que more compatible with ActiveJob
    #   and this probably needs to be revisited.

    def _run_with_scout(*args)
      # default queue unless specifed is a blank string
      queue = attrs['queue'] || UNKNOWN_QUEUE_PLACEHOLDER

      job_class = begin
                    if self.class == ActiveJob::QueueAdapters::QueAdapter::JobWrapper
                      Array(attrs['args']).first['job_class'] || UNKNOWN_CLASS_PLACEHOLDER
                    else
                      self.class.name
                    end
                  rescue => e
                    UNKNOWN_CLASS_PLACEHOLDER
                  end

      latency = begin
                  Time.now - attrs['run_at']
                rescue
                  0
                end

      req = ScoutApm::RequestManager.lookup
      req.annotate_request(:queue_latency => latency)

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

        _run_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 :_run_without_scout, :_run
    alias_method :_run, :_run_with_scout
  end
end

#install_tracerObject



33
34
35
36
37
# File 'lib/scout_apm/background_job_integrations/que.rb', line 33

def install_tracer
  ::Que::Job.class_eval do
    include ScoutApm::Tracer
  end
end

#install_workerObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/scout_apm/background_job_integrations/que.rb', line 39

def install_worker
  ::Que::Worker.class_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
  end
end

#nameObject



11
12
13
# File 'lib/scout_apm/background_job_integrations/que.rb', line 11

def name
  :que
end

#present?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
20
21
# File 'lib/scout_apm/background_job_integrations/que.rb', line 15

def present?
  if defined?(::Que) && File.basename($PROGRAM_NAME).start_with?('que')
    # 0.x releases used "Version" constant.
    # 1.x releases used "VERSION" constant.
    return defined?(::Que::Version)
  end
end