Skip to content

Commit

Permalink
Add queue up/down/pause/resume buttons and queuemgr handler
Browse files Browse the repository at this point in the history
  • Loading branch information
sodabrew committed Jul 16, 2014
1 parent 50c5e41 commit b31fa3d
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 4 deletions.
28 changes: 28 additions & 0 deletions code/main.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,34 @@ def flash(type, msg)
end
end

post '/q/:name/adminoper' do
res = if params[:pause] && params[:resume] && params[:down] && params[:up]
# Makes no sense
elsif params[:pause]
action = "pause"
queuemgr.pause_queue(params[:name])
elsif params[:resume]
action = "resume"
queuemgr.resume_queue(params[:name])
elsif params[:down]
action = "down"
queuemgr.down_queue(params[:name])
elsif params[:up]
action = "up"
queuemgr.up_queue(params[:name])
end

if not res
throw :halt, [500, "500 - Couldn't #{action} queue. Internal error."]
end
if res[0] != 'ok'
throw :halt, [500, "500 - Couldn't #{action} queue. #{res.inspect}."]
end

flash :notice, "Successfully #{action}d queue #{params[:name]}"
redirect back
end

post '/q/:name/restart' do
res = queuemgr.restart_queue(params[:name])

Expand Down
9 changes: 9 additions & 0 deletions code/public/css/rq.css
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ form .note {
font-style: italic;
}

form.inline {
padding: 0;
margin: 0;
}

form.inline button {
width: 5em;
}

#queue-lists {table-layout: fixed}
#queue-lists td, #queue-lists th {padding: 5px}
#queue-lists th {font-size: 20px}
Expand Down
62 changes: 62 additions & 0 deletions code/queuemgr.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,68 @@ def handle_request(sock)
resp = [(Time.now - @start_time).to_i, ].to_json
send_packet(sock, resp)

when 'down_queue'
if valid_queue_name(arg) && queue = @queues[arg]
f = File.new("config/#{arg}.down", File::CREAT, 0644) rescue nil
if f
resp = ['ok', arg].to_json
else
resp = ['fail', 'not allowed to down'].to_json
end
else
resp = ['fail', 'invalid queue name'].to_json
end
send_packet(sock, resp)

when 'up_queue'
if valid_queue_name(arg) && queue = @queues[arg]
if File.exists? "config/#{arg}.down"
count = File.unlink "config/#{arg}.down" rescue 0
if count > 0
resp = ['ok', arg].to_json
else
resp = ['fail', 'not allowed to up'].to_json
end
else
# Not downed anyways, return success
resp = ['ok', arg].to_json
end
else
resp = ['fail', 'invalid queue name'].to_json
end
send_packet(sock, resp)

when 'pause_queue'
if valid_queue_name(arg) && queue = @queues[arg]
f = File.new("config/#{arg}.pause", File::CREAT, 0644) rescue nil
if f
resp = ['ok', arg].to_json
else
resp = ['fail', 'not allowed to pause'].to_json
end
else
resp = ['fail', 'invalid queue name'].to_json
end
send_packet(sock, resp)

when 'resume_queue'
if valid_queue_name(arg) && queue = @queues[arg]
if File.exists? "config/#{arg}.pause"
count = File.unlink "config/#{arg}.pause" rescue 0
if count > 0
resp = ['ok', arg].to_json
else
resp = ['fail', 'not allowed to resume'].to_json
end
else
# Not paused anyways, return success
resp = ['ok', arg].to_json
end
else
resp = ['fail', 'invalid queue name'].to_json
end
send_packet(sock, resp)

when 'restart_queue'
stop_queue(arg)
# Reset the error count because the queue was manually restarted
Expand Down
4 changes: 4 additions & 0 deletions code/queuemgrclient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def read_pid
queues
create_queue
create_queue_link
up_queue
down_queue
pause_queue
resume_queue
restart_queue
delete_queue
}
Expand Down
24 changes: 21 additions & 3 deletions code/views/_main_queue_row.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
raise unless qc.running?
admin_stat, oper_stat = qc.status
-%>
<% if admin_stat != 'DOWN' -%>
<td><pre><%= msgs_labels.zip(qc.num_messages.values_at(*msgs_labels)).map{|ab| "#{ab[0]}:#{ab[1].to_s.ljust(4)} "}.join %></pre></td>
<% else -%>
<td>-</td>
<% end -%>
<td><%= qc.ping %></td>
<td><%= qc.read_pid %></td>
<td><%= qc.uptime %></td>
Expand All @@ -20,7 +24,21 @@
<td>-</td>
<td><span class="red">DOWN <%= $! %></span></td>
<% end -%>
<td><form method="post" action="<%= "#{url}q/#{name}/restart" %>">
<button id="restart-queue">Restart</button>
</form></td>
<td>
<form class="inline" method="post" action="<%= "#{url}q/#{name}/adminoper" %>">
<%- if admin_stat == 'PAUSE' -%>
<button id="resume-queue" name="resume" value="resume" <%= 'disabled="disabled"' if admin_stat == 'DOWN' %>>Resume</button>
<%- else -%>
<button id="pause-queue" name="pause" value="pause" <%= 'disabled="disabled"' if admin_stat == 'DOWN' %>>Pause</button>
<%- end -%>
<%- if admin_stat == 'DOWN' -%>
<button id="up-queue" name="up" value="up">Up</button>
<%- else -%>
<button id="down-queue" name="down" value="down">Down</button>
<%- end -%>
</form>
<form class="inline" method="post" action="<%= "#{url}q/#{name}/restart" %>">
<button id="restart-queue" name="restart" value="restart">Restart</button>
</form>
</td>
</tr>
2 changes: 1 addition & 1 deletion code/views/queue.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<pre id="config" class="json hidden"><%= JSON.pretty_generate(config) %></pre>
</div>

<% if admin_status == 'UP' %>
<% if %w{UP PAUSE}.include? admin_status %>
<h2>Message List</h2>

<% num_msgs = qc.num_messages %>
Expand Down

0 comments on commit b31fa3d

Please sign in to comment.