Skip to content

Commit

Permalink
Final cleanups/refactorings for the 1.1.0 Release
Browse files Browse the repository at this point in the history
  • Loading branch information
denniskuczynski committed Nov 4, 2012
1 parent 74096c8 commit 2665f2a
Show file tree
Hide file tree
Showing 12 changed files with 126 additions and 99 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
source "http://rubygems.org"

#gem 'beaneater', :git => 'https://github.com/beanstalkd/beaneater.git'

# Specify your gem's dependencies in beanstalkd_view.gemspec
gemspec

2 changes: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ PATH
remote: .
specs:
beanstalkd_view (1.1.0)
beaneater (>= 0.1.0)
beaneater (~> 0.1.0)
json
sinatra (>= 1.3.0)
sinatra-assetpack (>= 0.0.11)
Expand Down
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Use the following environment variable to specify the location of the beanstalk
ENV['BEANSTALK_URL'] = 'beanstalk://localhost/'
```

(This can be a comma separated list.)
This can be a comma separated list, e.g. 'beanstalk://localhost:11300,beanstalk://localhost:11400'

Embedding in a Rails app
------------------------
Expand Down Expand Up @@ -62,7 +62,10 @@ bundle exec beanstalkd_view

(This will use the vegas gem to launch the Sinatra app on an available port.)

Alternatively, a Rackup file is provided. To use: cd into the beanstalkd_view directory and execute the rackup command.
Alternatively, a Rackup file is provided. To use: cd into the beanstalkd_view directory and execute:

rackup


Screenshot
------------------------
Expand All @@ -73,8 +76,7 @@ Running the tests
There are 3 variants of RSpec tests.
* Without beanstalkd running, just execute: rspec spec
* Without 1 instance of beanstalkd running (default port), execute: rspec spec --tag requires_beanstalkd
* Without 2 instances of beanstalkd running (ports 12300 and 12400), execute: rspec spec --tag requires_two_beanstalkd
(Also, you must uncomment the modified BEANSTALK_URL setting in spec_helper.rb for requires_two_beanstalk tests.)
* Without 2 instances of beanstalkd running (ports 11300 and 11400), execute: rspec spec --tag requires_two_beanstalkd

License
------------------------
Expand Down
10 changes: 6 additions & 4 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,12 @@ namespace :beanstalkd_view do
while true
tube_name = TEST_QUEUES.sample
begin
tube = beanstalk.tubes[tube_name]
job = tube.reserve
puts "Pulled Job #{job} from #{tube_name}"
job.delete
beanstalk.tubes.watch!(tube_name)
job = beanstalk.tubes.reserve(1)
if job
puts "Pulled Job #{job} from #{tube_name}"
job.delete
end
rescue Exception => ex
puts "Exception while pulling job from #{tube_name}: #{ex}"
end
Expand Down
2 changes: 1 addition & 1 deletion beanstalkd_view.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Gem::Specification.new do |s|
s.add_dependency "sinatra", ">= 1.3.0"
s.add_dependency "sinatra-contrib", ">= 1.3.0"
s.add_dependency "sinatra-assetpack", ">= 0.0.11"
s.add_dependency "beaneater", ">= 0.1.0"
s.add_dependency "beaneater", "~> 0.1.0"
s.add_dependency "vegas", "~> 0.1.2"
s.add_dependency "json"

Expand Down
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$LOAD_PATH.unshift File.expand_path(File.dirname(__FILE__) + '/../lib')

ENV['BEANSTALK_URL'] ||= 'beanstalk://localhost/'
#ENV['BEANSTALK_URL'] = 'beanstalk://localhost:12300,beanstalk://localhost:12400'
#ENV['BEANSTALK_URL'] ||= 'beanstalk://localhost:11300,beanstalk://localhost:11400'

# config.ru
require 'beanstalkd_view'
Expand Down
4 changes: 0 additions & 4 deletions lib/beanstalkd_view/Gemfile

This file was deleted.

1 change: 0 additions & 1 deletion lib/beanstalkd_view/Rakefile

This file was deleted.

66 changes: 64 additions & 2 deletions lib/beanstalkd_view/beanstalkd_utils.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ module BeanstalkdView

module BeanstalkdUtils

GUESS_PEEK_RANGE = 100 # Default number of elements to use in peek-range guesses

class BadURL < RuntimeError; end

def beanstalk
Expand All @@ -13,8 +15,6 @@ def beanstalk_url
ENV['BEANSTALK_URL'] || 'beanstalk://localhost/'
end

class BadURL < RuntimeError; end

def beanstalk_addresses
uris = beanstalk_url.split(/[\s,]+/)
uris.map {|uri| beanstalk_host_and_port(uri)}
Expand All @@ -25,5 +25,67 @@ def beanstalk_host_and_port(uri_string)
raise(BadURL, uri_string) if uri.scheme != 'beanstalk'
"#{uri.host}:#{uri.port || 11300}"
end

# Convert Beaneater::Job to hash
def job_to_hash(job)
ret_value = {}
job_stats = job.stats
job_stats.keys.each { |key| ret_value[key] = job_stats[key] }
ret_value['body'] = job.body.inspect
ret_value
end

# Return the stats data in a format for the Bluff JS UI Charts
def get_chart_data_hash(tubes)
chart_data = {}
chart_data["total_jobs_data"] = Hash.new
chart_data["buried_jobs_data"] = Hash.new
chart_data["total_jobs_data"]["items"] = Array.new
chart_data["buried_jobs_data"]["items"] = Array.new
tubes.each do |tube|
stats = tube.stats
#total_jobs
total_jobs = stats[:total_jobs]
if total_jobs > 0
total_datum = {}
total_datum["label"] = tube.name
total_datum["data"] = total_jobs
chart_data["total_jobs_data"]["items"] << total_datum
end
#buried_jobs
buried_jobs = stats[:current_jobs_buried]
if buried_jobs > 0
buried_datum = {}
buried_datum["label"] = tube.name
buried_datum["data"] = buried_jobs
chart_data["buried_jobs_data"]["items"] << buried_datum
end
end
chart_data
end

# Pick a Minimum Peek Range Based on minumum ready jobs on all tubes
def guess_min_peek_range(tubes)
min = 0
tubes.each do |tube|
response = tube.peek('ready')
if response
if min == 0
min = response.id.to_i
else
min = [min, response.id.to_i].min
end
end
end
# Add some jitter in the opposite direction of 1/4 range
jitter_min = (min-(GUESS_PEEK_RANGE*0.25)).to_i
[1, jitter_min].max
end

# Pick a Minimum Peek Range Based on the minimum
def guess_max_peek_range(min)
(min+GUESS_PEEK_RANGE)-1
end

end
end
91 changes: 16 additions & 75 deletions lib/beanstalkd_view/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ class Server < Sinatra::Base
'/css/vendor/bootstrap.min.css',
'/css/app.css']
end

GUESS_PEEK_RANGE = 100 # Default number of elements to use in peek-range guesses


get "/" do
begin
@connections = beanstalk.connections
@tubes = beanstalk.tubes.all
@stats = beanstalk.stats
chart_data = get_chart_data_hash(@tubes)
@total_jobs_data = chart_data["total_jobs_data"]
@buried_jobs_data = chart_data["buried_jobs_data"] if chart_data["buried_jobs_data"]["items"].size > 0
erb :index
rescue Beaneater::NotConnected => @error
rescue Beaneater::NotConnected, Beaneater::NotFoundError => @error
erb :error
end
end
Expand All @@ -59,7 +58,7 @@ class Server < Sinatra::Base
cookies[:beanstalkd_view_notice] = "Error adding job"
redirect url("/")
end
rescue Beaneater::NotConnected => @error
rescue Beaneater::NotConnected, Beaneater::NotFoundError => @error
erb :error
end
end
Expand All @@ -69,7 +68,7 @@ class Server < Sinatra::Base
@tube = beanstalk.tubes[params[:tube]]
@stats = @tube.stats
erb :tube_stats
rescue Beaneater::NotConnected => @error
rescue Beaneater::NotConnected, Beaneater::NotFoundError => @error
erb :error
end
end
Expand All @@ -84,7 +83,7 @@ class Server < Sinatra::Base
else
{ :error => "No job was found, or an error occurred while trying to peek at the next job."}.to_json
end
rescue Beaneater::NotConnected => @error
rescue Beaneater::NotConnected, Beaneater::NotFoundError => @error
{ :error => @error.to_s }.to_json
end
end
Expand All @@ -111,14 +110,16 @@ class Server < Sinatra::Base
@jobs = []
for i in min..max
begin
job = beanstalk.jobs.find(i)
@jobs << job_to_hash(job) if job
jobs = beanstalk.jobs.find_all(i)
jobs.each do |job|
@jobs << job_to_hash(job)
end
rescue Beaneater::NotFoundError => e
# Since we're looping over potentially non-existant jobs, ignore
end
end
erb :peek_range
rescue Beaneater::NotConnected => @error
rescue Beaneater::NotConnected, Beaneater::NotFoundError => @error
erb :error
end
end
Expand All @@ -135,7 +136,7 @@ class Server < Sinatra::Base
cookies[:beanstalkd_view_notice] = "Error deleting Job #{params[:job_id]}"
redirect url("/tube/#{params[:tube]}")
end
rescue Beaneater::NotConnected => @error
rescue Beaneater::NotConnected, Beaneater::NotFoundError => @error
erb :error
end
end
Expand All @@ -151,7 +152,7 @@ class Server < Sinatra::Base
cookies[:beanstalkd_view_notice] = "Error pausing #{params[:tube]}."
redirect url("/tube/#{params[:tube]}")
end
rescue Beaneater::NotConnected => @error
rescue Beaneater::NotConnected, Beaneater::NotFoundError => @error
erb :error
end
end
Expand All @@ -168,7 +169,7 @@ class Server < Sinatra::Base
cookies[:beanstalkd_view_notice] = "Error kicking #{params[:tube]}."
redirect url("/tube/#{params[:tube]}")
end
rescue Beaneater::NotConnected => @error
rescue Beaneater::NotConnected, Beaneater::NotFoundError => @error
erb :error
end
end
Expand All @@ -178,6 +179,8 @@ def url_path(*path_parts)
end
alias_method :u, :url_path

private

def path_prefix
request.env['SCRIPT_NAME']
end
Expand All @@ -187,68 +190,6 @@ def notice_message
cookies[:beanstalkd_view_notice] = ''
message
end

private

def job_to_hash(job)
ret_value = {}
job_stats = job.stats
job_stats.keys.each { |key| ret_value[key] = job_stats[key] }
ret_value['body'] = job.body.inspect
ret_value
end

# Return the stats data in a format for the Bluff JS UI Charts
def get_chart_data_hash(tubes)
chart_data = {}
chart_data["total_jobs_data"] = Hash.new
chart_data["buried_jobs_data"] = Hash.new
chart_data["total_jobs_data"]["items"] = Array.new
chart_data["buried_jobs_data"]["items"] = Array.new
tubes.each do |tube|
stats = tube.stats
#total_jobs
total_jobs = stats[:total_jobs]
if total_jobs > 0
total_datum = {}
total_datum["label"] = tube.name
total_datum["data"] = total_jobs
chart_data["total_jobs_data"]["items"] << total_datum
end
#buried_jobs
buried_jobs = stats[:current_jobs_buried]
if buried_jobs > 0
buried_datum = {}
buried_datum["label"] = tube.name
buried_datum["data"] = buried_jobs
chart_data["buried_jobs_data"]["items"] << buried_datum
end
end
chart_data
end

# Pick a Minimum Peek Range Based on calls to peek_ready
def guess_min_peek_range(tubes)
min = 0
tubes.each do |tube|
response = tube.peek('ready')
if response
if min == 0
min = response.id.to_i
else
min = [min, response.id.to_i].min
end
end
end
# Add some jitter in the opposite direction of 1/4 range
jitter_min = (min-(GUESS_PEEK_RANGE*0.25)).to_i
[1, jitter_min].max
end

# Pick a Minimum Peek Range Based on the minimum
def guess_max_peek_range(min)
(min+GUESS_PEEK_RANGE)-1
end

end
end
21 changes: 18 additions & 3 deletions lib/beanstalkd_view/views/index.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,34 @@
<div class="span6">
<h1>Overview</h1>

<table class="table">
<thead>
<tr>
<th>Connections</th>
</tr>
</thead>
<tbody>
<% @connections.each do |connection| %>
<tr>
<td><%= connection.address %></a></td>
</tr>
<% end %>
</tbody>
</table>

<table class="table">
<thead>
<tr>
<th>Tubes</th>
</tr>
</thead>
<tbody>
<% @tubes.each do |tube| %>
<% @tubes.each do |tube| %>
<tr>
<td><a href="<%= u("/tube/#{tube.name}") %>"><%= tube.name %></a></td>
</tr>
<% end %>
</tbody>
<% end %>
</tbody>
</table>

<table class="table">
Expand Down
Loading

0 comments on commit 2665f2a

Please sign in to comment.