Skip to content

Commit

Permalink
update to work with latest sinatra + chrome
Browse files Browse the repository at this point in the history
  • Loading branch information
igrigorik committed Jun 3, 2011
1 parent 64a0692 commit dec0afc
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 16 deletions.
8 changes: 4 additions & 4 deletions jobserver.rb
Expand Up @@ -6,7 +6,7 @@
# 2) Client executes javascript map/reduce jobs via JavaScript # 2) Client executes javascript map/reduce jobs via JavaScript
# 3) Client emits (POST) intermediate results back to job server # 3) Client emits (POST) intermediate results back to job server
# 4) Client is redirected to next available job (map or reduce) # 4) Client is redirected to next available job (map or reduce)
# #


require "rubygems" require "rubygems"
require "sinatra" require "sinatra"
Expand All @@ -23,9 +23,9 @@
redirect "/done" redirect "/done"
end end


get "/map/*" do erb :map, :file => params[:splat].first; end get "/map/*" do erb :map, :locals => {:file => params[:splat].first}; end
get "/reduce" do erb :reduce, :data => options.reduce_jobs; end get "/reduce" do erb :reduce, :locals => {:data => options.reduce_jobs}; end
get "/done" do erb :done, :answer => options.result; end get "/done" do erb :done, :locals => {:answer => options.result}; end


post "/emit/:phase" do post "/emit/:phase" do
case params[:phase] case params[:phase]
Expand Down
2 changes: 1 addition & 1 deletion views/done.erb
@@ -1,4 +1,4 @@
<%= options[:answer] %> <%= answer %>


<!-- Another client could be doing the final reduce, poll to see result --> <!-- Another client could be doing the final reduce, poll to see result -->
<meta http-equiv="refresh" content="2" /> <meta http-equiv="refresh" content="2" />
19 changes: 10 additions & 9 deletions views/layout.erb
@@ -1,6 +1,6 @@
<html> <html>
<head> <head>
<script type="text/javascript"> <script type="text/javascript" defer>


function map() { function map() {
var words = document.body.innerHTML.split(/\n|\s/).length; var words = document.body.innerHTML.split(/\n|\s/).length;
Expand All @@ -10,7 +10,7 @@
function reduce() { function reduce() {
var sum = 0; var sum = 0;
var docs = document.body.innerHTML.split(/\n/); var docs = document.body.innerHTML.split(/\n/);
for each (num in docs) { sum+= parseInt(num) > 0 ? parseInt(num) : 0 } for (num in docs) { sum += ((parseInt(num) > 0) ? parseInt(num) : 0) }
emit('finalize', {'sum': sum}); emit('finalize', {'sum': sum});
} }


Expand All @@ -20,21 +20,22 @@
myForm.action = "/emit/"+phase; myForm.action = "/emit/"+phase;


for (var k in data) { for (var k in data) {
var myInput = document.createElement("input") ; var myInput = document.createElement("input");
myInput.setAttribute("name", k) ; myInput.setAttribute("name", k);
myInput.setAttribute("value", data[k]); myInput.setAttribute("value", data[k]);
myForm.appendChild(myInput) ; myForm.appendChild(myInput);
} }


document.body.appendChild(myForm) ; document.body.appendChild(myForm);
myForm.submit() ; myForm.submit();
document.body.removeChild(myForm) ; document.body.removeChild(myForm);
} }


window.onload = function(){ <%= "#{@execute}();" if @execute %> };
</script> </script>
</head> </head>


<body onload="<%= "#{@execute}();" if @execute %>"> <body>
<%= yield %> <%= yield %>
</body> </body>
</html> </html>
2 changes: 1 addition & 1 deletion views/map.erb
@@ -1,2 +1,2 @@
<% @execute = "map" %> <% @execute = "map" %>
<%= File.open(options[:file], "r").readlines.join %> <%= IO.read(file).strip %>
2 changes: 1 addition & 1 deletion views/reduce.erb
@@ -1,2 +1,2 @@
<% @execute = "reduce" %> <% @execute = "reduce" %>
<%= options[:data].join("\n") %> <%= data.join("\n") %>

0 comments on commit dec0afc

Please sign in to comment.