mirror of
https://github.com/Fishwaldo/huginn.git
synced 2025-03-16 03:41:41 +00:00
Simple job management page
Also added a seperate `job-indicator` for every job state (pending/awaiting retry and failed) The jobs page is automatically reloading when jobs are enqueued, retried or failed.
This commit is contained in:
parent
3afd215457
commit
830cf1bf3d
16 changed files with 322 additions and 35 deletions
3
Gemfile
3
Gemfile
|
@ -45,9 +45,6 @@ gem 'delayed_job', '~> 4.0.0'
|
|||
gem 'delayed_job_active_record', '~> 4.0.0'
|
||||
gem 'daemons', '~> 1.1.9'
|
||||
|
||||
# To enable DelayedJobWeb, see the 'Enable DelayedJobWeb' section of the README.
|
||||
# gem 'delayed_job_web'
|
||||
|
||||
gem 'foreman', '~> 0.63.0'
|
||||
|
||||
gem 'sass-rails', '~> 4.0.0'
|
||||
|
|
|
@ -1,22 +1,29 @@
|
|||
$ ->
|
||||
firstEventCount = null
|
||||
previousJobs = null
|
||||
|
||||
if $("#job-indicator").length
|
||||
if $(".job-indicator").length
|
||||
check = ->
|
||||
$.getJSON "/worker_status", (json) ->
|
||||
for method in ['pending', 'awaiting_retry', 'recent_failures']
|
||||
count = json[method]
|
||||
elem = $(".job-indicator[role=#{method}]")
|
||||
if count > 0
|
||||
tooltipOptions = {
|
||||
title: "#{count} jobs #{method.split('_').join(' ')}"
|
||||
delay: 0
|
||||
placement: "bottom"
|
||||
trigger: "hover"
|
||||
}
|
||||
if elem.is(":visible")
|
||||
elem.tooltip('destroy').tooltip(tooltipOptions).find(".number").text(count)
|
||||
else
|
||||
elem.tooltip('destroy').tooltip(tooltipOptions).fadeIn().find(".number").text(count)
|
||||
else
|
||||
if elem.is(":visible")
|
||||
elem.tooltip('destroy').fadeOut()
|
||||
|
||||
firstEventCount = json.event_count unless firstEventCount?
|
||||
|
||||
if json.pending? && json.pending > 0
|
||||
tooltipOptions = {
|
||||
title: "#{json.pending} jobs pending, #{json.awaiting_retry} awaiting retry, and #{json.recent_failures} recent failures"
|
||||
delay: 0
|
||||
placement: "bottom"
|
||||
trigger: "hover"
|
||||
}
|
||||
$("#job-indicator").tooltip('destroy').tooltip(tooltipOptions).fadeIn().find(".number").text(json.pending)
|
||||
else
|
||||
$("#job-indicator:visible").tooltip('destroy').fadeOut()
|
||||
|
||||
if firstEventCount? && json.event_count > firstEventCount
|
||||
$("#event-indicator").tooltip('destroy').
|
||||
tooltip(title: "Click to reload", delay: 0, placement: "bottom", trigger: "hover").
|
||||
|
@ -26,6 +33,12 @@ $ ->
|
|||
else
|
||||
$("#event-indicator").tooltip('destroy').fadeOut()
|
||||
|
||||
currentJobs = [json.pending, json.awaiting_retry, json.recent_failures]
|
||||
if document.location.pathname == '/jobs' && previousJobs? && previousJobs.join(',') != currentJobs.join(',')
|
||||
$.get '/jobs', (data) =>
|
||||
$("#main-content").html(data)
|
||||
previousJobs = currentJobs
|
||||
|
||||
window.workerCheckTimeout = setTimeout check, 2000
|
||||
|
||||
check()
|
||||
|
|
|
@ -88,9 +88,10 @@ span.not-applicable:after {
|
|||
}
|
||||
|
||||
// Navbar
|
||||
|
||||
#job-indicator, #event-indicator {
|
||||
display: none;
|
||||
.nav > li {
|
||||
&.job-indicator, &#event-indicator {
|
||||
display: none;
|
||||
}
|
||||
}
|
||||
|
||||
.navbar-search > .spinner {
|
||||
|
|
3
app/assets/stylesheets/jobs.css.scss
Normal file
3
app/assets/stylesheets/jobs.css.scss
Normal file
|
@ -0,0 +1,3 @@
|
|||
.big-modal-dialog {
|
||||
width: 90% !important;
|
||||
}
|
|
@ -14,6 +14,10 @@ class ApplicationController < ActionController::Base
|
|||
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
|
||||
end
|
||||
|
||||
def authenticate_admin!
|
||||
redirect_to root_path unless current_user && current_user.admin
|
||||
end
|
||||
|
||||
def upgrade_warning
|
||||
return unless current_user
|
||||
twitter_oauth_check
|
||||
|
|
55
app/controllers/jobs_controller.rb
Normal file
55
app/controllers/jobs_controller.rb
Normal file
|
@ -0,0 +1,55 @@
|
|||
class JobsController < ApplicationController
|
||||
before_filter :authenticate_admin!
|
||||
|
||||
def index
|
||||
@jobs = Delayed::Job.page(params[:page])
|
||||
|
||||
respond_to do |format|
|
||||
format.html { render layout: !request.xhr? }
|
||||
format.json { render json: @jobs }
|
||||
end
|
||||
end
|
||||
|
||||
def destroy
|
||||
@job = Delayed::Job.find(params[:id])
|
||||
|
||||
respond_to do |format|
|
||||
if !running? && @job.destroy
|
||||
format.html { redirect_to jobs_path, notice: "Job deleted." }
|
||||
format.json { render json: "", status: :ok }
|
||||
else
|
||||
format.html { redirect_to jobs_path, alert: 'Can not delete a running job.' }
|
||||
format.json { render json: "", status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def run
|
||||
@job = Delayed::Job.find(params[:id])
|
||||
@job.last_error = nil
|
||||
|
||||
respond_to do |format|
|
||||
if !running? && @job.update_attributes!(run_at: Time.now, failed_at: nil)
|
||||
format.html { redirect_to jobs_path, notice: "Job enqueued." }
|
||||
format.json { render json: @job, status: :ok }
|
||||
else
|
||||
format.html { redirect_to jobs_path, alert: 'Can not enqueue a running job.' }
|
||||
format.json { render json: "", status: :unprocessable_entity }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def destroy_failed
|
||||
Delayed::Job.where.not(failed_at: nil).destroy_all
|
||||
|
||||
respond_to do |format|
|
||||
format.html { redirect_to jobs_path, notice: "Failed jobs removed." }
|
||||
format.json { render json: '', status: :ok }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def running?
|
||||
@job.locked_at || @job.locked_by
|
||||
end
|
||||
end
|
|
@ -38,4 +38,8 @@ module ApplicationHelper
|
|||
link_to 'No', agent_path(agent, tab: (agent.recent_error_logs? ? 'logs' : 'details')), class: 'label label-danger'
|
||||
end
|
||||
end
|
||||
|
||||
def user_is_admin?
|
||||
current_user && current_user.admin == true
|
||||
end
|
||||
end
|
||||
|
|
21
app/helpers/jobs_helper.rb
Normal file
21
app/helpers/jobs_helper.rb
Normal file
|
@ -0,0 +1,21 @@
|
|||
module JobsHelper
|
||||
|
||||
def status(job)
|
||||
case
|
||||
when job.failed_at
|
||||
content_tag :span, 'failed', class: 'label label-danger'
|
||||
when job.locked_at && job.locked_by
|
||||
content_tag :span, 'running', class: 'label label-info'
|
||||
else
|
||||
content_tag :span, 'queued', class: 'label label-warning'
|
||||
end
|
||||
end
|
||||
|
||||
def relative_distance_of_time_in_words(time)
|
||||
if time < (now = Time.now)
|
||||
time_ago_in_words(time) + ' ago'
|
||||
else
|
||||
'in ' + distance_of_time_in_words(time, now)
|
||||
end
|
||||
end
|
||||
end
|
73
app/views/jobs/index.html.erb
Normal file
73
app/views/jobs/index.html.erb
Normal file
|
@ -0,0 +1,73 @@
|
|||
<div class='container'>
|
||||
<div class='row'>
|
||||
<div class='col-md-12'>
|
||||
<div class="page-header">
|
||||
<h2>
|
||||
Background Jobs
|
||||
</h2>
|
||||
</div>
|
||||
|
||||
<div class='table-responsive'>
|
||||
<table class='table table-striped events'>
|
||||
<tr>
|
||||
<th>Status</th>
|
||||
<th>Created</th>
|
||||
<th>Next Run</th>
|
||||
<th>Attempts</th>
|
||||
<th>Last Error</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
|
||||
<% @jobs.each do |job| %>
|
||||
<tr>
|
||||
<td><%= status(job) %></td>
|
||||
<td title='<%= job.created_at %>'><%= time_ago_in_words job.created_at %> ago</td>
|
||||
<td title='<%= job.run_at %>'>
|
||||
<% if !job.failed_at %>
|
||||
<%= relative_distance_of_time_in_words job.run_at %>
|
||||
<% end %>
|
||||
</td>
|
||||
<td><%= job.attempts %></td>
|
||||
<td>
|
||||
<a data-toggle="modal" data-target="#error<%= job.id %>"><%= truncate job.last_error, :length => 90, :omission => "", :separator => "\n" %></a>
|
||||
<div class="modal fade" id="error<%= job.id %>" tabindex="-1" role="dialog" aria-labelledby="#<%= "error#{job.id}" %>" aria-hidden="true">
|
||||
<div class="modal-dialog big-modal-dialog">
|
||||
<div class="modal-content">
|
||||
<div class="modal-header">
|
||||
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
|
||||
<h4 class="modal-title" id="myModalLabel">Error Backtrace</h4>
|
||||
</div>
|
||||
<div class="modal-body">
|
||||
<%= raw html_escape(job.last_error).split("\n").join('<br/>') %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</td>
|
||||
<td>
|
||||
<% if !job.locked_at && !job.locked_by %>
|
||||
<div class="btn-group btn-group-xs" style="float: right">
|
||||
<% if job.run_at > Time.now %>
|
||||
<%= link_to 'Run now', run_job_path(job), class: "btn btn-default", method: :put %>
|
||||
<% end %>
|
||||
<%= link_to 'Delete', job_path(job), class: "btn btn-danger", method: :delete, data: { confirm: 'Really delete this job?' } %>
|
||||
</div>
|
||||
<% end %>
|
||||
</td>
|
||||
</tr>
|
||||
<% end %>
|
||||
</table>
|
||||
</div>
|
||||
|
||||
<%= paginate @jobs, :theme => 'twitter-bootstrap-3' %>
|
||||
|
||||
<br />
|
||||
<div class="btn-group">
|
||||
<%= link_to destroy_failed_jobs_path, class: "btn btn-default", method: :delete do %>
|
||||
<span class="glyphicon glyphicon-trash"></span> Remove failed jobs
|
||||
<% end %>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
|
@ -35,15 +35,19 @@
|
|||
</div>
|
||||
</form>
|
||||
|
||||
<li id='job-indicator'>
|
||||
<% if defined?(DelayedJobWeb) %>
|
||||
<a href="/delayed_job">
|
||||
<span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
|
||||
</a>
|
||||
<% else %>
|
||||
<a href="#" onclick='return false;'>
|
||||
<span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
|
||||
</a>
|
||||
<li class='job-indicator' role='pending'>
|
||||
<%= link_to jobs_path do %>
|
||||
<span class="badge"><span class="glyphicon glyphicon-refresh icon-white"></span> <span class='number'>0</span></span>
|
||||
<% end %>
|
||||
</li>
|
||||
<li class='job-indicator' role='awaiting_retry'>
|
||||
<%= link_to jobs_path do %>
|
||||
<span class="badge"><span class="glyphicon glyphicon-question-sign icon-yellow"></span> <span class='number'>0</span></span>
|
||||
<% end %>
|
||||
</li>
|
||||
<li class='job-indicator' role='recent_failures'>
|
||||
<%= link_to jobs_path do %>
|
||||
<span class="badge"><span class="glyphicon glyphicon-exclamation-sign icon-white"></span> <span class='number'>0</span></span>
|
||||
<% end %>
|
||||
</li>
|
||||
<li id='event-indicator'>
|
||||
|
@ -66,7 +70,11 @@
|
|||
<%= link_to 'Sign up', new_user_registration_path, :tabindex => "-1" %>
|
||||
<% end %>
|
||||
</li>
|
||||
|
||||
<% if user_signed_in? && current_user.admin %>
|
||||
<li>
|
||||
<%= link_to 'Job Management', jobs_path, :tabindex => '-1' %>
|
||||
</li>
|
||||
<% end %>
|
||||
<li>
|
||||
<%= link_to 'About', 'https://github.com/cantino/huginn', :tabindex => "-1" %>
|
||||
</li>
|
||||
|
|
|
@ -28,7 +28,9 @@
|
|||
<%= render "upgrade_warning" %>
|
||||
<% end %>
|
||||
|
||||
<%= yield %>
|
||||
<div id="main-content">
|
||||
<%= yield %>
|
||||
</div>
|
||||
|
||||
</div>
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
Delayed::Worker.destroy_failed_jobs = true
|
||||
Delayed::Worker.destroy_failed_jobs = false
|
||||
Delayed::Worker.max_attempts = 5
|
||||
Delayed::Worker.max_run_time = 20.minutes
|
||||
Delayed::Worker.read_ahead = 5
|
||||
|
|
|
@ -51,6 +51,15 @@ Huginn::Application.routes.draw do
|
|||
end
|
||||
end
|
||||
|
||||
resources :jobs, :only => [:index, :destroy] do
|
||||
member do
|
||||
put :run
|
||||
end
|
||||
collection do
|
||||
delete :destroy_failed
|
||||
end
|
||||
end
|
||||
|
||||
get "/worker_status" => "worker_status#show"
|
||||
|
||||
post "/users/:user_id/update_location/:secret" => "user_location_updates#create"
|
||||
|
@ -58,9 +67,6 @@ Huginn::Application.routes.draw do
|
|||
match "/users/:user_id/web_requests/:agent_id/:secret" => "web_requests#handle_request", :as => :web_requests, :via => [:get, :post, :put, :delete]
|
||||
post "/users/:user_id/webhooks/:agent_id/:secret" => "web_requests#handle_request" # legacy
|
||||
|
||||
# To enable DelayedJobWeb, see the 'Enable DelayedJobWeb' section of the README.
|
||||
# get "/delayed_job" => DelayedJobWeb, :anchor => false
|
||||
|
||||
devise_for :users, :sign_out_via => [ :post, :delete ]
|
||||
get '/auth/:provider/callback', to: 'services#callback'
|
||||
|
||||
|
|
67
spec/controllers/jobs_controller_spec.rb
Normal file
67
spec/controllers/jobs_controller_spec.rb
Normal file
|
@ -0,0 +1,67 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe JobsController do
|
||||
|
||||
describe "GET index" do
|
||||
before do
|
||||
Delayed::Job.create
|
||||
Delayed::Job.create
|
||||
Delayed::Job.count.should > 0
|
||||
end
|
||||
|
||||
it "does not allow normal users"do
|
||||
sign_in users(:bob)
|
||||
get(:index).should redirect_to(root_path)
|
||||
end
|
||||
it "returns all jobs", focus: true do
|
||||
sign_in users(:jane)
|
||||
get :index
|
||||
assigns(:jobs).length.should == 2
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE destroy" do
|
||||
before do
|
||||
@not_running = Delayed::Job.create
|
||||
@running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
|
||||
sign_in users(:jane)
|
||||
end
|
||||
|
||||
it "destroy a job which is not running" do
|
||||
expect { delete :destroy, id: @not_running.id }.to change(Delayed::Job, :count).by(-1)
|
||||
end
|
||||
|
||||
it "does not destroy a running job" do
|
||||
expect { delete :destroy, id: @running.id }.to change(Delayed::Job, :count).by(0)
|
||||
end
|
||||
end
|
||||
|
||||
describe "PUT run" do
|
||||
before do
|
||||
@not_running = Delayed::Job.create(run_at: Time.now - 1.hour)
|
||||
@running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
|
||||
sign_in users(:jane)
|
||||
end
|
||||
|
||||
it "queue a job which is not running" do
|
||||
expect { put :run, id: @not_running.id }.to change { @not_running.reload.run_at }
|
||||
end
|
||||
|
||||
it "not queue a running job" do
|
||||
expect { put :run, id: @running.id }.not_to change { @not_running.reload.run_at }
|
||||
end
|
||||
end
|
||||
|
||||
describe "DELETE destroy_failed" do
|
||||
before do
|
||||
@failed = Delayed::Job.create(failed_at: Time.now - 1.minute)
|
||||
@running = Delayed::Job.create(locked_at: Time.now, locked_by: 'test')
|
||||
sign_in users(:jane)
|
||||
end
|
||||
|
||||
it "just destroy failed jobs" do
|
||||
expect { delete :destroy_failed, id: @failed.id }.to change(Delayed::Job, :count).by(-1)
|
||||
expect { delete :destroy_failed, id: @running.id }.to change(Delayed::Job, :count).by(0)
|
||||
end
|
||||
end
|
||||
end
|
3
spec/fixtures/users.yml
vendored
3
spec/fixtures/users.yml
vendored
|
@ -10,4 +10,5 @@ jane:
|
|||
email: "jane@example.com"
|
||||
username: jane
|
||||
invitation_code: <%= User::INVITATION_CODES.last %>
|
||||
scenario_count: 1
|
||||
scenario_count: 1
|
||||
admin: true
|
32
spec/helpers/jobs_helper_spec.rb
Normal file
32
spec/helpers/jobs_helper_spec.rb
Normal file
|
@ -0,0 +1,32 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe JobsHelper do
|
||||
let(:job) { Delayed::Job.new }
|
||||
|
||||
describe '#status' do
|
||||
it "works for failed jobs" do
|
||||
job.failed_at = Time.now
|
||||
status(job).should == '<span class="label label-danger">failed</span>'
|
||||
end
|
||||
|
||||
it "works for running jobs" do
|
||||
job.locked_at = Time.now
|
||||
job.locked_by = 'test'
|
||||
status(job).should == '<span class="label label-info">running</span>'
|
||||
end
|
||||
|
||||
it "works for queued jobs" do
|
||||
status(job).should == '<span class="label label-warning">queued</span>'
|
||||
end
|
||||
end
|
||||
|
||||
describe '#relative_distance_of_time_in_words' do
|
||||
it "in the past" do
|
||||
relative_distance_of_time_in_words(Time.now-5.minutes).should == '5m ago'
|
||||
end
|
||||
|
||||
it "in the furute" do
|
||||
relative_distance_of_time_in_words(Time.now+5.minutes).should == 'in 5m'
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Add table
Reference in a new issue