Module: GoodJob::CurrentThread

Defined in:
lib/good_job/current_thread.rb

Overview

Thread-local attributes for passing values from Instrumentation. (Cannot use ActiveSupport::CurrentAttributes because ActiveJob resets it)

Constant Summary collapse

ACCESSORS =

Resettable accessors for thread-local values.

%i[
  active_job
  cron_at
  cron_key
  error_on_discard
  error_on_retry
  error_on_retry_stopped
  job
  execution_interrupted
  retried_job
  retry_now
].freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.cron_atDateTime?

Cron At

Returns:

  • (DateTime, nil)


27
# File 'lib/good_job/current_thread.rb', line 27

thread_mattr_accessor :active_job

.cron_keyString?

Cron Key

Returns:

  • (String, nil)


39
# File 'lib/good_job/current_thread.rb', line 39

thread_mattr_accessor :cron_key

.error_on_discardException?

Error captured by discard_on

Returns:

  • (Exception, nil)


45
# File 'lib/good_job/current_thread.rb', line 45

thread_mattr_accessor :error_on_discard

.error_on_retryException?

Error captured by retry_on

Returns:

  • (Exception, nil)


51
# File 'lib/good_job/current_thread.rb', line 51

thread_mattr_accessor :error_on_retry

.error_on_retry_stoppedException?

Error captured by retry_stopped

Returns:

  • (Exception, nil)


57
# File 'lib/good_job/current_thread.rb', line 57

thread_mattr_accessor :error_on_retry_stopped

.execution_interruptedBoolean?

Execution Interrupted

Returns:

  • (Boolean, nil)


69
# File 'lib/good_job/current_thread.rb', line 69

thread_mattr_accessor :execution_interrupted

.jobsGoodJob::Job?

Execution

Returns:



63
# File 'lib/good_job/current_thread.rb', line 63

thread_mattr_accessor :job

.retried_jobGoodJob::Job?

Execution Retried

Returns:



75
# File 'lib/good_job/current_thread.rb', line 75

thread_mattr_accessor :retried_job

.retry_nowBoolean?

Execution Retried

Returns:

  • (Boolean, nil)


81
# File 'lib/good_job/current_thread.rb', line 81

thread_mattr_accessor :retry_now

Class Method Details

.active_job_idString

Returns UUID of the currently executing GoodJob::Job.

Returns:

  • (String)

    UUID of the currently executing GoodJob::Job



101
102
103
# File 'lib/good_job/current_thread.rb', line 101

def self.active_job_id
  job&.active_job_id
end

.process_idInteger

Returns Current process ID.

Returns:

  • (Integer)

    Current process ID



106
107
108
# File 'lib/good_job/current_thread.rb', line 106

def self.process_id
  ::Process.pid
end

.reset(values = {}) ⇒ void

This method returns an undefined value.

Resets attributes

Parameters:

  • values (Hash) (defaults to: {})

    to assign



86
87
88
89
90
# File 'lib/good_job/current_thread.rb', line 86

def self.reset(values = {})
  ACCESSORS.each do |accessor|
    send(:"#{accessor}=", values[accessor])
  end
end

.thread_nameString

Returns Current thread name.

Returns:

  • (String)

    Current thread name



111
112
113
# File 'lib/good_job/current_thread.rb', line 111

def self.thread_name
  (Thread.current.name || Thread.current.object_id).to_s
end

.to_hHash

Exports values to hash

Returns:

  • (Hash)


94
95
96
97
98
# File 'lib/good_job/current_thread.rb', line 94

def self.to_h
  ACCESSORS.index_with do |accessor|
    send(accessor)
  end
end

.within {|self| ... } ⇒ void

This method returns an undefined value.

Wrap the yielded block with CurrentThread values and reset after the block

Yields:

  • (self)


118
119
120
121
122
123
# File 'lib/good_job/current_thread.rb', line 118

def self.within
  original_values = to_h
  yield(self)
ensure
  reset(original_values)
end