Class: Resque::Failure::Slack

Inherits:
Base
  • Object
show all
Defined in:
lib/resque/failure/slack.rb

Constant Summary collapse

LEVELS =
%i(verbose compact minimal)
SLACK_URL =
'https://slack.com/api'

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.channelObject

Slack channel id.



12
13
14
# File 'lib/resque/failure/slack.rb', line 12

def channel
  @channel
end

.levelObject

Notification style:

verbose: full backtrace (default) compact: exception only minimal: worker and payload



20
21
22
# File 'lib/resque/failure/slack.rb', line 20

def level
  @level
end

.tokenObject

Team token



13
14
15
# File 'lib/resque/failure/slack.rb', line 13

def token
  @token
end

Class Method Details

.configure {|_self| ... } ⇒ Object

Configures the failure backend. You will need to set a channel id and a team token.

Examples:

Configure your Slack account:

Resque::Failure::Slack.configure do |config|
  config.channel = 'CHANNEL_ID'
  config.token = 'TOKEN'
  config.verbose = true or false, true is the default
end

Yields:

  • (_self)

Yield Parameters:



36
37
38
39
# File 'lib/resque/failure/slack.rb', line 36

def self.configure
  yield self
  fail 'Slack channel and token are not configured.' unless configured?
end

.configured?Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/resque/failure/slack.rb', line 41

def self.configured?
  !!channel && !!token
end

Instance Method Details

#report_exceptionObject

Sends a HTTP Post to the Slack api.



56
57
58
59
60
# File 'lib/resque/failure/slack.rb', line 56

def report_exception
  uri = URI.parse(SLACK_URL + '/chat.postMessage')
  params = { 'channel' => self.class.channel, 'token' => self.class.token, 'text' => text }
  Net::HTTP.post_form(uri, params)
end

#saveObject

Sends the exception data to the Slack channel.

When a job fails, a new instance is created and #save is called.



48
49
50
51
52
# File 'lib/resque/failure/slack.rb', line 48

def save
  return unless self.class.configured?

  report_exception
end

#textObject

Text to be displayed in the Slack notification



64
65
66
# File 'lib/resque/failure/slack.rb', line 64

def text
  Notification.generate(self, self.class.level)
end