Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Make Redmon a mountable Rack app

  • Loading branch information...
commit 9ac2469ae8d6e0efdb476befb26a963bcd11399a 1 parent 177dc3d
@romanbsd romanbsd authored
View
26 README.md
@@ -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
14 lib/redmon.rb
@@ -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
9 lib/redmon/app.rb
@@ -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
26 lib/redmon/config.rb
@@ -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
6 lib/redmon/helpers.rb
@@ -1,3 +1,5 @@
+require 'redmon/redis'
+
module Redmon
module Helpers
include Redmon::Redis
@@ -7,7 +9,7 @@ def prompt
end
def poll_interval
- Redmon[:poll_interval] * 1000
+ Redmon.config.poll_interval * 1000
end
def count
@@ -15,4 +17,4 @@ def count
end
end
-end
+end
View
10 lib/redmon/redis.rb
@@ -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
4 lib/redmon/worker.rb
@@ -34,8 +34,8 @@ def sort(slowlog)
end
def interval
- Redmon[:poll_interval]
+ Redmon.config.poll_interval
end
end
-end
+end
View
10 spec/helpers_spec.rb
@@ -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
4 spec/worker_spec.rb
@@ -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
Please sign in to comment.
Something went wrong with that request. Please try again.