Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions lib/qless.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@ def initialize(client)
def [](name)
Throttle.new(name, @client)
end

def counts
@client.queues.counts.map do |queue|
Throttle.new(queue['name'], @client)
end
end
end

# A class for interacting with events. Not meant to be instantiated directly,
Expand Down
34 changes: 34 additions & 0 deletions lib/qless/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ def paginated(qless_object, method, *args)
def tabs
[
{ name: 'Queues' , path: '/queues' },
{ name: 'Throttles', path: '/throttles'},
{ name: 'Workers' , path: '/workers' },
{ name: 'Track' , path: '/track' },
{ name: 'Failed' , path: '/failed' },
Expand All @@ -95,6 +96,10 @@ def queues
client.queues.counts
end

def throttles
client.throttles.counts
end

def tracked
client.jobs.tracked
end
Expand Down Expand Up @@ -194,6 +199,12 @@ def strftime(t)
}
end

get '/throttles/?' do
erb :throttles, layout: true, locals: {
title: 'Throttles'
}
end

get '/failed.json' do
json(client.jobs.failed)
end
Expand Down Expand Up @@ -481,6 +492,29 @@ def strftime(t)
end
end

post '/delete_throttle' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is a delete action in the http e.g. delete '/throttle' do ... end

# Expects a JSON object: {'id': id}
data = JSON.parse(request.body.read)
if data['id'].nil?
halt 400, 'Need throttle id'
else
throttle = Throttle.new(data['id'], client)
throttle.delete
return json({id: throttle.id, maximum: throttle.maximum})
end
end

post '/update_throttle' do

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should just be post /throttle do .. end

# Expects a JSON object: {'id': id, 'maximum': maximum}
data = JSON.parse(request.body.read)
if data['id'].nil? || data['maximum'].nil?
halt 400, 'Need throttle id and maximum value'
else
throttle = Throttle.new(data['id'], client)
throttle.maximum = data['maximum']
end
end

# start the server if ruby file executed directly
run! if app_file == $PROGRAM_NAME
end
Expand Down
28 changes: 28 additions & 0 deletions lib/qless/server/views/layout.erb
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,34 @@
})
}

var delete_throttle = function(throttle_id) {
_ajax({
url: '<%= u "/delete_throttle" %>',
data: {
'id': throttle_id
}, success: function(data) {
flash('Deleted throttle for ' + throttle_id, 'success');
$('.throttle-' + throttle_id).val(data['maximum']);
}, error: function(data) {
flash('Couldn\'t delete thottle ' + throttle_id);
}
})
}

var update_throttle = function(throttle_id, maximum) {
_ajax({
url: '<%= u "/update_throttle" %>',
data: {
'id': throttle_id,
'maximum': maximum
}, success: function(data) {
flash('Updated throttle for ' + throttle_id, 'success');
}, error: function(data) {
flash('Couldn\'t update throttle ' + throttle_id);
}
})
}

$(document).ready(function() {
$('button').tooltip({delay:200});
});
Expand Down
33 changes: 33 additions & 0 deletions lib/qless/server/views/throttles.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<% if throttles.empty? %>
<div class="page-header">
<h1>No Queues To Throttle <small>What a fine predicament to be in...</small></h1>
</div>
<% else %>
<div class="page-header">
<h1>Throttles <small>By queue.</small></h1>
</div>

<% throttles.each do |throttle| %>
<div class="row">
<div class="span3">
<h3>
<%= throttle.id %>
</h3>
</div>
<div class="span2">
<div style="float:right; margin-right; 12px; margin-top: 4px">
<div class="btn-group">
<input class="span1 maximum throttle-<%= throttle.id %>" type="text" placeholder="<%= throttle.maximum %>" onchange="update_throttle('<%= throttle.id %>', $(this).val())"></input>
</div>
</div>
</div>
<div class="span2">
<div class="btn-group" style="float:left; margin-top: 4px">
<button title="delete" class="btn btn-danger" onclick="confirmation(this, 'Delete?', function() { delete_throttle('<%= throttle.id %>', fade) })">
<i class="icon-remove"></i>
</button>
</div>
</div>
</div>
<% end %>
<% end %>
1 change: 0 additions & 1 deletion lib/qless/throttle.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,5 @@ def throttle_attrs
def update(max, expiration = 0)
@client.call('throttle.set', @name, max || maximum, expiration)
end

end
end
27 changes: 27 additions & 0 deletions spec/integration/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,33 @@ def test_pagination(page_1_jid = 1, page_2_jid = 27)
test_pagination
end

it 'can set and delete throttles for all the queues', js: true do
q.put(Qless::Job, {})

text_field_class = ".throttle-#{q.name}"
throttle = Throttle.new(q.name, client)

throttle.maximum.should eq(0)

visit '/throttles'

first('h3', text: /testing/i).should be
first(text_field_class, placeholder: /0/i).should be

maximum = first(text_field_class)
maximum.set(3)
maximum.trigger('blur');

first(text_field_class, value: /3/i).should be

throttle.maximum.should eq(3)

first('button.btn-danger').click
first('button.btn-danger').click

throttle.maximum.should eq(0)
end

it 'can see the root-level summary' do
visit '/'

Expand Down