Skip to content

Commit

Permalink
Updated code to push towards the web server.
Browse files Browse the repository at this point in the history
  • Loading branch information
dbalatero committed Jun 5, 2009
1 parent 34a6510 commit 42ee7ff
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 4 deletions.
18 changes: 14 additions & 4 deletions lib/queue_stick/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,14 @@ class Runner
class MissingPortError < ArgumentError; end
class NumWorkersError < ArgumentError; end

attr_reader :workers
attr_reader :start_time

def initialize(argv, io_stream = STDOUT)
parse_opts!(argv)
validate_opts!
@io = io_stream
@start_time = Time.now.freeze
end

def run!(worker_klass)
Expand All @@ -29,19 +33,25 @@ def initialize_workers!(worker_klass)

def start_web_server!
@io.puts "Starting a web server on port #{@options.port}..."
Thread.new(@options.port, @workers) do |port, workers|
Thread.new(@options.port, self) do |port, runner|
require 'sinatra/base'
app = Sinatra.new(QueueStick::WebServer) do
set :port, port
set :queue_stick_workers, workers
set :queue_runner, runner
end
app.run!
end
end

def errors
workers.map { |worker|
worker.errors
}.flatten!
end

def start_workers!
@io.puts "Starting up #{@workers.size} workers..."
@threads = @workers.map do |worker|
@io.puts "Starting up #{workers.size} workers..."
@threads = workers.map do |worker|
Thread.new do
worker.run_loop while true
end
Expand Down
104 changes: 104 additions & 0 deletions lib/queue_stick/views/index.erb
Original file line number Diff line number Diff line change
@@ -1,8 +1,112 @@
<html>
<head>
<title>QueueStick Status Page</title>
<link rel="stylesheet" href="/stylesheets/reset-fonts-grids.css" type="text/css" />
<link rel="stylesheet" href="/stylesheets/main.css" type="text/css" />

<script type="text/javascript" src="/js/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$('li.error').click(function (event) {
$('li.error-detail').hide();
$('li#' + this.id + '-detail').slideDown('slow');

event.preventDefault();
});
});
</script>
</head>
<body>
<div id="doc2" class="yui-t7">
<div id="hd" class="divider" role="banner">
<h1>queue_stick</h1>
<div class="yui-g" id="header-container">
<div class="yui-u first">
<em>Job started by <span class="network"><%= @username %></span> <%= @start_time %></em></li>
</div>
<div class="yui-u">
<em>Running on IP <span class="network">TODO</span> / <span class="network"><%= @hostname %></span><br />Port <span class="network">TODO</span></em>
</div>
</div>
</div>
<div id="bd" role="main">
<div class="yui-g padded">
<div class="yui-u first counters">
<h2>Messages processed</h2>
<table id="messages_processed" class="counter">
<thead>
<tr>
<th>Thread</th>
<th>Total</th>
<th>&lt;5 mins</th>
<th>&lt;10 mins</th>
<th>&lt;30 mins</th>
</tr>
</thead>
<tbody>
<tr>
<td class="blue">VideoTranscoder&lt;#0x8389afd&gt;</td>
<td>300</td>
<td>200</td>
<td>100</td>
<td>20</td>
</tr>
<tr>
<td class="blue">VideoTranscoder&lt;#0x8389afc&gt;</td>
<td>345</td>
<td>201</td>
<td>150</td>
<td>25</td>
</tr>
</tbody>
<tfoot>
<tr>
<th class="left"><strong>Totals</strong></td>
<th>645</td>
<th>401</td>
<th>250</td>
<th>45</td>
</tr>
</tfoot>
</table>
</div>
<div class="yui-u">
<h1>Errors (3)</h1>
<ul>
<li class="error" id="error-1">
<span class="klass">ArgumentError</span> |
<span class="time">5 minutes ago</span> |
<span class="desc">Wrong number of arguments (0 for 1...</span>
</li>
<li class="error-detail" id="error-1-detail">
<ul>
<li><strong>Message ID:</strong> 94939002</li>
<li><strong>Timestamp:</strong> June 14, 2009</li>
</ul>
<pre>
ArgumentError: ArgumentError
from (irb):1
</pre>
</li>
<li class="error" id="error-2">
<span class="klass">ArgumentError</span> |
<span class="time">7 minutes ago</span> |
<span class="desc">Wrong number of arguments (0 for 1...</span>
</li>
<li class="error" id="error-3">
<span class="klass">ArgumentError</span> |
<span class="time">9 minutes ago</span> |
<span class="desc">Wrong number of arguments (0 for 2...</span>
</li>
</ul>
</div>
</div>
</div>
<div id="ft" role="contentinfo">
queue_stick is an open-source project of <a href="http://bitwax.cd">bitwax.cd</a>, released under the MIT license<br />
<a href="http://github.com/dbalatero/queue_stick/tree/master">browse the source on GitHub</a>
</div>
</div>

</body>
</html>
5 changes: 5 additions & 0 deletions lib/queue_stick/web_server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@
module QueueStick
class WebServer < Sinatra::Application
set :environment, 'production'
set :static, true
set :public, File.expand_path(File.dirname(__FILE__) + '/../../public')
set :views, File.expand_path(File.dirname(__FILE__) + '/views')

get '/' do
@start_time = options.queue_runner.start_time
@username = ENV['USER']
@hostname = `hostname`.chomp! rescue nil
erb :index
end

Expand Down
1 change: 1 addition & 0 deletions lib/queue_stick/worker.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
module QueueStick
class Worker
# TODO(dbalatero): configurable?
MAX_ERRORS = 50

attr_reader :errors
Expand Down
36 changes: 36 additions & 0 deletions spec/queue_stick/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,42 @@
end
end

describe "start_time" do
it "should be set once and then frozen" do
time = Time.now
Time.should_receive(:now).and_return(time)

runner = QueueStick::Runner.new(@argv, @io_stream)
runner.start_time.should be_frozen
runner.start_time.should == time
end
end

describe "workers" do
it "should respond to workers" do
runner = QueueStick::Runner.new(@argv, @io_stream)
runner.should respond_to(:workers)
end
end

describe "errors" do
it "should return an aggregate of all the errors across all threads" do
worker1 = mock
worker1.should_receive(:errors).and_return([:w1e1, :w1e2, :w1e3])
worker2 = mock
worker2.should_receive(:errors).and_return([:w2e1, :w2e2])

runner = QueueStick::Runner.new(@argv, @io_stream)
runner.should_receive(:workers).and_return([worker1, worker2])
errors = runner.errors

errors.should have(5).things
[:w1e1, :w1e2, :w1e3, :w2e1, :w2e2].each do |error|
errors.should include(error)
end
end
end

describe "run!" do
it "should initialize the workers" do
runner = QueueStick::Runner.new(['--disable-web-server', '--port', '1234'], @io_stream)
Expand Down
7 changes: 7 additions & 0 deletions spec/queue_stick/web_server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,18 @@ def app

describe "GET /" do
before(:all) do
runner = mock
runner.should_receive(:start_time).and_return(Time.now)
app.set :queue_runner, runner
get '/'
end

it "should return 200 OK" do
last_response.should be_ok
end

it "should have the person's username in the template" do
last_response.body.should =~ /Job started by.*#{ENV['USER']}/
end
end
end

0 comments on commit 42ee7ff

Please sign in to comment.