Class: Railscope::Subscribers::JobSubscriber

Inherits:
BaseSubscriber show all
Defined in:
lib/railscope/subscribers/job_subscriber.rb

Constant Summary collapse

ENQUEUE_EVENT =
"enqueue.active_job"
PERFORM_START_EVENT =
"perform_start.active_job"
PERFORM_EVENT =
"perform.active_job"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.subscribe ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/railscope/subscribers/job_subscriber.rb', line 10

def self.subscribe
  return if @subscribed

  @subscribed = true

  ActiveSupport::Notifications.subscribe(ENQUEUE_EVENT) do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    new.record_enqueue(event)
  end

  # Setup context BEFORE job runs (so queries are linked)
  ActiveSupport::Notifications.subscribe(PERFORM_START_EVENT) do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    new.setup_perform_context(event)
  end

  # Record result AFTER job completes
  ActiveSupport::Notifications.subscribe(PERFORM_EVENT) do |*args|
    event = ActiveSupport::Notifications::Event.new(*args)
    new.record_perform(event)
  end
end

Instance Method Details

#record_enqueue(event) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/railscope/subscribers/job_subscriber.rb', line 41

def record_enqueue(event)
  return unless Railscope.enabled?
  return unless Railscope.ready?
  return if ignore_job?(event.payload[:job])

  job = event.payload[:job]

  create_entry!(
    entry_type: "job_enqueue",
    payload: build_enqueue_payload(event),
    tags: build_tags(event, "enqueue"),
    family_hash: build_family_hash(job),
    should_display_on_index: true
  )
rescue StandardError => e
  Rails.logger.error("[Railscope] Failed to record job enqueue: #{e.message}")
end

#record_perform(event) ⇒ Object



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
# File 'lib/railscope/subscribers/job_subscriber.rb', line 59

def record_perform(event)
  return unless Railscope.enabled?
  return unless Railscope.ready?
  return if ignore_job?(event.payload[:job])

  job = event.payload[:job]
  exception_object = event.payload[:exception_object]

  # Create the job perform entry
  create_entry!(
    entry_type: "job_perform",
    payload: build_perform_payload(event),
    tags: build_tags(event, "perform"),
    family_hash: build_family_hash(job),
    should_display_on_index: true
  )

  # Also create a separate exception entry if job failed
  create_exception_entry!(job, exception_object) if exception_object

  # In conditional mode: flush if triggered, otherwise entries are discarded with context
  if Railscope.conditional_recording? && context.triggered?
    # Response update for jobs is already in the entry payload, nothing extra needed
  end

  # Clear context after job completes (discards any unflushed buffer)
  Railscope::Context.clear!
rescue StandardError => e
  Rails.logger.error("[Railscope] Failed to record job perform: #{e.message}")
end

#setup_perform_context(event) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/railscope/subscribers/job_subscriber.rb', line 33

def setup_perform_context(event)
  return unless Railscope.enabled?
  return if ignore_job?(event.payload[:job])

  job = event.payload[:job]
  setup_job_context(job)
end