Module: WorkerTools::SlackErrorNotifier

Defined in:
lib/worker_tools/slack_error_notifier.rb

Instance Method Summary collapse

Instance Method Details

#slack_error_notifiable?(error) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/worker_tools/slack_error_notifier.rb', line 16

def slack_error_notifiable?(error)
  error.is_a?(StandardError)
end

#slack_error_notifierObject



95
96
97
# File 'lib/worker_tools/slack_error_notifier.rb', line 95

def slack_error_notifier
  Slack::Notifier.new(slack_error_notifier_webhook)
end

#slack_error_notifier_attachments(error) ⇒ Object



77
78
79
80
81
82
83
84
85
86
# File 'lib/worker_tools/slack_error_notifier.rb', line 77

def slack_error_notifier_attachments(error)
  [
    { color: slack_error_notifier_attachments_color, fields: slack_error_notifier_attachments_fields },
    {
      title: [error.class, error.message].join(' : '),
      color: slack_error_notifier_attachments_color,
      text: slack_error_notifier_error_details(error)
    }
  ]
end

#slack_error_notifier_attachments_colorObject



44
45
46
47
# File 'lib/worker_tools/slack_error_notifier.rb', line 44

def slack_error_notifier_attachments_color
  # good, warning, danger, hex color
  'danger'
end

#slack_error_notifier_attachments_fieldsObject



88
89
90
91
92
93
# File 'lib/worker_tools/slack_error_notifier.rb', line 88

def slack_error_notifier_attachments_fields
  [
    { title: 'Application', value: Rails.application.class.module_parent_name, short: true },
    { title: 'Environment', value: Rails.env, short: true }
  ]
end

#slack_error_notifier_channelObject



24
25
26
27
28
# File 'lib/worker_tools/slack_error_notifier.rb', line 24

def slack_error_notifier_channel
  return SLACK_NOTIFIER_CHANNEL if defined?(SLACK_NOTIFIER_CHANNEL)

  raise 'Define slack_error_notifier_channel or set SLACK_NOTIFIER_CHANNEL in an initializer'
end

#slack_error_notifier_emojiObject



20
21
22
# File 'lib/worker_tools/slack_error_notifier.rb', line 20

def slack_error_notifier_emoji
  ':red_circle:'
end

#slack_error_notifier_enabledObject



12
13
14
# File 'lib/worker_tools/slack_error_notifier.rb', line 12

def slack_error_notifier_enabled
  Rails.env.production?
end

#slack_error_notifier_error_details(error) ⇒ Object



66
67
68
# File 'lib/worker_tools/slack_error_notifier.rb', line 66

def slack_error_notifier_error_details(error)
  error.backtrace[0..2].join("\n")
end

#slack_error_notifier_messageObject



70
71
72
73
74
75
# File 'lib/worker_tools/slack_error_notifier.rb', line 70

def slack_error_notifier_message
  message = []
  message << slack_error_notifier_receivers
  message << slack_error_notifier_title
  message.compact.join(' - ')
end

#slack_error_notifier_receiversObject



40
41
42
# File 'lib/worker_tools/slack_error_notifier.rb', line 40

def slack_error_notifier_receivers
  # Ex: '@all'
end

#slack_error_notifier_titleObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/worker_tools/slack_error_notifier.rb', line 49

def slack_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 = slack_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

#slack_error_notifier_usernameObject



36
37
38
# File 'lib/worker_tools/slack_error_notifier.rb', line 36

def slack_error_notifier_username
  'Notifier'
end

#slack_error_notifier_webhookObject



30
31
32
33
34
# File 'lib/worker_tools/slack_error_notifier.rb', line 30

def slack_error_notifier_webhook
  return SLACK_NOTIFIER_WEBHOOK if defined?(SLACK_NOTIFIER_WEBHOOK)

  raise 'Define slack_error_notifier_webhook or set SLACK_NOTIFIER_WEBHOOK in an initializer'
end

#slack_error_notify(error) ⇒ Object



99
100
101
102
103
104
105
106
107
# File 'lib/worker_tools/slack_error_notifier.rb', line 99

def slack_error_notify(error)
  slack_error_notifier.post(
    username: slack_error_notifier_username,
    channel: slack_error_notifier_channel,
    icon_emoji: slack_error_notifier_emoji,
    text: "*#{slack_error_notifier_message}*",
    attachments: slack_error_notifier_attachments(error)
  )
end

#with_wrapper_slack_error_notifier(&block) ⇒ Object



5
6
7
8
9
10
# File 'lib/worker_tools/slack_error_notifier.rb', line 5

def with_wrapper_slack_error_notifier(&block)
  block.yield
rescue StandardError => e
  slack_error_notify(e) if slack_error_notifier_enabled && slack_error_notifiable?(e)
  raise
end