Browse Source

Just keep the most recent 100 failed jobs + small improvements

Dominik Sander 10 years ago
parent
commit
f27b6c5c21

+ 4 - 1
.env.example

@@ -124,4 +124,7 @@ ENABLE_INSECURE_AGENTS=false
 #USE_GRAPHVIZ_DOT=dot
 
 # Timezone. Use `rake time:zones:local` or `rake time:zones:all` to get your zone name
-TIMEZONE="Pacific Time (US & Canada)"
+TIMEZONE="Pacific Time (US & Canada)"
+
+# Number of failed jobs to keep in the database
+FAILED_JOBS_TO_KEEP=100

+ 0 - 6
README.md

@@ -85,12 +85,6 @@ See [private development instructions](https://github.com/cantino/huginn/wiki/Pr
 
 In order to use the WeatherAgent you need an [API key with Wunderground](http://www.wunderground.com/weather/api/). Signup for one and then change the value of `api_key: your-key` in your seeded WeatherAgent.
 
-#### Enable DelayedJobWeb for handy delayed\_job monitoring and control
-
-* Edit `config.ru`, uncomment the DelayedJobWeb section, and change the DelayedJobWeb username and password.
-* Uncomment `match "/delayed_job" => DelayedJobWeb, :anchor => false` in `config/routes.rb`.
-* Uncomment `gem "delayed_job_web"` in Gemfile and run `bundle`.
-
 #### Disable SSL
 
 We assume your deployment will run over SSL. This is a very good idea! However, if you wish to turn this off, you'll probably need to edit `config/initializers/devise.rb` and modify the line containing `config.rememberable_options = { :secure => true }`.  You will also need to edit `config/environments/production.rb` and modify the value of `config.force_ssl`.

+ 1 - 1
app/controllers/application_controller.rb

@@ -15,7 +15,7 @@ class ApplicationController < ActionController::Base
   end
 
   def authenticate_admin!
-    redirect_to root_path unless current_user && current_user.admin
+    redirect_to(root_path, alert: 'Admin access required to view that page.') unless current_user && current_user.admin
   end
 
   def upgrade_warning

+ 1 - 1
app/controllers/jobs_controller.rb

@@ -40,7 +40,7 @@ class JobsController < ApplicationController
   end
 
   def destroy_failed
-    Delayed::Job.where.not(failed_at: nil).destroy_all
+    Delayed::Job.where.not(failed_at: nil).delete_all
 
     respond_to do |format|
       format.html { redirect_to jobs_path, notice: "Failed jobs removed." }

+ 3 - 1
app/views/jobs/index.html.erb

@@ -38,7 +38,9 @@
                       <h4 class="modal-title" id="myModalLabel">Error Backtrace</h4>
                     </div>
                     <div class="modal-body">
-                      <%= raw html_escape(job.last_error).split("\n").join('<br/>') %>
+                      <pre>
+                        <%= raw html_escape(job.last_error).split("\n").join('<br/>') %>
+                      </pre>
                     </div>
                   </div>
                 </div>

+ 3 - 3
app/views/layouts/_navigation.html.erb

@@ -36,17 +36,17 @@
       </form>
       
       <li class='job-indicator' role='pending'>
-        <%= link_to jobs_path do %>
+        <%= link_to current_user.admin? ? 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 %>
+        <%= link_to current_user.admin? ? 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 %>
+        <%= link_to current_user.admin? ? jobs_path : '#' do %>
           <span class="badge"><span class="glyphicon glyphicon-exclamation-sign icon-white"></span> <span class='number'>0</span></span>
         <% end %>
       </li>

+ 0 - 8
config.ru

@@ -2,12 +2,4 @@
 
 require ::File.expand_path('../config/environment',  __FILE__)
 
-# To enable DelayedJobWeb, see the 'Enable DelayedJobWeb' section of the README.
-
-# if Rails.env.production?
-#  DelayedJobWeb.use Rack::Auth::Basic do |username, password|
-#    username == 'admin' && password == 'password'
-#  end
-# end
-
 run Huginn::Application

+ 12 - 0
lib/huginn_scheduler.rb

@@ -32,6 +32,11 @@ class HuginnScheduler
     end
   end
 
+  def cleanup_failed_jobs!
+    first_to_delete = Delayed::Job.where.not(failed_at: nil).order("failed_at DESC").offset(ENV['FAILED_JOBS_TO_KEEP'].try(:to_i) || 100).limit(ENV['FAILED_JOBS_TO_KEEP'].try(:to_i) || 100).pluck(:failed_at).first
+    Delayed::Job.where(["failed_at <= ?", first_to_delete]).delete_all if first_to_delete.present?
+  end
+
   def with_mutex
     ActiveRecord::Base.connection_pool.with_connection do
       mutex.synchronize do
@@ -57,6 +62,13 @@ class HuginnScheduler
       cleanup_expired_events!
     end
 
+    # Schedule failed job cleanup.
+
+    @rufus_scheduler.every '1h' do
+      cleanup_failed_jobs!
+    end
+
+
     # Schedule repeating events.
 
     %w[1m 2m 5m 10m 30m 1h 2h 5h 12h 1d 2d 7d].each do |schedule|

+ 1 - 1
spec/helpers/jobs_helper_spec.rb

@@ -25,7 +25,7 @@ describe JobsHelper do
       relative_distance_of_time_in_words(Time.now-5.minutes).should == '5m ago'
     end
 
-    it "in the furute" do
+    it "in the future" do
       relative_distance_of_time_in_words(Time.now+5.minutes).should == 'in 5m'
     end
   end