Module: WorkerTools::RocketchatErrorNotifier

Defined in:
lib/worker_tools/rocketchat_error_notifier.rb

Instance Method Summary collapse

Instance Method Details

#rocketchat_error_notifier_attachment(error) ⇒ Object



55
56
57
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 55

def rocketchat_error_notifier_attachment(error)
  { collapsed: true, title: 'Error', text: rocketchat_error_notifier_error_details(error) }
end

#rocketchat_error_notifier_emojiObject



14
15
16
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 14

def rocketchat_error_notifier_emoji
  ':red_circle:'
end

#rocketchat_error_notifier_enabledObject



10
11
12
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 10

def rocketchat_error_notifier_enabled
  Rails.env.production?
end

#rocketchat_error_notifier_error_details(error) ⇒ Object



43
44
45
46
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 43

def rocketchat_error_notifier_error_details(error)
  details = "#{error.class}: #{error.message}\n"
  details << error.backtrace[0..10].join("\n")
end

#rocketchat_error_notifier_eventObject



22
23
24
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 22

def rocketchat_error_notifier_event
  'Worker Error Notifier'
end

#rocketchat_error_notifier_messageObject



48
49
50
51
52
53
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 48

def rocketchat_error_notifier_message
  message = []
  message << rocketchat_error_notifier_receivers
  message << rocketchat_error_notifier_title
  message.compact.join(' - ')
end

#rocketchat_error_notifier_receiversObject



18
19
20
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 18

def rocketchat_error_notifier_receivers
  # Ex: '@all'
end

#rocketchat_error_notifier_titleObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 26

def rocketchat_error_notifier_title
  # Example with a link:
  #
  # For urls a default_url_options[:host] might be necessary.
  # In this example I just copy it from existing action_mailer defaults.
  #
  # import = rocketchat_error_notifier_model
  # host = Rails.application.config.action_mailer.default_url_options[:host]
  # url = Rails.application.routes.url_helpers.import_url(import, host: host, protocol: :https)
  # kind = I18n.t(import.kind, scope: 'import.kinds')
  # text = "##{import.id} *#{kind}*"
  # "[#{text}](#{url})"
  klass = model.class.model_name.human
  kind = I18n.t("activerecord.attributes.#{model.class.name.underscore}.kinds.#{model.kind}")
  "#{klass} #{kind} ##{model.id}"
end

#rocketchat_error_notify(error) ⇒ Object



59
60
61
62
63
64
65
66
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 59

def rocketchat_error_notify(error)
  RocketChatNotifier.notify(
    rocketchat_error_notifier_message,
    emoji: rocketchat_error_notifier_emoji,
    event: "#{rocketchat_error_notifier_event} (#{Rails.env})",
    attachment: rocketchat_error_notifier_attachment(error)
  )
end

#with_wrapper_rocketchat_error_notifier(&block) ⇒ Object



3
4
5
6
7
8
# File 'lib/worker_tools/rocketchat_error_notifier.rb', line 3

def with_wrapper_rocketchat_error_notifier(&block)
  block.yield
rescue StandardError => e
  rocketchat_error_notify(e) if rocketchat_error_notifier_enabled
  raise
end