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[
  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)


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

thread_mattr_accessor :cron_at

.cron_keyString?

Cron Key

Returns:

  • (String, nil)


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

thread_mattr_accessor :cron_key

.error_on_discardException?

Error captured by discard_on

Returns:

  • (Exception, nil)


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

thread_mattr_accessor :error_on_discard

.error_on_retryException?

Error captured by retry_on

Returns:

  • (Exception, nil)


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

thread_mattr_accessor :error_on_retry

.error_on_retry_stoppedException?

Error captured by retry_stopped

Returns:

  • (Exception, nil)


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

thread_mattr_accessor :error_on_retry_stopped

.execution_interruptedBoolean?

Execution Interrupted

Returns:

  • (Boolean, nil)


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

thread_mattr_accessor :execution_interrupted

.jobsGoodJob::Job?

Execution

Returns:



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

thread_mattr_accessor :job

.retried_jobGoodJob::Job?

Execution Retried

Returns:



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

thread_mattr_accessor :retried_job

.retry_nowBoolean?

Execution Retried

Returns:

  • (Boolean, nil)


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

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



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

def self.active_job_id
  job&.active_job_id
end

.process_idInteger

Returns Current process ID.

Returns:

  • (Integer)

    Current process ID



99
100
101
# File 'lib/good_job/current_thread.rb', line 99

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



79
80
81
82
83
# File 'lib/good_job/current_thread.rb', line 79

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



104
105
106
# File 'lib/good_job/current_thread.rb', line 104

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

.to_hHash

Exports values to hash

Returns:

  • (Hash)


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

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)


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

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