proby_notifier 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.swp
2
+ pkg/*
3
+ .yardoc
4
+ doc
5
+ rdoc
6
+
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source :rubygems
2
+
3
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,25 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proby_notifier (1.0.0)
5
+
6
+ GEM
7
+ remote: http://rubygems.org/
8
+ specs:
9
+ bluecloth (2.1.0)
10
+ fakeweb (1.3.0)
11
+ rake (0.8.7)
12
+ shoulda (2.11.3)
13
+ yard (0.6.8)
14
+
15
+ PLATFORMS
16
+ ruby
17
+
18
+ DEPENDENCIES
19
+ bluecloth (~> 2.1.0)
20
+ bundler (~> 1.0.0)
21
+ fakeweb (~> 1.3.0)
22
+ proby_notifier!
23
+ rake (~> 0.8.7)
24
+ shoulda (~> 2.11.3)
25
+ yard (~> 0.6.4)
data/README.md ADDED
@@ -0,0 +1,53 @@
1
+ # ProbyNotifier
2
+ A simple library for working with the Proby task monitoring application.
3
+
4
+
5
+ Setup
6
+ -----
7
+ Before notifications can be sent, you must tell ProbyNotifier your API key. This only needs to be done once,
8
+ and should ideally be done inside your apps initialization code.
9
+
10
+ ProbyNotifier.api_key = "b4fe1200c105012efde3482a1411a947"
11
+
12
+ In addition, you can optionally give ProbyNotifier a logger to use.
13
+
14
+ ProbyNotifier.logger = Rails.logger
15
+
16
+
17
+ Sending Notifications
18
+ ---------------------
19
+ To send a start notification
20
+
21
+ ProbyNotifier.send_start_notification(task_api_id)
22
+
23
+ To send a finish notification
24
+
25
+ ProbyNotifier.send_finish_notification(task_api_id)
26
+
27
+ Specifying the `task_api_id` when calling the notification methods is optional. If it is not provided,
28
+ ProbyNotifier will use the value of the `PROBY_TASK_ID` environment variable. If no task id is specified
29
+ in the method call, and no value is set in the `PROBY_TASK_ID` environment variable, then no notification
30
+ will be sent.
31
+
32
+
33
+ The Resque Plugin
34
+ -----------------
35
+ The Resque plugin will automatically send start and finish notifications to Proby when your job
36
+ starts and finishes. Simply `extend ProbyNotifier::ResquePlugin` in your Resque job. The task id
37
+ can either be pulled from the `PROBY_TASK_ID` environment variable, or specified in the job itself
38
+ by setting the `@proby_id` attribute to the task id.
39
+
40
+ class SomeJob
41
+ extend ProbyNotifier::ResquePlugin
42
+ @proby_id = 'abc123' # Or simply let it use the value in the PROBY_TASK_ID environment variable
43
+
44
+ self.perform
45
+ do_stuff
46
+ end
47
+ end
48
+
49
+
50
+ API Doc
51
+ -------
52
+ [http://rdoc.info/github/signal/proby_notifier/master/frames](http://rdoc.info/github/signal/proby_notifier/master/frames)
53
+
data/Rakefile ADDED
@@ -0,0 +1,38 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ begin
7
+ Bundler.setup(:default, :development)
8
+ rescue Bundler::BundlerError => e
9
+ $stderr.puts e.message
10
+ $stderr.puts "Run `bundle install` to install missing gems"
11
+ exit e.status_code
12
+ end
13
+
14
+ require 'rake'
15
+ require 'rake/testtask'
16
+ require 'yard'
17
+
18
+ task :default => :test
19
+
20
+ Rake::TestTask.new(:test) do |test|
21
+ test.libs << 'lib' << 'test'
22
+ test.pattern = ENV['TEST'] || "test/**/*_test.rb"
23
+ test.verbose = true
24
+ end
25
+
26
+ YARD::Rake::YardocTask.new do |t|
27
+ t.files = ['lib/**/*.rb']
28
+ end
29
+
30
+ desc 'Delete yard, and other generated files'
31
+ task :clobber => [:clobber_yard]
32
+
33
+ desc 'Delete yard generated files'
34
+ task :clobber_yard do
35
+ puts 'rm -rf doc .yardoc'
36
+ FileUtils.rm_rf ['doc', '.yardoc']
37
+ end
38
+
@@ -0,0 +1,88 @@
1
+ require 'net/https'
2
+ require 'logger'
3
+
4
+ module ProbyNotifier
5
+
6
+ BASE_URL = "https://proby.signalhq.com/tasks/"
7
+
8
+ # Deliver start and finish notifications to Proby.
9
+ class << self
10
+
11
+ # Set your Proby API key.
12
+ #
13
+ # @param [String] api_key Your Proby API key
14
+ #
15
+ # @example
16
+ # ProbyNotifier.api_key = '1234567890abcdefg'
17
+ def api_key=(api_key)
18
+ @api_key = api_key
19
+ end
20
+
21
+ # Set the logger to be used by Proby.
22
+ #
23
+ # @param [Logger] logger The logger you would like ProbyNotifier to use
24
+ #
25
+ # @example
26
+ # ProbyNotifier.logger = Rails.logger
27
+ # ProbyNotifier.logger = Logger.new(STDERR)
28
+ def logger=(logger)
29
+ @logger = logger
30
+ end
31
+
32
+ # Get the logger used by Proby.
33
+ def logger
34
+ @logger || Logger.new("/dev/null")
35
+ end
36
+
37
+ # Send a start notification for this task to Proby.
38
+ #
39
+ # @param [String] proby_task_id The id of the task to be notified. If nil, the
40
+ # value of the +PROBY_TASK_ID+ environment variable will be used.
41
+ def send_start_notification(proby_task_id=nil)
42
+ send_notification('/start', proby_task_id)
43
+ end
44
+
45
+ # Send a finish notification for this task to Proby
46
+ #
47
+ # @param [String] proby_task_id The id of the task to be notified. If nil, the
48
+ # value of the +PROBY_TASK_ID+ environment variable will be used.
49
+ def send_finish_notification(proby_task_id=nil)
50
+ send_notification('/finish', proby_task_id)
51
+ end
52
+
53
+ private
54
+
55
+ def send_notification(type, proby_task_id)
56
+ if @api_key.nil?
57
+ logger.warn "ProbyNotifier: No notification sent because API key is not set"
58
+ return nil
59
+ end
60
+
61
+ proby_task_id = ENV['PROBY_TASK_ID'] if is_blank?(proby_task_id)
62
+ if is_blank?(proby_task_id)
63
+ logger.warn "ProbyNotifier: No notification sent because task ID was not specified"
64
+ return nil
65
+ end
66
+
67
+ url = BASE_URL + proby_task_id + type
68
+ uri = URI.parse(url)
69
+ req = Net::HTTP::Post.new(uri.path, {'api_key' => @api_key})
70
+
71
+ http = Net::HTTP.new(uri.host, uri.port)
72
+ http.open_timeout = 3
73
+ http.read_timeout = 3
74
+ http.use_ssl = true
75
+ res = http.start { |h| h.request(req) }
76
+ return res.code.to_i
77
+ rescue Exception => e
78
+ logger.error "ProbyNotifier: Proby notification failed: #{e.message}"
79
+ logger.error e.backtrace
80
+ end
81
+
82
+ def is_blank?(string)
83
+ string.nil? || string.strip == ''
84
+ end
85
+ end
86
+
87
+ end
88
+
@@ -0,0 +1,58 @@
1
+ module ProbyNotifier
2
+ # Automatically notifies Proby when this job starts and finishes.
3
+ #
4
+ # class SomeJob
5
+ # extend ProbyNotifier::ResquePlugin
6
+ #
7
+ # self.perform
8
+ # do_stuff
9
+ # end
10
+ # end
11
+ #
12
+ # The Proby Task ID can be set in one of two ways. The most common way is to
13
+ # put the ID in an ENV variable that is set in your crontab. This ID will be
14
+ # transparently passed to the Resque job via Redis.
15
+ #
16
+ # 0 0 * * * PROBY_TASK_ID=abc123 ./queue_some_job
17
+ #
18
+ # Alternatively, if you're not using cron and therefore don't want that
19
+ # support, you can just set the @proby_id ivar in the class, like so.
20
+ #
21
+ # class SomeJob
22
+ # extend ProbyNotifier::ResquePlugin
23
+ # @proby_id = 'abc123'
24
+ #
25
+ # self.perform
26
+ # do_stuff
27
+ # end
28
+ # end
29
+ #
30
+ # Setting the @proby_id variable will take precendence over the ENV variable.
31
+ #
32
+ module ResquePlugin
33
+ def proby_id_bucket(*args)
34
+ "proby_id:#{name}-#{args.to_s}"
35
+ end
36
+
37
+ def before_enqueue_proby(*args)
38
+ return true if @proby_id
39
+
40
+ env_proby_id = ENV['PROBY_TASK_ID']
41
+ Resque.redis.setex(proby_id_bucket(*args), 24.hours, env_proby_id)
42
+ return true
43
+ end
44
+
45
+ def proby_id(*args)
46
+ @proby_id || Resque.redis.get(proby_id_bucket(*args))
47
+ end
48
+
49
+ def around_perform_proby(*args)
50
+ _proby_id = proby_id(*args)
51
+ ProbyNotifier.send_start_notification(_proby_id)
52
+ yield
53
+ ensure
54
+ ProbyNotifier.send_finish_notification(_proby_id)
55
+ end
56
+ end
57
+ end
58
+
@@ -0,0 +1,3 @@
1
+ module ProbyNotifier
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ require 'proby_notifier/proby_notifier'
2
+ require 'proby_notifier/resque_plugin'
@@ -0,0 +1,30 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/proby_notifier/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "proby_notifier"
6
+ s.license = "MIT"
7
+ s.version = ProbyNotifier::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["John Wood", "Doug Barth"]
10
+ s.email = ["john@signalhq.com", "doug@signalhq.com"]
11
+ s.homepage = "http://github.com/signal/proby_notifier"
12
+ s.summary = %Q{A simple library for working with the Proby task monitoring application.}
13
+ s.description = %Q{A simple library for working with the Proby task monitoring application.}
14
+
15
+ s.rubyforge_project = "proby_notifier"
16
+
17
+ s.required_rubygems_version = ">= 1.3.6"
18
+
19
+ s.add_development_dependency "bundler", "~> 1.0.0"
20
+ s.add_development_dependency "rake", "~> 0.8.7"
21
+ s.add_development_dependency "yard", "~> 0.6.4"
22
+ s.add_development_dependency "bluecloth", "~> 2.1.0"
23
+ s.add_development_dependency "fakeweb", "~> 1.3.0"
24
+ s.add_development_dependency "shoulda", "~> 2.11.3"
25
+
26
+ s.files = `git ls-files`.split("\n")
27
+ s.executables = `git ls-files`.split("\n").map{|f| f =~ /^bin\/(.*)/ ? $1 : nil}.compact
28
+ s.require_path = 'lib'
29
+ end
30
+
@@ -0,0 +1,50 @@
1
+ require 'test_helper'
2
+
3
+ class ProbyNotifierTest < Test::Unit::TestCase
4
+
5
+ def setup
6
+ ENV['PROBY_TASK_ID'] = nil
7
+ FakeWeb.clean_registry
8
+ end
9
+
10
+ should "not send the notification if the api_key is not set" do
11
+ assert_nil ProbyNotifier.send_start_notification("abc123")
12
+ end
13
+
14
+ context "with an api key set" do
15
+ setup do
16
+ ProbyNotifier.api_key = '1234567890abcdefg'
17
+ end
18
+
19
+ should "not send the notification if a task id was not specified" do
20
+ assert_nil ProbyNotifier.send_start_notification
21
+ end
22
+
23
+ should "not send the notification if a task id is blank" do
24
+ assert_nil ProbyNotifier.send_start_notification(" ")
25
+ end
26
+
27
+ should "send a start notification if a task_id is specified in the call" do
28
+ FakeWeb.register_uri(:post, "https://proby.signalhq.com/tasks/abc123xyz456/start", :status => ["200", "Success"])
29
+ assert_equal 200, ProbyNotifier.send_start_notification("abc123xyz456")
30
+ end
31
+
32
+ should "send a start notification if a task_id is specified in an environment variable" do
33
+ ENV['PROBY_TASK_ID'] = "uuu777sss999"
34
+ FakeWeb.register_uri(:post, "https://proby.signalhq.com/tasks/uuu777sss999/start", :status => ["200", "Success"])
35
+ assert_equal 200, ProbyNotifier.send_start_notification
36
+ end
37
+
38
+ should "send a finish notification if a task_id is specified in the call" do
39
+ FakeWeb.register_uri(:post, "https://proby.signalhq.com/tasks/abc123xyz456/finish", :status => ["200", "Success"])
40
+ assert_equal 200, ProbyNotifier.send_finish_notification("abc123xyz456")
41
+ end
42
+
43
+ should "send a finish notification if a task_id is specified in an environment variable" do
44
+ ENV['PROBY_TASK_ID'] = "iii999ooo222"
45
+ FakeWeb.register_uri(:post, "https://proby.signalhq.com/tasks/iii999ooo222/finish", :status => ["200", "Success"])
46
+ assert_equal 200, ProbyNotifier.send_finish_notification
47
+ end
48
+ end
49
+
50
+ end
@@ -0,0 +1,16 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+
11
+ require 'test/unit'
12
+ require 'fakeweb'
13
+ require 'shoulda'
14
+
15
+ require 'proby_notifier'
16
+
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: proby_notifier
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease:
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - John Wood
14
+ - Doug Barth
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2011-09-17 00:00:00 -05:00
20
+ default_executable:
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ~>
27
+ - !ruby/object:Gem::Version
28
+ hash: 23
29
+ segments:
30
+ - 1
31
+ - 0
32
+ - 0
33
+ version: 1.0.0
34
+ name: bundler
35
+ type: :development
36
+ prerelease: false
37
+ requirement: *id001
38
+ - !ruby/object:Gem::Dependency
39
+ version_requirements: &id002 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ hash: 49
45
+ segments:
46
+ - 0
47
+ - 8
48
+ - 7
49
+ version: 0.8.7
50
+ name: rake
51
+ type: :development
52
+ prerelease: false
53
+ requirement: *id002
54
+ - !ruby/object:Gem::Dependency
55
+ version_requirements: &id003 !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ~>
59
+ - !ruby/object:Gem::Version
60
+ hash: 15
61
+ segments:
62
+ - 0
63
+ - 6
64
+ - 4
65
+ version: 0.6.4
66
+ name: yard
67
+ type: :development
68
+ prerelease: false
69
+ requirement: *id003
70
+ - !ruby/object:Gem::Dependency
71
+ version_requirements: &id004 !ruby/object:Gem::Requirement
72
+ none: false
73
+ requirements:
74
+ - - ~>
75
+ - !ruby/object:Gem::Version
76
+ hash: 11
77
+ segments:
78
+ - 2
79
+ - 1
80
+ - 0
81
+ version: 2.1.0
82
+ name: bluecloth
83
+ type: :development
84
+ prerelease: false
85
+ requirement: *id004
86
+ - !ruby/object:Gem::Dependency
87
+ version_requirements: &id005 !ruby/object:Gem::Requirement
88
+ none: false
89
+ requirements:
90
+ - - ~>
91
+ - !ruby/object:Gem::Version
92
+ hash: 27
93
+ segments:
94
+ - 1
95
+ - 3
96
+ - 0
97
+ version: 1.3.0
98
+ name: fakeweb
99
+ type: :development
100
+ prerelease: false
101
+ requirement: *id005
102
+ - !ruby/object:Gem::Dependency
103
+ version_requirements: &id006 !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ~>
107
+ - !ruby/object:Gem::Version
108
+ hash: 37
109
+ segments:
110
+ - 2
111
+ - 11
112
+ - 3
113
+ version: 2.11.3
114
+ name: shoulda
115
+ type: :development
116
+ prerelease: false
117
+ requirement: *id006
118
+ description: A simple library for working with the Proby task monitoring application.
119
+ email:
120
+ - john@signalhq.com
121
+ - doug@signalhq.com
122
+ executables: []
123
+
124
+ extensions: []
125
+
126
+ extra_rdoc_files: []
127
+
128
+ files:
129
+ - .gitignore
130
+ - Gemfile
131
+ - Gemfile.lock
132
+ - README.md
133
+ - Rakefile
134
+ - lib/proby_notifier.rb
135
+ - lib/proby_notifier/proby_notifier.rb
136
+ - lib/proby_notifier/resque_plugin.rb
137
+ - lib/proby_notifier/version.rb
138
+ - proby_notifier.gemspec
139
+ - test/proby_notifier_test.rb
140
+ - test/test_helper.rb
141
+ has_rdoc: true
142
+ homepage: http://github.com/signal/proby_notifier
143
+ licenses:
144
+ - MIT
145
+ post_install_message:
146
+ rdoc_options: []
147
+
148
+ require_paths:
149
+ - lib
150
+ required_ruby_version: !ruby/object:Gem::Requirement
151
+ none: false
152
+ requirements:
153
+ - - ">="
154
+ - !ruby/object:Gem::Version
155
+ hash: 3
156
+ segments:
157
+ - 0
158
+ version: "0"
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ none: false
161
+ requirements:
162
+ - - ">="
163
+ - !ruby/object:Gem::Version
164
+ hash: 23
165
+ segments:
166
+ - 1
167
+ - 3
168
+ - 6
169
+ version: 1.3.6
170
+ requirements: []
171
+
172
+ rubyforge_project: proby_notifier
173
+ rubygems_version: 1.4.2
174
+ signing_key:
175
+ specification_version: 3
176
+ summary: A simple library for working with the Proby task monitoring application.
177
+ test_files: []
178
+