Guest User

Untitled

a guest
Oct 16th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.19 KB | None | 0 0
  1. after_fork do |server, worker|
  2.  
  3. require "amqp"
  4.  
  5. amqp_opts = {:port => $RABBITMQ_PORT, :host => $RABBITMQ_HOST, :username => $RABBITMQ_USER, :password => $RABBITMQ_PASS, :vhost => $RABBITMQ_VHOST}
  6. t = Thread.new { AMQP.start(amqp_opts) }
  7. sleep(1.0)
  8.  
  9. EventMachine.add_periodic_timer(60) do
  10. exchange = AMQP.channel.topic($TOPIC_EXCHANGE)
  11. exchange.publish("Heartbeat message #{Time.now.to_s}", :routing_key => "websocket.heartbeat.0")
  12. end
  13.  
  14. EventMachine.next_tick do
  15. AMQP.channel ||= AMQP::Channel.new(AMQP.connection, 2, :auto_recovery => true)
  16.  
  17. AMQP.channel.on_error do |ch, channel_close|
  18. raise channel_close.reply_text
  19. end
  20.  
  21. if AMQP.channel.auto_recovering?
  22. puts "Channel #{ch1.id} IS auto-recovering"
  23. end
  24.  
  25. AMQP.connection.on_tcp_connection_loss do |conn, settings|
  26. puts "[AMQP network failure] Trying to reconnect..."
  27. conn.reconnect(false, 2)
  28. end
  29. exchange = AMQP.channel.topic($TOPIC_EXCHANGE)
  30.  
  31. 3.times do |i|
  32. puts "[after_fork/amqp] Publishing a warmup message ##{i}"
  33. exchange.publish("A warmup message #{i} from #{Time.now.strftime('%H:%M:%S %m/%b/%Y')}", :routing_key => "websocket.heartbeat.0")
  34. end
  35.  
  36. end
  37.  
  38. end
Add Comment
Please, Sign In to add comment