Module: Slackiq

Defined in:
lib/slackiq.rb,
lib/slackiq/version.rb,
lib/slackiq/time_helper.rb

Defined Under Namespace

Modules: TimeHelper

Constant Summary collapse

VERSION =
"1.1.4"

Class Method Summary collapse

Class Method Details

.configure(webhook_urls = {}) ⇒ Object

Configure all of the webhook URLs you’re going to use

Author:

  • Jason Lew



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

def configure(webhook_urls={})
  raise 'Argument must be a Hash' unless webhook_urls.class == Hash
  @@webhook_urls = webhook_urls
end

.message(text, options) ⇒ Object

Send a notification without Sidekiq batch info

Author:

  • Jason Lew



140
141
142
143
144
145
146
# File 'lib/slackiq.rb', line 140

def message(text, options)
  url = @@webhook_urls[options[:webhook_name]]

  body = { 'text' => text }.to_json
  
  HTTParty.post(url, body: body)
end

.notify(options = {}) ⇒ Object

Send a notification to Slack with Sidekiq info about the batch

Author:

  • Jason Lew



24
25
26
27
28
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/slackiq.rb', line 24

def notify(options={})  
  url = @@webhook_urls[options[:webhook_name]]
  title = options[:title]
  #description = options[:description]
  status = options[:status]
  
  if (bid = options[:bid]) && status.nil?
    raise "Sidekiq::Batch::Status is not defined. Are you sure Sidekiq Pro is set up correctly?" unless defined?(Sidekiq::Batch::Status)
    
    status = Sidekiq::Batch::Status.new(bid)
  end
  
  extra_fields = options.except(:webhook_name, :title, :description, :status)

  fields = []
  
  if status
    created_at = status.created_at
  
    if created_at
      time_now = Time.now
      duration = Slackiq::TimeHelper.elapsed_time_humanized(created_at, time_now)
      time_now_title = (status.complete? ? 'Completed' : 'Now')
    end
  
    total_jobs = status.total
    failures = status.failures
    jobs_run = total_jobs - status.pending

    completion_percentage = (jobs_run/total_jobs.to_f)*100
    failure_percentage = (failures/total_jobs.to_f)*100 if total_jobs && failures

    # round to two decimal places
    decimal_places = 2
    completion_percentage = completion_percentage.round(decimal_places)
    failure_percentage = failure_percentage.round(decimal_places)
    
    description = status.description

    fields += [
                {
                  'title' => 'Created',
                  'value' => Slackiq::TimeHelper.format(created_at),
                  'short' => true
                },
                {
                  'title' => time_now_title,
                  'value' => Slackiq::TimeHelper.format(time_now),
                  'short' => true
                },
                {
                  'title' => "Duration",
                  'value' => duration,
                  'short' => true
                },
                {
                  'title' => "Total Jobs",
                  'value' => total_jobs,
                  '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
                },
              ]
  end
  
  
  
  # add extra fields
  fields += extra_fields.map do |title, value|
    {
      'title' => title,
      'value' => value,
      'short' => false
    }
  end
            
  attachments = 
  [
    {
      'fallback' => title,

      'color' => '#00ff66',

      'title' => title,

      'text' => description,

      'fields' => fields,
    }
]

  body = {attachments: attachments}.to_json
  
  HTTParty.post(url, body: body)
end