Skip to content

Commit

Permalink
Merge pull request ejschmitt#80 from shinydevelopment/optional-requeu…
Browse files Browse the repository at this point in the history
…e-pending

Make the 'Enqueue all' button for Pending jobs optional
  • Loading branch information
andyatkinson authored May 2, 2017
2 parents 4e77d8a + 3c7b764 commit 3278a2e
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
13 changes: 13 additions & 0 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,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
------------
Expand Down
16 changes: 12 additions & 4 deletions lib/delayed_job_web/application/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -120,6 +122,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
Expand All @@ -129,11 +132,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
Expand Down
10 changes: 6 additions & 4 deletions lib/delayed_job_web/application/views/pending.erb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
<h1>Pending</h1>
<% if @jobs.any? %>
<form action="<%= u('requeue/pending') %>" method="POST">
<%= csrf_token_tag %>
<input type="submit" value="Enqueue All Immediately"></input>
</form>
<% if @allow_requeue_pending -%>
<form action="<%= u('requeue/pending') %>" method="POST">
<%= csrf_token_tag %>
<input type="submit" value="Enqueue All Immediately"></input>
</form>
<% end -%>
<% end %>
<p class="sub">
The list below contains jobs currently being processed.
Expand Down
21 changes: 21 additions & 0 deletions test/lib/delayed_job_web/application/test_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down

0 comments on commit 3278a2e

Please sign in to comment.