slackiq 0.0.2

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
+ SHA1:
3
+ metadata.gz: 6ec21526ea00d3e7952686bfa6ebbe6ddda8af07
4
+ data.tar.gz: bde34918c70eae1a2a0734a1bcd72f29cbda8435
5
+ SHA512:
6
+ metadata.gz: c1c09484ce20cf6e6028b16c0cbaff7fa35dbf938bb2942ea3f9912b61fd40aa02c8c7a44d29efa0fbb316203692a010364139f84c6e0c0017ada8fa54a9acb3
7
+ data.tar.gz: b3859bc77ae70aaad00bb161fe934d42272f47bc3b10499e0836f112a54696b983618d68cd6d17c569a005ed3746af3aac5004693d54dceb7efd414add7c9d1b
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
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,77 @@
1
+ # Slackiq
2
+
3
+ Slackiq integrates [Slack](https://slack.com/) and [Sidekiq Pro](http://sidekiq.org/pro/) so that you can have vital information about your Sidekiq jobs sent directly to your team's Slack.
4
+
5
+ ![demo](http://i.imgur.com/4NLq2rP.gif)
6
+
7
+ ## Installation
8
+
9
+ Add this line to your Gemfile:
10
+
11
+ `gem 'slackiq'`
12
+
13
+ ## Configuration
14
+
15
+ First, set up any number of Slack Incoming Webhooks [from your Slack](https://slack.com/services/new/incoming-webhook).
16
+
17
+ 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:
18
+
19
+ ```
20
+ Slackiq.configure( web_scrapes: 'https://hooks.slack.com/services/HA298HF2/ALSKF2451/lknsaHHA2323KKDKND',
21
+ data_processing: 'https://hooks.slack.com/services/HA298HF2/ALSKF2451/H24dLKAHD22423')
22
+ ```
23
+
24
+ ## Usage
25
+
26
+ You can call `notify` to send a nicely-formatted notification to your Slack.
27
+
28
+ The `notify` method has a single Hash parameter. Here are the keys and values in the Hash:
29
+ * `:webhook_name` The name of the webhook (Symbol) that you configured (eg. `:main` or `:data_processing`)
30
+ * `:title` The title of the notification (String)
31
+ * `:status` An instance of `Sidekiq::Batch::Status` (see [this link](https://github.com/mperham/sidekiq/wiki/Batches) for more info)
32
+ * Any other keys and values (both Strings) can be added too, and they'll be added to the Slack notification!
33
+
34
+ Here's an example showing how you would use Slackiq to send a notification to your Slack when your Sidekiq batch completes:
35
+
36
+ ```
37
+ class WebScraper
38
+
39
+ class << self
40
+
41
+ # Scrape the first 100 URLs in the database
42
+ def scrape_100
43
+ batch = Sidekiq::Batch.new
44
+ batch.description = 'Scrape the first 100 URLs!'
45
+ batch.on(:complete, self)
46
+
47
+ batch.jobs do
48
+
49
+ urls = Url.limit(100) # Url is a Rails model in this case
50
+
51
+ urls.each do |url|
52
+ ScraperWorker.perform_async(url.id)
53
+ end
54
+ end
55
+
56
+ end
57
+
58
+ def on_complete(status, options)
59
+ Slackiq.notify(webhook_name: :web_scrapes, status: status, title: 'Scrape Completed!',
60
+ 'Total URLs in DB' => URL.count.to_s,
61
+ 'Servers' => "#{Server.active_count} active, #{Server.inactive_count} inactive")
62
+ end
63
+
64
+ end
65
+ ```
66
+
67
+ Note that in this case, `'Total URLs in DB'` and `'Servers'` are custom fields that will also appear in Slack!
68
+
69
+ ## Contributing
70
+
71
+ 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.
72
+
73
+
74
+ ## License
75
+
76
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
77
+
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/Slackiq-0.0.0.gem ADDED
Binary file
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
@@ -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 = "0.0.2"
3
+ end
data/lib/slackiq.rb ADDED
@@ -0,0 +1,121 @@
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/core_ext' #for Hash#except
10
+
11
+ module Slackiq
12
+
13
+ class << self
14
+
15
+ def configure(webhook_urls={})
16
+ raise 'Argument must be a Hash' unless webhook_urls.class == Hash
17
+ @@webhook_urls = webhook_urls
18
+ end
19
+
20
+ def notify(options={})
21
+ url = @@webhook_urls[options[:webhook_name]]
22
+ title = options[:title]
23
+ #description = options[:description]
24
+ status = options[:status]
25
+ extra_fields = options.except(:webhook_name, :title, :description, :status)
26
+
27
+ if status
28
+ created_at = status.created_at
29
+
30
+ if created_at
31
+ time_now = Time.now
32
+ duration = Slackiq::TimeHelper.elapsed_time_humanized(created_at, time_now)
33
+ time_now_title = (status.complete? ? 'Completed' : 'Now')
34
+ end
35
+
36
+ total_jobs = status.total
37
+ failures = status.failures
38
+ jobs_run = total_jobs - status.pending
39
+
40
+ completion_percentage = (jobs_run/total_jobs.to_f)*100
41
+
42
+ failure_percentage = (failures/total_jobs.to_f)*100 if total_jobs && failures
43
+
44
+ description = status.description
45
+ end
46
+
47
+ fields = [
48
+ {
49
+ "title" => "Created",
50
+ "value" => Slackiq::TimeHelper.format(created_at),
51
+ "short" => true
52
+ },
53
+ {
54
+ "title" => time_now_title,
55
+ "value" => Slackiq::TimeHelper.format(time_now),
56
+ "short" => true
57
+ },
58
+ {
59
+ "title" => "Duration",
60
+ "value" => duration,
61
+ "short" => true
62
+ },
63
+ {
64
+ "title" => "Total Jobs",
65
+ "value" => total_jobs,
66
+ "short" => true
67
+ },
68
+ {
69
+ "title" => "Jobs Run",
70
+ "value" => jobs_run,
71
+ "short" => true
72
+ },
73
+ {
74
+ "title" => "Completion %",
75
+ "value" => "#{completion_percentage}%",
76
+ "short" => true
77
+ },
78
+ {
79
+ "title" => "Failures",
80
+ "value" => status.failures,
81
+ "short" => true
82
+ },
83
+ {
84
+ "title" => "Failure %",
85
+ "value" => "#{failure_percentage}%",
86
+ "short" => true
87
+ },
88
+ ]
89
+
90
+ # add extra fields
91
+ fields += extra_fields.map do |title, value|
92
+ {
93
+ "title" => title,
94
+ "value" => value,
95
+ "short" => false
96
+ }
97
+ end
98
+
99
+ attachments =
100
+ [
101
+ {
102
+ "fallback" => "Sidekiq Batch Completed! (#{description})",
103
+
104
+ 'color' => '#00ff66',
105
+
106
+ 'title' => title,
107
+
108
+ 'text' => description,
109
+
110
+ 'fields' => fields,
111
+ }
112
+ ]
113
+
114
+ body = {attachments: attachments}.to_json
115
+
116
+ HTTParty.post(url, body: body)
117
+ end
118
+
119
+ end
120
+
121
+ end
data/slackiq.gemspec ADDED
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'slackiq/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "slackiq"
8
+ spec.version = Slackiq::VERSION
9
+ spec.authors = ['Jason Lew']
10
+ spec.email = ['jason@mightysignal.com']
11
+ spec.summary = 'MightySignal: Slack and Sidekiq Pro integration'
12
+ spec.description = "Slackiq (by MightySignal) integrates Slack and Sidekiq Pro so that you can have vital information about your Sidekiq jobs sent directly to your team's Slack."
13
+ spec.homepage = 'https://github.com/MightySignal/slackiq'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'httparty'
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,101 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: slackiq
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Jason Lew
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-28 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httparty
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ description: Slackiq (by MightySignal) integrates Slack and Sidekiq Pro so that you
56
+ can have vital information about your Sidekiq jobs sent directly to your team's
57
+ Slack.
58
+ email:
59
+ - jason@mightysignal.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - ".gitignore"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - Slackiq-0.0.0.gem
71
+ - bin/console
72
+ - bin/setup
73
+ - lib/slackiq.rb
74
+ - lib/slackiq/time_helper.rb
75
+ - lib/slackiq/version.rb
76
+ - slackiq.gemspec
77
+ homepage: https://github.com/MightySignal/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
+ rubyforge_project:
97
+ rubygems_version: 2.2.2
98
+ signing_key:
99
+ specification_version: 4
100
+ summary: 'MightySignal: Slack and Sidekiq Pro integration'
101
+ test_files: []