Class: Hare::Runner
- Inherits:
-
Object
- Object
- Hare::Runner
- Defined in:
- lib/hare/runner.rb
Instance Attribute Summary collapse
-
#arguments ⇒ Object
Returns the value of attribute arguments.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#initialize(argv) ⇒ Runner
constructor
A new instance of Runner.
- #parse! ⇒ Object
- #parser ⇒ Object
- #run! ⇒ Object
Constructor Details
#initialize(argv) ⇒ Runner
Returns a new instance of Runner.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/hare/runner.rb', line 12 def initialize(argv) @argv = argv = { :logging => false, :publish => false, :amqp => { :host => 'localhost', :port => nil, :exchange => { :name => nil, :kind => :topic, }, :queue => '', :vhost => '/', :via_ssl => false, :ssl_cert => nil, :ssl_key => nil, :ssl_verify => false } } parse! end |
Instance Attribute Details
#arguments ⇒ Object
Returns the value of attribute arguments.
10 11 12 |
# File 'lib/hare/runner.rb', line 10 def arguments @arguments end |
#options ⇒ Object
Returns the value of attribute options.
9 10 11 |
# File 'lib/hare/runner.rb', line 9 def end |
Instance Method Details
#parse! ⇒ Object
105 106 107 108 |
# File 'lib/hare/runner.rb', line 105 def parse! parser.parse! @argv @arguments = @argv end |
#parser ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/hare/runner.rb', line 37 def parser @parser ||= OptionParser.new do |opts| opts. = "Usage: hare [options] [MSG]" opts.separator "" opts.separator "Common Options:" opts.on("-h", "--host HOST", "The AMQP server host") { |host| [:amqp][:host] = host } opts.on("-p", "--port PORT", "The AMQP server host port") { |port| [:amqp][:port] = port } opts.on("--vhost VHOST", "The AMQP vhost on which to connect") { |vhost| [:amqp][:vhost] = vhost } opts.on("--exchange_name EXCHANGE", "The name of the AMQP exchange on which to connect") { |exc| [:amqp][:exchange][:name] = exc } opts.on("--exchange_type TYPE", "The type of the AMQP exchange on which to connect") do |type| if ['topic', 'direct', 'fanout'].member? type [:amqp][:exchange][:kind] = type.to_sym else puts "Exchange type must be (topic|direct|fanout), not #{type}." exit 1 end end opts.on("--username NAME", "The AMQP username.") { |u| [:amqp][:username] = u } opts.on("--password PSWD", "The AMQP password for the user given.") { |p| [:amqp][:password] = p } opts.on("--route_key KEY", "The key to route messages over.") { |k| [:amqp][:key] = k } opts.on("--logging", "Enable logging of AMQP interactions.") { [:logging] = true } opts.on("--ssl_cert CERT", "Path to SSL chain certificates") do |c| [:amqp][:ssl_cert] = c end opts.on("--ssl_key PRIVKEY", "Path to SSL private key") do |k| [:amqp][:ssl_key] = k end opts.separator "" opts.separator "Consumer Options: " opts.on("--queue QUEUE", "The queue on which to listen.") { |q| [:amqp][:queue] = q } opts.separator "" opts.separator "Producer Options: " opts.on("--producer", "Toggle to enable producing messages") { [:publish] = true } opts.separator "" opts.on_tail("--help", "Show this message.") do puts opts exit end opts.on_tail("-v", "--version", "Show version") { puts Hare::VERSION; exit } end end |
#run! ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/hare/runner.rb', line 110 def run! amqp = [:amqp] EventMachine.run do AMQP.connect(:host => amqp[:host], :port => amqp[:port], :vhost => amqp[:vhost], :username => amqp[:username], :password => amqp[:password], :ssl => { :cert_chain_file => amqp[:ssl_cert], :private_key_file => amqp[:ssl_key] }, :on_tcp_connection_failure => Proc.new { |settings| puts "TCP Connection failure; details:\n\n#{settings.inspect}\n\n"; exit 1 }, :on_possible_authentication_failure => Proc.new { |settings| puts "Authentication failure, I'm afraid:\n\n#{settings.inspect}\n\n"; exit 1 }) do |connection| channel = AMQP::Channel.new(connection) case amqp[:exchange][:kind] when :direct exchange = channel.direct(amqp[:exchange][:name]) when :fanout exchange = channel.fanout(amqp[:exchange][:name]) when :topic exchange = channel.topic(amqp[:exchange][:name]) end if [:publish] exchange.publish(@arguments[0], :routing_key => amqp[:key]) do connection.disconnect { EM.stop } end else channel.queue(amqp[:queue]).bind(exchange, :key => amqp[:key]).subscribe do |payload| puts payload end end end end # EM end |