Class: ApplicationJob

Inherits:
ActiveJob::Base
  • Object
show all
Includes:
ActionView::Helpers::NumberHelper, ActionView::Helpers::TextHelper, App47Logger
Defined in:
lib/app/jobs/application_job.rb

Overview

Base application job that all jobs extend from

Direct Known Subclasses

Cron::Job

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from App47Logger

clean_params, #clean_params, delete_parameter_keys, #log_controller_error, log_debug, #log_debug, log_error, #log_error, log_exception, #log_message, log_message, #log_warn, log_warn, mask_parameter_keys, #update_flash_messages

Instance Attribute Details

#payloadObject

Returns the value of attribute payload.



10
11
12
# File 'lib/app/jobs/application_job.rb', line 10

def payload
  @payload
end

#started_atObject

Returns the value of attribute started_at.



10
11
12
# File 'lib/app/jobs/application_job.rb', line 10

def started_at
  @started_at
end

Class Method Details

.valid_environment?Boolean

Is this job in a valid environment

Returns:

  • (Boolean)


37
38
39
40
# File 'lib/app/jobs/application_job.rb', line 37

def self.valid_environment?
  my_environments = valid_environments
  my_environments.empty? || my_environments.include?(Rails.env)
end

.valid_environmentsObject

If this job should run in this current environment, defaults to true



30
31
32
# File 'lib/app/jobs/application_job.rb', line 30

def self.valid_environments
  []
end

Instance Method Details

#durationObject

Public: Determine the duration of the process, only reporting on the values that are

greater than zero.

Examples

duration
# => '1 day 1 hour 32 minutes 10 seconds'
# => '1 hour 32 minutes 10 seconds'
# => '32 minutes 10 seconds'
# => '10 seconds'

Returns the duration up until this point in time.



76
77
78
79
80
81
82
# File 'lib/app/jobs/application_job.rb', line 76

def duration
  start_time = started_at.is_a?(String) ? Time.parse(started_at) : started_at
  minutes, seconds = split_duration(Time.zone.now - start_time)
  hours, minutes = split_duration minutes
  days, hours = split_duration hours, 24
  report_duration(days, hours, minutes, seconds)
end

#parse_payloadObject

Internal: Parse the payload

payload - The payload from the class attribute

Examples

parse_payload

Assigns the class attributes from the payload



53
54
55
56
57
58
59
60
# File 'lib/app/jobs/application_job.rb', line 53

def parse_payload
  attributes = payload.is_a?(Hash) ? payload : JSON.parse(payload)
  # Set the values contained in the payload
  attributes.each_pair do |key, value|
    method = "#{key}=".to_sym
    send(method, value) if respond_to?(method)
  end
end

#perform(payload = {}) ⇒ Object

Standard approach to running jobs



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/app/jobs/application_job.rb', line 15

def perform(payload = {})
  @started_at = Time.now.utc
  @payload = payload
  parse_payload
  execute
rescue StandardError => error
  log_error "Failed to execute job: #{self.inspect}", error
  raise error
ensure
  GC.start
end