Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 76 lines (61 sloc) 2.042 kb
67983da @skaes make it possible to selectively pause listening on queues
skaes authored
1 # attempts.rb
2 # this example shows you how to use the exception limiting feature of beetle
3 # it allows you to control the number of retries your handler will go through
4 # with one message before giving up on it
5 #
6 # ! check the examples/README.rdoc for information on starting your redis/rabbit !
7 #
8 # start it with ruby attempts.rb
9
10 require "rubygems"
11 require File.expand_path("../lib/beetle", File.dirname(__FILE__))
12
13 # set Beetle log level to info, less noisy than debug
14 Beetle.config.logger.level = Logger::INFO
15
16 # setup client
17 $client = Beetle::Client.new
18 $client.register_queue(:test)
19 $client.register_message(:test)
20
21 # purge the test queue
22 $client.purge(:test)
23
24 # initially, our service is online
25 $online = true
26
27 # declare a handler class for message processing
28 # in this example we've not only overwritten the process method but also the
29 # error and failure methods of the handler baseclass
30 class Handler < Beetle::Handler
31
32 # called when the handler receives the message - fail everytime
33 def process
34 puts "received message #{message.data} online=#{$online}"
35 unless $online
36 $client.pause_listening(:test)
37 raise "offline"
38 end
39 end
40
41 # called when handler process raised an exception
42 def error(exception)
43 puts "execution failed: #{exception}"
44 end
45
46 end
47
48 # publish a decent amount of messages
49 # 1000.times do |i|
50 # $client.publish(:test, i)
51 # end
52 # $client.stop_publishing
53
54 # register our handler to the message, configure it to our max_exceptions limit, we configure a delay of 0 to have it not wait before retrying
55 $client.register_handler(:test, Handler, :exceptions => 1, :delay => 0)
56
57 # and start our listening loop...
58 $client.listen do
59 n = 0
60 ptimer = EM.add_periodic_timer(0.1) do
61 data = (n+=1)
62 puts "publishing message #{data}"
63 $client.publish(:test, data)
64 end
65
66 EM.add_periodic_timer(2) do
67 $online = !$online
68 $client.resume_listening(:test) if $online
69 end
70
71 EM.add_timer(10) do
72 $client.pause_listening
73 EM.add_timer(1) { $client.stop_listening }
74 end
75 end
Something went wrong with that request. Please try again.