Skip to content

Instantly share code, notes, and snippets.

@amolpujari
Last active April 18, 2020 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amolpujari/0a4899e7f642b9e6b4cec51f0b6a3840 to your computer and use it in GitHub Desktop.
Save amolpujari/0a4899e7f642b9e6b4cec51f0b6a3840 to your computer and use it in GitHub Desktop.
rails 5 application job
class ApplicationJob < ActiveJob::Base
include ::PrivateInspector
include ::SanitizerHelper
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked
# Most jobs are safe to ignore if the underlying records are no longer available
# discard_on ActiveJob::DeserializationError
rescue_from Exception do |e|
ExceptionNotifier.notify_exception(e, data: { data: private_inspect })
if Rails.env.development?
Rails.logger.error e.message
Rails.logger.error e.backtrace.join("\r\n")
end
end
def perform *args
options = args.last || {}
@start_time = Time.now
@result = execute *args
@end_time = Time.now
@time_taken = "#{TimeDifference.between(@start_time, @end_time).in_ms}ms"
ensure
unless self.class.do_not_notify_on_finish?
notify_finished unless options[:application_job_do_not_notify]
end
end
def notify_finished
Notifier.notify_dev "#{self.class.name} finished", private_inspect.merge!({ application_job_do_not_notify: true})
end
def self.do_not_notify_on_finish
@do_not_notify_on_finish = true
end
def self.do_not_notify_on_finish?
@do_not_notify_on_finish
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment