Class: Profiler::JobProfiler

Inherits:
Object
  • Object
show all
Defined in:
lib/profiler/job_profiler.rb

Constant Summary collapse

JOB_COLLECTOR_CLASSES =
[
  Collectors::DatabaseCollector,
  Collectors::CacheCollector,
  Collectors::HttpCollector,
  Collectors::DumpCollector,
  Collectors::LogCollector,
  Collectors::ExceptionCollector,
  Collectors::EnvCollector,
  Collectors::FlameGraphCollector
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_class:, job_id:, queue:, arguments:, executions:, parent_token: nil) ⇒ JobProfiler

Returns a new instance of JobProfiler.



41
42
43
44
45
46
47
48
# File 'lib/profiler/job_profiler.rb', line 41

def initialize(job_class:, job_id:, queue:, arguments:, executions:, parent_token: nil)
  @job_class = job_class
  @job_id = job_id
  @queue = queue
  @arguments = arguments
  @executions = executions
  @parent_token = parent_token
end

Class Method Details

.profile(job_class:, job_id:, queue:, arguments:, executions:, parent_token: nil, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/profiler/job_profiler.rb', line 28

def self.profile(job_class:, job_id:, queue:, arguments:, executions:, parent_token: nil, &block)
  return block.call unless Profiler.enabled? && Profiler.configuration.track_jobs

  new(
    job_class: job_class,
    job_id: job_id,
    queue: queue,
    arguments: arguments,
    executions: executions,
    parent_token: parent_token
  ).run(&block)
end

Instance Method Details

#run(&block) ⇒ Object



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
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
# File 'lib/profiler/job_profiler.rb', line 50

def run(&block)
  profile = Models::Profile.new
  profile.profile_type = "job"
  profile.path = @job_class
  profile.method = "JOB"
  profile.parent_token = @parent_token if @parent_token

  job_collector = Collectors::JobCollector.new(profile, {
    job_class: @job_class,
    job_id: @job_id,
    queue: @queue,
    arguments: sanitize_arguments(@arguments),
    executions: @executions
  })

  collectors = [job_collector] + JOB_COLLECTOR_CLASSES.map { |klass| klass.new(profile) }
  collectors.each { |c| c.subscribe if c.respond_to?(:subscribe) }

  exception_collector = collectors.find { |c| c.is_a?(Collectors::ExceptionCollector) }

  memory_before = current_memory if Profiler.configuration.track_memory

  job_status = "completed"
  error_message = nil

  previous_token = Profiler::CurrentContext.token
  Profiler::CurrentContext.token = profile.token
  begin
    result = block.call
    result
  rescue => e
    job_status = "failed"
    error_message = "#{e.class}: #{e.message}"
    exception_collector&.capture(e)
    raise
  ensure
    Profiler::CurrentContext.token = previous_token
    if Profiler.configuration.track_memory
      profile.memory = current_memory - memory_before
    end

    job_collector.update_status(job_status, error_message)
    profile.finish(job_status == "completed" ? 200 : 500)

    collectors.each do |collector|
      begin
        collector.collect if collector.respond_to?(:collect)
        profile.(collector)
      rescue => e
        warn "Profiler JobProfiler: Collector #{collector.class} failed: #{e.message}"
      end
    end

    Profiler.storage.save(profile.token, profile)
  end
end