Class: Slackiq

Inherits:
Object
  • Object
show all
Defined in:
lib/slackiq.rb,
lib/slackiq/version.rb

Constant Summary collapse

VERSION =
"1.3.1".freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Slackiq

Returns a new instance of Slackiq.

Parameters:

  • options (Hash)


24
25
26
# File 'lib/slackiq.rb', line 24

def initialize(options)
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.configure(webhook_urls = {}) ⇒ Object

Parameters:

  • options (Hash)


19
20
21
# File 'lib/slackiq.rb', line 19

def self.configure(webhook_urls={})
  @webhook_urls = webhook_urls
end

.notify(options) ⇒ Object

Parameters:

  • options (Hash)


13
14
15
16
# File 'lib/slackiq.rb', line 13

def self.notify(options)
  raise "Need to run Slackiq.configure first" if @webhook_urls.nil?
  new(options.merge(webhook_urls: @webhook_urls)).execute
end

Instance Method Details

#executeObject

Send a notification to Slack with Sidekiq info about the batch



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/slackiq.rb', line 29

def execute
  time_now = Time.now

  title    = options[:title]
  status   = options[:status]

  if (bid = options[:bid]) && status.nil?
    raise <<~EOT.chomp unless defined?(Sidekiq::Batch::Status)
             Sidekiq::Batch::Status is not defined. \
             Are you sure Sidekiq Pro is set up correctly?
             EOT
    status = Sidekiq::Batch::Status.new(bid)
  end

  return if status.nil?

  color      = options[:color] || color_for(status)

  duration   = elapsed_time_humanized(status.created_at, time_now)
  time_title = status.complete? ? "Completed" : "Now"
  jobs_run   = status.total - status.pending

  return if status.total.to_f <= 0

  completion_percentage = percentage(jobs_run        / status.total.to_f)
  failure_percentage    = percentage(status.failures / status.total.to_f)

  fields = [
    {
      title: title,
      value: status.description,
      short: false
    },
    {
      title: "Batch ID",
      value: status.bid,
      short: false
    },
    {
      title: "Created",
      value: time_format(status.created_at),
      short: true
    },
    {
      title: time_title,
      value: time_format(time_now),
      short: true
    },
    {
      title: "Duration",
      value: duration,
      short: true
    },
    {
      title: "Total Jobs",
      value: status.total,
      short: true
    },
    {
      title: "Jobs Run",
      value: jobs_run,
      short: true
    },
    {
      title: "Completion %",
      value: completion_percentage,
      short: true
    },
    {
      title: "Failures",
      value: status.failures,
      short: true
    },
    {
      title: "Failure %",
      value: failure_percentage,
      short: true
    }
  ]

  body = {
    attachments: [
      fields: fields,
      color:  color
    ]
  }
  http_post(body)
end