This is a very simple example of an RabbitMQ subscriber in Ruby using the bunny gem. It uses the dead_letter pattern to resend failures to another queue if processing fails. The original message is ack'd so it does not get reprocessed. Something else probably needs to be monitoring the messages on the dead_letter queue.
Signal.trap('INT') { @subscriber.shutdown }
Signal.trap('TERM') { @subscriber.shutdown }
class Subscriber
def initialize
@bunny = Bunny.new(...)
@queue = ...
@dead_letter = ...
end
def run
@queue.subscribe(:ack => true) do |msg|
payload = JSON.parse(msg[:payload])
begin
do_something_with_the_payload(payload)
rescue StandardError, Timeout::Error => error
routing_key = msg[:delivery_details][:routing_key]
@dead_letter.publish(payload, :key => routing_key)
end
end
end
def shutdown
@queue.unsubscribe rescue nil
@bunny.stop rescue nil
exit
end
end
@subscriber = Subscriber.new
@subscriber.run
Unicorn is Unix mentions the use of the self-pipe trick, mixing select with Unix signals. D. J. Bernstein, the creator of daemontools states he came up with it here. The problem is that a signal handler might be called while select is starting up. The solution is to select for read on a pipe and write to the pipe from within the signal handler.
# self_pipe example in Ruby
SELF_PIPE = IO.pipe
['INT', 'TERM'].each { |sig| trap(sig) {shutdown}}
def shutdown
SELF_PIPE[1].puts '.'
end
selected = select(SELF_PIPE, nil, nil, nil)
puts "SELF_PIPE success" if selected[0][0] == SELF_PIPE[0]
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut in tortor augue, eu accumsan lorem. Sed eget mauris ut nulla posuere molestie a sit amet massa. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Etiam a quam lobortis justo aliquet varius. Donec sollicitudin mauris non quam consectetur nec fringilla sem placerat. Quisque viverra sapien mi, nec eleifend nibh. Vestibulum at tortor eu nisi aliquam porta sed eu risus. Phasellus auctor est volutpat velit suscipit eget mollis lacus suscipit. Praesent sed felis id leo pellentesque tincidunt in mattis nibh. Ut diam erat, porta a varius at, facilisis eu orci. Mauris in turpis purus. Praesent congue, urna vel varius congue, elit lectus ultrices arcu, quis consequat eros quam non tortor. Nam eget felis elit. Curabitur nunc massa, molestie ac cursus in, convallis a urna. Maecenas laoreet justo ac leo pulvinar a pharetra libero molestie. Fusce lacus ligula, ullamcorper eu fringilla vitae, pharetra ullamcorper mi.
The surprising truth about what motivates us.
(It's not the money.)
The Blueprint CSS framework can be used to apply a fixed-width, column based layout and typographic principles to html. The css reset minimizes browser differences and provide a starting point for applying consistent CSS styles across browsers.
The use of a grid enables both a column based layout and a typographic baseline to be applied consistently throughout the site.
This is the personal blog of Simon Horne. I currently work as a Software Developer for Streetline Networks in San Francisco.