HornsAndHooves-slackiq 1.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 29415d6f7b5d276e2984ca50664017ddb5f9aa595f3cc608bb18eca1389ae9c9
4
+ data.tar.gz: 56df8d55ebcda7d4e4b55acd7229ed82ed0834ff7be7ff6faf5161925e224126
5
+ SHA512:
6
+ metadata.gz: a3f02f4a93667bf141d24dfc221e003381c55116c57576ece093606a9c6697ca38361ecfe04745cc22245b4c2e9559c1c47fe9add671ef53e39dbf73371e4c0b
7
+ data.tar.gz: c807186a0076bb85bdabcb0cb32342da1678680ba23dc7cfd669dea5fc7f44a0c496af2d9cf77c24b702fcb18b1c02febf42c4875798ba3e6ec48a0590237dc5
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in slackiq.gemspec
4
+ gemspec
@@ -0,0 +1,27 @@
1
+ # -*- encoding: utf-8 -*-
2
+
3
+ lib = File.expand_path('../lib', __FILE__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'slackiq/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = "HornsAndHooves-slackiq"
9
+ spec.version = Slackiq::VERSION
10
+ spec.authors = ["HornsAndHooves", "Peter Maneykowski"]
11
+ spec.email = ['maneyko@integracredit.com']
12
+ spec.summary = 'HornsAndHooves: Slack and Sidekiq Pro integration'
13
+ spec.description = "Slackiq (by HornsAndHooves) integrates Slack and Sidekiq so that you can "\
14
+ "have vital information about your Sidekiq jobs sent directly to your team's Slack."
15
+ spec.homepage = 'https://github.com/HornsAndHooves/slackiq'
16
+ spec.license = 'MIT'
17
+
18
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency 'httparty'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Jason Lew
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ # Slackiq
2
+ [![Gem Version](https://badge.fury.io/rb/slackiq.svg)](http://badge.fury.io/rb/slackiq)
3
+
4
+ Slackiq (pronounced *slack-kick*) integrates [Slack](https://slack.com/) and [Sidekiq](http://sidekiq.org) so that you can have vital information about your Sidekiq jobs sent directly to your team's Slack.
5
+
6
+ ![demo](http://i.imgur.com/4NLq2rP.gif)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your Gemfile:
11
+
12
+ ```ruby
13
+ gem 'slackiq'
14
+ ```
15
+
16
+ Then run:
17
+
18
+ ```
19
+ bundle install
20
+ ```
21
+
22
+ ## Configuration
23
+
24
+ First, set up any number of Slack Incoming Webhooks [from your Slack](https://slack.com/services/new/incoming-webhook).
25
+
26
+ Then, you only need to call the `configure` method when your application launches to configure all of the webhooks to which you want to post. If you're using Rails, create an initializer at `config/initializers/slackiq.rb`. Here's an example:
27
+
28
+ ```ruby
29
+ Slackiq.configure( web_scrapes: 'https://hooks.slack.com/services/HA298HF2/ALSKF2451/lknsaHHA2323KKDKND',
30
+ data_processing: 'https://hooks.slack.com/services/HA298HF2/ALSKF2451/H24dLKAHD22423')
31
+ ```
32
+
33
+ `:web_scrapes` and `data_processing` are examples of keys. Use whatever keys you want.
34
+
35
+ ## Usage
36
+
37
+ You can call `notify` to send a nicely-formatted notification to your Slack. You can call `notify`
38
+
39
+ * Inside the Sidekiq Pro `on_success` or `on_complete` callback (not available on regular Sidekiq--only Pro)
40
+ * From inside a Sidekiq worker while it's running, in which case you should pass in the `bid` to the `perform` method of the worker
41
+
42
+ The `notify` method has a single Hash parameter. Here are the keys and values in the Hash:
43
+ * `:webhook_name` The name of the webhook (Symbol) that you configured (eg. `:web_scrapes` or `:data_processing`)
44
+ * `:title` The title of the notification (String)
45
+ * `:status` An instance of `Sidekiq::Batch::Status`
46
+ * Any other keys and values (both Strings) can be added too, and they'll be added to the Slack notification!
47
+
48
+ If you haven't used batches with Sidekiq Pro before, [read this first](https://github.com/mperham/sidekiq/wiki/Batches).
49
+
50
+ Here's an example showing how you would use Slackiq to send a notification to your Slack when your Sidekiq batch completes:
51
+
52
+ ```ruby
53
+ class WebScraper
54
+
55
+ class << self
56
+
57
+ # Scrape the first 100 URLs in the database
58
+ def scrape_100
59
+ batch = Sidekiq::Batch.new
60
+ batch.description = 'Scrape the first 100 URLs!'
61
+ batch.on(:complete, self)
62
+
63
+ batch.jobs do
64
+
65
+ urls = Url.limit(100) # Url is a Rails model in this case
66
+
67
+ urls.each do |url|
68
+ ScraperWorker.perform_async(url.id)
69
+ end
70
+ end
71
+
72
+ end
73
+
74
+ def on_complete(status, options)
75
+ Slackiq.notify(webhook_name: :web_scrapes, status: status, title: 'Scrape Completed!',
76
+ 'Total URLs in DB' => URL.count.to_s,
77
+ 'Servers' => "#{Server.active_count} active, #{Server.inactive_count} inactive")
78
+ end
79
+
80
+ end
81
+ ```
82
+
83
+ Note that in this case, `'Total URLs in DB'` and `'Servers'` are custom fields that will also appear in Slack!
84
+
85
+ ### Want to send a message to Slack that isn't Sidekiq-related?
86
+
87
+ No prob. Just:
88
+
89
+ ```ruby
90
+ Slackiq.message('Server 5 is overloaded!', webhook_name: :data_processing)
91
+ ```
92
+
93
+ ## Contributing
94
+
95
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MightySignal/slackiq. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
96
+
97
+
98
+ ## License
99
+
100
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
101
+
102
+ ## Blog Post about Slackiq
103
+
104
+ https://blog.mightysignal.com/slackiq-a-ruby-gem-that-connects-slack-and-sidekiq-a2308c1974b7
105
+
106
+ ## More Open Source Projects
107
+
108
+ * [Slacktivity](https://github.com/MightySignal/slacktivity)
109
+
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ task :console do
4
+ exec "irb -r slackiq -I ./lib"
5
+ end
6
+
7
+ task c: :console
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "slackiq"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/slackiq.rb ADDED
@@ -0,0 +1,148 @@
1
+ require 'slackiq/version'
2
+
3
+ require 'net/http'
4
+ require 'json'
5
+ require 'httparty'
6
+
7
+ require 'slackiq/time_helper'
8
+
9
+ require 'active_support' # For Hash#except
10
+
11
+ module Slackiq
12
+
13
+ class << self
14
+
15
+ # Configure all of the webhook URLs you're going to use
16
+ # @author Jason Lew
17
+ def configure(webhook_urls={})
18
+ raise 'Argument must be a Hash' unless webhook_urls.class == Hash
19
+ @@webhook_urls = webhook_urls
20
+ end
21
+
22
+ # Send a notification to Slack with Sidekiq info about the batch
23
+ # @author Jason Lew
24
+ def notify(options={})
25
+ url = @@webhook_urls[options[:webhook_name]]
26
+ title = options[:title]
27
+ # description = options[:description]
28
+ color = options[:color] || color_for(status)
29
+ status = options[:status]
30
+
31
+ if (bid = options[:bid]) && status.nil?
32
+ raise "Sidekiq::Batch::Status is not defined. Are you sure Sidekiq Pro is set up correctly?" unless defined?(Sidekiq::Batch::Status)
33
+ status = Sidekiq::Batch::Status.new(bid)
34
+ end
35
+
36
+ extra_fields = options.except(:webhook_name, :title, :description, :status)
37
+
38
+ fields = []
39
+
40
+ if status
41
+ created_at = status.created_at
42
+
43
+ if created_at
44
+ time_now = Time.now
45
+ duration = Slackiq::TimeHelper.elapsed_time_humanized(created_at, time_now)
46
+ time_now_title = (status.complete? ? 'Completed' : 'Now')
47
+ end
48
+
49
+ total_jobs = status.total
50
+ failures = status.failures
51
+ jobs_run = total_jobs - status.pending
52
+
53
+ completion_percentage = (jobs_run/total_jobs.to_f)*100
54
+ failure_percentage = (failures/total_jobs.to_f)*100 if total_jobs && failures
55
+
56
+ # Round to two decimal places
57
+ decimal_places = 2
58
+ completion_percentage = completion_percentage.round(decimal_places)
59
+ failure_percentage = failure_percentage.round(decimal_places)
60
+
61
+ description = status.description
62
+
63
+ fields += [
64
+ {
65
+ 'title' => 'Created',
66
+ 'value' => Slackiq::TimeHelper.format(created_at),
67
+ 'short' => true
68
+ },
69
+ {
70
+ 'title' => time_now_title,
71
+ 'value' => Slackiq::TimeHelper.format(time_now),
72
+ 'short' => true
73
+ },
74
+ {
75
+ 'title' => "Duration",
76
+ 'value' => duration,
77
+ 'short' => true
78
+ },
79
+ {
80
+ 'title' => "Total Jobs",
81
+ 'value' => total_jobs,
82
+ 'short' => true
83
+ },
84
+ {
85
+ 'title' => "Jobs Run",
86
+ 'value' => jobs_run,
87
+ 'short' => true
88
+ },
89
+ {
90
+ 'title' => "Completion %",
91
+ 'value' => "#{completion_percentage}%",
92
+ 'short' => true
93
+ },
94
+ {
95
+ 'title' => "Failures",
96
+ 'value' => status.failures,
97
+ 'short' => true
98
+ },
99
+ {
100
+ 'title' => "Failure %",
101
+ 'value' => "#{failure_percentage}%",
102
+ 'short' => true
103
+ },
104
+ ]
105
+ end
106
+
107
+ # Add extra fields
108
+ fields += extra_fields.map do |title, value|
109
+ {
110
+ 'title' => title,
111
+ 'value' => value,
112
+ 'short' => false
113
+ }
114
+ end
115
+
116
+ attachments = [
117
+ {
118
+ 'fallback' => title,
119
+ 'color' => color,
120
+ 'title' => title,
121
+ 'text' => description,
122
+ 'fields' => fields,
123
+ }
124
+ ]
125
+
126
+ body = { attachments: attachments }.to_json
127
+
128
+ HTTParty.post(url, body: body)
129
+ end
130
+
131
+ # Send a notification without Sidekiq batch info
132
+ # @author Jason Lew
133
+ def message(text, options)
134
+ url = @@webhook_urls[options[:webhook_name]]
135
+ body = { 'text' => text }.to_json
136
+ HTTParty.post(url, body: body)
137
+ end
138
+
139
+ private
140
+ def color_for(status)
141
+ case status
142
+ when :success then '#1C9513'
143
+ when :failure then '#FF0000'
144
+ else '#FBBD08'
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,39 @@
1
+ require 'date'
2
+
3
+ module Slackiq
4
+ module TimeHelper
5
+
6
+ class << self
7
+
8
+ def elapsed_time_humanized(t0, t1)
9
+ humanize(elapsed_seconds(t0, t1))
10
+ end
11
+
12
+ def elapsed_seconds(t0, t1)
13
+ dt0 = t0.to_datetime
14
+ dt1 = t1.to_datetime
15
+ ((dt1-dt0)*24*60*60).to_i
16
+ end
17
+
18
+ # http://stackoverflow.com/questions/4136248/how-to-generate-a-human-readable-time-range-using-ruby-on-rails
19
+ def humanize(secs)
20
+ [[60, :s], [60, :m], [24, :h], [1000, :d]].map{ |count, name|
21
+ if secs > 0
22
+ secs, n = secs.divmod(count)
23
+ "#{n.to_i}#{name}"
24
+ end
25
+ }.compact.reverse.join(' ')
26
+ end
27
+
28
+ def format(time)
29
+ time.strftime('%D @ %r').gsub('PM', 'pm').gsub('AM', 'am')
30
+ end
31
+
32
+
33
+ end
34
+
35
+ end
36
+ end
37
+
38
+
39
+
@@ -0,0 +1,3 @@
1
+ module Slackiq
2
+ VERSION = "1.1.5"
3
+ end
metadata ADDED
@@ -0,0 +1,100 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: HornsAndHooves-slackiq
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.1.5
5
+ platform: ruby
6
+ authors:
7
+ - HornsAndHooves
8
+ - Peter Maneykowski
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2020-03-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: httparty
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: '0'
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: '0'
28
+ - !ruby/object:Gem::Dependency
29
+ name: bundler
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '1.10'
35
+ type: :development
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '1.10'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '10.0'
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '10.0'
56
+ description: Slackiq (by HornsAndHooves) integrates Slack and Sidekiq so that you
57
+ can have vital information about your Sidekiq jobs sent directly to your team's
58
+ Slack.
59
+ email:
60
+ - maneyko@integracredit.com
61
+ executables: []
62
+ extensions: []
63
+ extra_rdoc_files: []
64
+ files:
65
+ - ".gitignore"
66
+ - CODE_OF_CONDUCT.md
67
+ - Gemfile
68
+ - HornsAndHooves-slackiq.gemspec
69
+ - LICENSE.txt
70
+ - README.md
71
+ - Rakefile
72
+ - bin/console
73
+ - bin/setup
74
+ - lib/slackiq.rb
75
+ - lib/slackiq/time_helper.rb
76
+ - lib/slackiq/version.rb
77
+ homepage: https://github.com/HornsAndHooves/slackiq
78
+ licenses:
79
+ - MIT
80
+ metadata: {}
81
+ post_install_message:
82
+ rdoc_options: []
83
+ require_paths:
84
+ - lib
85
+ required_ruby_version: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ required_rubygems_version: !ruby/object:Gem::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: '0'
95
+ requirements: []
96
+ rubygems_version: 3.0.6
97
+ signing_key:
98
+ specification_version: 4
99
+ summary: 'HornsAndHooves: Slack and Sidekiq Pro integration'
100
+ test_files: []