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.0.0"

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

.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
# 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)
  
  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
    
    description = status.description
  end
  
  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
              },
            ]
  
  # add extra fields
  fields += extra_fields.map do |title, value|
    {
      'title' => title,
      'value' => value,
      'short' => false
    }
  end
            
  attachments = 
  [
    {
      'fallback' => "Sidekiq Batch Completed! (#{description})",

      'color' => '#00ff66',

      'title' => title,

      'text' => description,

      'fields' => fields,
    }
]

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

.send(message, options) ⇒ Object

Send a notification without Sidekiq batch info

Author:

  • Jason Lew



132
133
134
135
136
137
138
# File 'lib/slackiq.rb', line 132

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

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