Class: Profiler::JobProfiler
- Inherits:
-
Object
- Object
- Profiler::JobProfiler
- 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, Collectors::MailerCollector ].freeze
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(job_class:, job_id:, queue:, arguments:, executions:, parent_token: nil) ⇒ JobProfiler
constructor
A new instance of JobProfiler.
- #run(&block) ⇒ Object
Constructor Details
#initialize(job_class:, job_id:, queue:, arguments:, executions:, parent_token: nil) ⇒ JobProfiler
Returns a new instance of JobProfiler.
43 44 45 46 47 48 49 50 |
# File 'lib/profiler/job_profiler.rb', line 43 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
30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/profiler/job_profiler.rb', line 30 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
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 |
# File 'lib/profiler/job_profiler.rb', line 52 def run(&block) profile = Models::Profile.new profile.profile_type = "job" profile.gem_version = Profiler::VERSION 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" = nil previous_token = Profiler::CurrentContext.token previous_job_class = Thread.current[:profiler_current_job_class] Profiler::CurrentContext.token = profile.token Thread.current[:profiler_current_job_class] = @job_class begin result = block.call result rescue => e job_status = "failed" = "#{e.class}: #{e.}" exception_collector&.capture(e) raise ensure Thread.current[:profiler_current_job_class] = previous_job_class Profiler::CurrentContext.token = previous_token if Profiler.configuration.track_memory profile.memory = current_memory - memory_before end job_collector.update_status(job_status, ) 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.}" end end Profiler.storage.save(profile.token, profile) end end |