From 10a16e491ad19cb7bdea2625bfcf89c23fbf5d08 Mon Sep 17 00:00:00 2001 From: Chris Mear Date: Fri, 3 Jul 2015 17:02:40 +0100 Subject: [PATCH 1/3] Hide 'enqueue all' for pending jobs, as a configurable option. --- lib/delayed_job_web/application/app.rb | 16 ++++++++++++---- .../application/views/pending.erb | 10 ++++++---- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/lib/delayed_job_web/application/app.rb b/lib/delayed_job_web/application/app.rb index bcc63ab..20c6957 100644 --- a/lib/delayed_job_web/application/app.rb +++ b/lib/delayed_job_web/application/app.rb @@ -9,6 +9,8 @@ class DelayedJobWeb < Sinatra::Base set :public_folder, File.expand_path('../public', __FILE__) set :views, File.expand_path('../views', __FILE__) + set :allow_requeue_pending, true + # Enable sessions so we can use CSRF protection enable :sessions @@ -106,6 +108,7 @@ def csrf_token_tag get "/#{page}" do @jobs = delayed_jobs(page.to_sym, @queues).order('created_at desc, id desc').offset(start).limit(per_page) @all_jobs = delayed_jobs(page.to_sym, @queues) + @allow_requeue_pending = settings.allow_requeue_pending erb page.to_sym end end @@ -115,11 +118,16 @@ def csrf_token_tag redirect back end - %w(pending failed).each do |page| - post "/requeue/#{page}" do - delayed_jobs(page.to_sym, @queues).update_all(:run_at => Time.now, :failed_at => nil) - redirect back + post "/requeue/pending" do + if settings.allow_requeue_pending + delayed_jobs(:pending, @queues).update_all(:run_at => Time.now, :failed_at => nil) end + redirect back + end + + post "/requeue/failed" do + delayed_jobs(:failed, @queues).update_all(:run_at => Time.now, :failed_at => nil) + redirect back end post "/requeue/:id" do diff --git a/lib/delayed_job_web/application/views/pending.erb b/lib/delayed_job_web/application/views/pending.erb index 87fcde2..d8f0255 100644 --- a/lib/delayed_job_web/application/views/pending.erb +++ b/lib/delayed_job_web/application/views/pending.erb @@ -1,9 +1,11 @@

Pending

<% if @jobs.any? %> -
- <%= csrf_token_tag %> - -
+ <% if @allow_requeue_pending -%> +
+ <%= csrf_token_tag %> + +
+ <% end -%> <% end %>

The list below contains jobs currently being processed. From d9d5d53cd374a124c2dc9d55ba87d63561db9511 Mon Sep 17 00:00:00 2001 From: Chris Mear Date: Mon, 6 Jul 2015 12:40:23 +0100 Subject: [PATCH 2/3] Test that /requeue/pending does nothing when allow_requeue_pending is false. --- .../delayed_job_web/application/test_app.rb | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/lib/delayed_job_web/application/test_app.rb b/test/lib/delayed_job_web/application/test_app.rb index 3b4f142..76e0da5 100644 --- a/test/lib/delayed_job_web/application/test_app.rb +++ b/test/lib/delayed_job_web/application/test_app.rb @@ -52,6 +52,27 @@ def test_requeue_pending end + def test_requeue_pending_with_requeue_pending_disallowed + + app.set(:allow_requeue_pending, false) + + dataset = Minitest::Mock.new + where = lambda { |criteria| + dataset + } + + Time.stub(:now, time) do + Delayed::Job.stub(:where, where) do + post "/requeue/pending", request_data, rack_env + last_response.status.must_equal 302 + end + end + + # Expect dataset to not have received any method calls. + dataset.verify + + end + def test_requeue_id job = Minitest::Mock.new job.expect(:update_attributes, nil, [:run_at => time, :failed_at => nil]) From 3c7b764775eaab46c24abd683752cfbc2239cb41 Mon Sep 17 00:00:00 2001 From: Chris Mear Date: Mon, 6 Jul 2015 12:49:23 +0100 Subject: [PATCH 3/3] Add documentation for `allow_requeue_pending` setting. --- README.markdown | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/README.markdown b/README.markdown index 8bd1ec6..b538407 100644 --- a/README.markdown +++ b/README.markdown @@ -75,6 +75,19 @@ Rails' will need to be configured to `config.action_dispatch.x_sendfile_header = Lighty is more `X-Sendfile`, like [outlined](http://redmine.lighttpd.net/projects/1/wiki/X-LIGHTTPD-send-file) in their wiki. +Configuration +------------- + +The following settings can be changed using the `.set` method in your configu.ru. For example: + +```ruby +DelayedJobWeb.set(:allow_requeue_pending, false) +``` + +* **`allow_requeue_pending`** (default: `true`) + + Controls whether the 'Enqueue all immediately' button is available on the list of Pending jobs. Hiding this button can be useful if you have jobs set to run in the future and you don't want to accidentally run them immediately. + Contributing ------------