Permalink
Browse files

Make Redmon a mountable Rack app

  • Loading branch information...
1 parent 177dc3d commit 9ac2469ae8d6e0efdb476befb26a963bcd11399a @romanbsd romanbsd committed Jul 25, 2012
Showing with 82 additions and 27 deletions.
  1. +25 −1 README.md
  2. +4 −10 lib/redmon.rb
  3. +8 −1 lib/redmon/app.rb
  4. +26 −0 lib/redmon/config.rb
  5. +4 −2 lib/redmon/helpers.rb
  6. +6 −4 lib/redmon/redis.rb
  7. +2 −2 lib/redmon/worker.rb
  8. +5 −5 spec/helpers_spec.rb
  9. +2 −2 spec/worker_spec.rb
View
@@ -67,6 +67,30 @@ $ ruby load_sim.rb
Open your browser to 0.0.0.0:4567
+
+## Using in a Rails application
+
+Add to Gemfile:
+
+ gem 'redmon', require: false
+
+Add to config/routes.rb:
+
+ require 'redmon/app'
+ mount Redmon::App => '/redmon'
+
+You can configure the Redmon using an initializer config/initializers/redmon.rb:
+
+```ruby
+Redmon.configure do |config|
+ config.redis_url = 'redis://127.0.0.1:6379'
+ config.namespace = 'redmon'
+ config.poll_interval = 10
+end
+```
+
+This will mount the Redmon application to the /redmon/ path. The trailing slash is important.
+
## License
Copyright (c) 2012 Sean McDaniel
@@ -75,4 +99,4 @@ Permission is hereby granted, free of charge, to any person obtaining a copy of
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
View
@@ -1,3 +1,4 @@
+require 'redmon/config'
require 'active_support/core_ext'
require 'eventmachine'
require 'haml'
@@ -10,16 +11,8 @@ module Redmon
attr_reader :opts
- @opts = {
- :web_interface => ['0.0.0.0', 4567],
- :redis_url => 'redis://127.0.0.1:6379',
- :namespace => 'redmon',
- :worker => true,
- :poll_interval => 10
- }
-
def run(opts={})
- @opts.merge! opts
+ @opts = Redmon::Config::DEFAULTS.merge(opts)
start_em
rescue Exception => e
log "!!! Redmon has shit the bed, restarting... #{e.message}"
@@ -55,6 +48,7 @@ def log(msg)
puts "[#{Time.now.strftime('%y-%m-%d %H:%M:%S')}] #{msg}"
end
+ # @deprecated
def [](option)
opts[option]
end
@@ -64,4 +58,4 @@ def [](option)
require 'redmon/redis'
require 'redmon/helpers'
require 'redmon/app'
-require 'redmon/worker'
+require 'redmon/worker'
View
@@ -1,5 +1,12 @@
+require 'redmon/helpers'
+
module Redmon
class App < Sinatra::Base
+ use Rack::Static, {
+ urls: [/\.css$/, /\.js$/],
+ root: "#{root}/public",
+ cache_control: 'public, max-age=3600'
+ }
helpers Redmon::Helpers
@@ -36,4 +43,4 @@ class App < Sinatra::Base
end
end
-end
+end
View
@@ -0,0 +1,26 @@
+module Redmon
+ def self.configure
+ yield config
+ end
+
+ def self.config
+ @config ||= Config.new
+ end
+
+ class Config
+ DEFAULTS = {
+ :web_interface => ['0.0.0.0', 4567],
+ :redis_url => 'redis://127.0.0.1:6379',
+ :namespace => 'redmon',
+ :worker => true,
+ :poll_interval => 10
+ }
+
+ attr_accessor(*DEFAULTS.keys)
+
+ def initialize
+ DEFAULTS.each { |k,v| send("#{k}=", v) }
+ end
+
+ end
+end
View
@@ -1,3 +1,5 @@
+require 'redmon/redis'
+
module Redmon
module Helpers
include Redmon::Redis
@@ -7,12 +9,12 @@ def prompt
end
def poll_interval
- Redmon[:poll_interval] * 1000
+ Redmon.config.poll_interval * 1000
end
def count
-(params[:count] ? params[:count].to_i : 1)
end
end
-end
+end
View
@@ -1,3 +1,5 @@
+require 'redmon/config'
+
module Redmon
module Redis
extend self
@@ -13,15 +15,15 @@ module Redis
]
def redis
- @redis ||= ::Redis.connect(:url => Redmon[:redis_url])
+ @redis ||= ::Redis.connect(:url => Redmon.config.redis_url)
end
def ns
- Redmon[:namespace]
+ Redmon.config.namespace
end
def redis_url
- @redis_url ||= Redmon[:redis_url].gsub(/\w*:\w*@/, '')
+ @redis_url ||= Redmon.config.redis_url.gsub(/\w*:\w*@/, '')
end
def redis_host
@@ -60,4 +62,4 @@ def stats_key
"#{ns}:redis:#{redis_host}:stats"
end
end
-end
+end
View
@@ -34,8 +34,8 @@ def sort(slowlog)
end
def interval
- Redmon[:poll_interval]
+ Redmon.config.poll_interval
end
end
-end
+end
View
@@ -4,7 +4,7 @@
describe "redis" do
it "should call Redis.connect" do
- Redis.should_receive(:connect).with({:url => Redmon[:redis_url]})
+ Redis.should_receive(:connect).with({:url => Redmon.config.redis_url})
Redmon::Redis.redis
end
end
@@ -19,19 +19,19 @@ def em_redis
describe "#ns" do
it "should return the configured namespace" do
- Redmon[:namespace].should == Redmon::Redis.ns
+ Redmon.config.namespace.should == Redmon::Redis.ns
end
end
describe "#redis_url" do
it "should return the configured redis url" do
- Redmon[:redis_url].should == Redmon::Redis.redis_url
+ Redmon.config.redis_url.should == Redmon::Redis.redis_url
end
end
describe "#redis_host" do
it "should return the configured redis host" do
- Redmon[:redis_url].gsub('redis://', '').should == Redmon::Redis.redis_host
+ Redmon.config.redis_url.gsub('redis://', '').should == Redmon::Redis.redis_host
end
end
@@ -83,4 +83,4 @@ def em_redis
end
end
-end
+end
View
@@ -55,8 +55,8 @@ def mock_timer
describe "#interval" do
it "should return the configured poll interval" do
- @worker.interval.should == Redmon[:poll_interval]
+ @worker.interval.should == Redmon.config.poll_interval
end
end
-end
+end

0 comments on commit 9ac2469

Please sign in to comment.