Skip to content

Commit

Permalink
[#2]Add polling on a front-end page so as to update when a worker com…
Browse files Browse the repository at this point in the history
…pletes
  • Loading branch information
rkononov committed Sep 21, 2012
1 parent 4750922 commit 3bf4b42
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 15 deletions.
Binary file added app/assets/images/ajax-loader.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions app/assets/javascripts/custom_examples.js.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,9 @@
# Place all the behaviors and hooks related to the matching controller here. # Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js. # All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
#= require task_updater
root = exports ? this
jQuery ->
if $('#task_list_table').size() > 0
window.setInterval(updateJobStatuses, 10000)

17 changes: 17 additions & 0 deletions app/assets/javascripts/task_updater.js.coffee
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
root = exports ? this
root.updateJobStatuses = ->
task_ids = []
$('#task_list_table').find('tbody tr[data-task-id]').each ->
status = $(this).data('status')
task_ids.push($(this).data('taskId')) if (status == 'running' || status == 'queued')
return true if task_ids.length == 0
for task_id in task_ids
$.get '/tasks/' + task_id + '/status', (json) ->
job_tr = $("tr[data-task-id=#{task_id}]")
job_tr.data('status', json.status)
job_tr.find('.status').html(json.status)
job_tr.find('.duration').html(json.duration)
if json.status != "running" && json.status != "queued"
job_tr.find('.results_link').show()
job_tr.find('.spinner').hide()
null
6 changes: 6 additions & 0 deletions app/assets/javascripts/tasks.js.coffee
Original file line number Original file line Diff line number Diff line change
@@ -1,3 +1,9 @@
# Place all the behaviors and hooks related to the matching controller here. # Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js. # All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ # You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
#= require task_updater
root = exports ? this
jQuery ->
if $('#task_list_table').size() > 0
window.setInterval(updateJobStatuses, 10000)

2 changes: 1 addition & 1 deletion app/controllers/custom_examples_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def deserialize_worker
m.a = params['a'].split(',').map(&:to_i) rescue 1 m.a = params['a'].split(',').map(&:to_i) rescue 1
m.b = params['b'].split(',').map(&:to_i) rescue [1, 2, 3] m.b = params['b'].split(',').map(&:to_i) rescue [1, 2, 3]
client.tasks.create('DeserializeWorker', :complex_model => m.to_json, :iron_config => Rails.application.config.iron_config.to_json) client.tasks.create('DeserializeWorker', :complex_model => m.to_json, :iron_config => Rails.application.config.iron_config.to_json)
redirect_to custom_examples_path redirect_to(custom_examples_path + "#deserialize_worker")
end end


def deserialize_worker_results def deserialize_worker_results
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/tasks_controller.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,4 +34,14 @@ def retry_task
redirect_to tasks_path redirect_to tasks_path
end end


def status
t = client.tasks.get(params[:id])
render json: {
:status => t.status,
:code_name => t.code_name,
:created_at => t.created_at,
:duration => t.duration
}
end

end end
21 changes: 14 additions & 7 deletions app/views/custom_examples/index.html.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ This worker show how to pass and parse different data types to worker
<%= submit_tag("Send to worker!", :class => 'controls btn btn-primary') %> <%= submit_tag("Send to worker!", :class => 'controls btn btn-primary') %>
</div> </div>
<% end %> <% end %>
<h3>Deserialize Worker</h3> <h3 id="deserialize_worker">Deserialize Worker</h3>
This is simple worker that create new custom object, send it to worker and recieve it back with processed data This is simple worker that create new custom object, send it to worker and recieve it back with processed data
<div> <div>
<%= form_tag(deserialize_worker_custom_examples_path, :class => 'form-inline') do %> <%= form_tag(deserialize_worker_custom_examples_path, :class => 'form-inline') do %>
Expand All @@ -54,7 +54,7 @@ This is simple worker that create new custom object, send it to worker and recie
<%= submit_tag("Process values!", :class => 'btn btn-primary') %> <%= submit_tag("Process values!", :class => 'btn btn-primary') %>
<% end %> <% end %>
<% if !@deserialize_tasks.empty? %> <% if !@deserialize_tasks.empty? %>
<table class="table table-bordered table-hover table-condensed"> <table id="task_list_table" class="table table-bordered table-hover table-condensed">
<thead> <thead>
<tr> <tr>
<th>Code Name</th> <th>Code Name</th>
Expand All @@ -66,12 +66,19 @@ This is simple worker that create new custom object, send it to worker and recie
</thead> </thead>


<% @deserialize_tasks.each do |t| %> <% @deserialize_tasks.each do |t| %>
<tr> <tr data-task-id='<%= t.id %>' data-status="<%= t.status %>">
<td><%= t.code_name %></td> <td><%= t.code_name %></td>
<td><%= t.created_at %></td> <td><span class="created_at"><%= t.created_at %></span></td>
<td><%= t.status %></td> <td><span class="status"><%= t.status %></span></td>
<td><%= t.duration %></td> <td><span class="duration"><%= t.duration %></span></td>
<td><%= link_to("Results", deserialize_worker_results_custom_examples_path(:id=>t.id)) if t.status=='complete' %></td> <td>
<div class="results_link" style="display:<%= (t.status=='complete'||t.status=='error') ? "block" : "none" %>;">
<%= link_to("Results", deserialize_worker_results_custom_examples_path(:id => t.id)) %>
</div>
<div class="spinner" style="display:<%= (t.status=='runnint'||t.status=='queued') ? "block" : "none" %>;">
<%= image_tag 'ajax-loader.gif', class: 'task-spinner' %>
</div>
</td>
<% end %> <% end %>
</table> </table>
<% else %> <% else %>
Expand Down
22 changes: 15 additions & 7 deletions app/views/tasks/index.html.erb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


<h3>List of tasks:</h3> <h3>List of tasks:</h3>
<% if !@tasks.empty? %> <% if !@tasks.empty? %>
<table class="table table-bordered table-hover table-condensed"> <table id="task_list_table" class="table table-bordered table-hover table-condensed">
<thead> <thead>
<tr> <tr>
<th>Code Name</th> <th>Code Name</th>
Expand All @@ -39,16 +39,24 @@
</thead> </thead>


<% @tasks.each do |t| %> <% @tasks.each do |t| %>
<tr> <tr data-task-id='<%= t.id %>' data-status="<%= t.status %>">
<td><%= t.code_name %></td> <td><%= t.code_name %></td>
<td><%= t.created_at %></td> <td><span class="created_at"><%= t.created_at %></span></td>
<td><%= t.status %></td> <td><span class="status"><%= t.status %></span></td>
<td><%= t.duration %></td> <td><span class="duration"><%= t.duration %></span></td>
<td><%= link_to "Log", log_task_path(t.id) %></td> <td>
<div class="spinner" style="display:<%= (t.status=='runnint'||t.status=='queued') ? "block" : "none" %>;">
<%= image_tag 'ajax-loader.gif', class: 'task-spinner' %>
</div>
<div class="results_link" style="display:<%= (t.status=='complete'||t.status=='error'||t.status=='timeout') ? "block" : "none" %>;">
<%= link_to "Log", log_task_path(t.id) %>
</div>
</td>
<td> <td>
<%= button_to "Retry", retry_task_task_path(t.id) %> <%= button_to "Retry", retry_task_task_path(t.id) %>
<%= button_to "Cancel", cancel_task_path(t.id) %> <%= button_to "Cancel", cancel_task_path(t.id) %>
<%= button_to "Cancel all", cancel_all_task_path(t.code_id) %></td> <%= button_to "Cancel all", cancel_all_task_path(t.code_id) %>
</td>
</tr> </tr>
<% end %> <% end %>
</table> </table>
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
post 'cancel_all' post 'cancel_all'
post 'retry_task' post 'retry_task'
get 'log' get 'log'
get 'status'
end end
end end
resources :schedules do resources :schedules do
Expand Down

0 comments on commit 3bf4b42

Please sign in to comment.