Permalink
Browse files

[#2]Add polling on a front-end page so as to update when a worker com…

…pletes
  • Loading branch information...
rkononov committed Sep 21, 2012
1 parent 4750922 commit 3bf4b42c034c396813c231cfb85fc69df7a2f6dc
Binary file not shown.
@@ -1,3 +1,9 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# 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)
+
@@ -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
@@ -1,3 +1,9 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# 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)
+
@@ -29,7 +29,7 @@ def deserialize_worker
m.a = params['a'].split(',').map(&:to_i) rescue 1
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)
- redirect_to custom_examples_path
+ redirect_to(custom_examples_path + "#deserialize_worker")
end
def deserialize_worker_results
@@ -34,4 +34,14 @@ def retry_task
redirect_to tasks_path
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
@@ -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') %>
</div>
<% 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
<div>
<%= form_tag(deserialize_worker_custom_examples_path, :class => 'form-inline') do %>
@@ -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') %>
<% end %>
<% 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>
<tr>
<th>Code Name</th>
@@ -66,12 +66,19 @@ This is simple worker that create new custom object, send it to worker and recie
</thead>
<% @deserialize_tasks.each do |t| %>
- <tr>
+ <tr data-task-id='<%= t.id %>' data-status="<%= t.status %>">
<td><%= t.code_name %></td>
- <td><%= t.created_at %></td>
- <td><%= t.status %></td>
- <td><%= t.duration %></td>
- <td><%= link_to("Results", deserialize_worker_results_custom_examples_path(:id=>t.id)) if t.status=='complete' %></td>
+ <td><span class="created_at"><%= t.created_at %></span></td>
+ <td><span class="status"><%= t.status %></span></td>
+ <td><span class="duration"><%= t.duration %></span></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 %>
</table>
<% else %>
@@ -26,7 +26,7 @@
<h3>List of tasks:</h3>
<% 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>
<tr>
<th>Code Name</th>
@@ -39,16 +39,24 @@
</thead>
<% @tasks.each do |t| %>
- <tr>
+ <tr data-task-id='<%= t.id %>' data-status="<%= t.status %>">
<td><%= t.code_name %></td>
- <td><%= t.created_at %></td>
- <td><%= t.status %></td>
- <td><%= t.duration %></td>
- <td><%= link_to "Log", log_task_path(t.id) %></td>
+ <td><span class="created_at"><%= t.created_at %></span></td>
+ <td><span class="status"><%= t.status %></span></td>
+ <td><span class="duration"><%= t.duration %></span></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>
<%= button_to "Retry", retry_task_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>
<% end %>
</table>
View
@@ -13,6 +13,7 @@
post 'cancel_all'
post 'retry_task'
get 'log'
+ get 'status'
end
end
resources :schedules do

0 comments on commit 3bf4b42

Please sign in to comment.