Skip to content

Instantly share code, notes, and snippets.

View andrewvc's full-sized avatar

Andrew Cholakian andrewvc

View GitHub Profile
DripDrop::Node.new do |node|
zpub = node.zmq_pub('tcp://127.0.0.1:2902') #Create a ZMQ Pub Socket
ws = node.websocket('ws://127.0.0.1:2903') #Create a em-websocket websocket socket
#Create a ZMQ Sub socket, and handle recv of DripDrop::Message messages
node.zmq_sub('tcp://127.0.0.1:2904') do |message|
message.body = message.decoded.body.upcase
zpub.send_message(message) websocket.send_message(message)
end
end
DripDrop::Node.new do |node|
###
### ZMQ Forwarder
###
fwd_pub = node.zmq_publish(FORWARDER_OUT)
node.zmq_subscribe(FORWARDER_IN,:socket_ctype => :bind).on_recv_raw do |message|
fwd_pub.send_message(message)
print 'f'
end
#Kind of like tail -f -n1
db = Mongo::Connection.new(mongo_hostname).db(mongo_dbname)
coll = db.collection(mongo_collection)
start_count = coll.count
tail = Mongo::Cursor.new(coll, :tailable => true, :order => [['$natural', 1]]).skip(start_count- 1)
loop do
if doc = tail.next_document
puts doc
else
sleep 1
require 'rubygems'
require 'ffi-rzmq'
#PUSH and PULL sockets work together to load balance messages going one way.
#Multiple PULL sockets connected to a PUSH each receive messages from the PUSH.
#ZeroMQ automatically load balances the messages between all pull sockets.
push_thread = Thread.new do
push_ctx = ZMQ::Context.new(1)
push_sock = push_ctx.socket(ZMQ::PUSH)
push_sock.bind('tcp://127.0.0.1:2200')
#Rescuing Exceptions is bad... use StandardError.
#Exception catches some signals, like SIGINT if there's no trap for them. Probably not what you want.
trap("TERM") {
puts "Received Term"
}
begin
puts "Sleeping"
puts Process.pid
+-[HTTP SERVER]--+ +----------------------+
| Incoming | | Misc ZMQ Service |
+-----[XREQ]-----+ +-------[XREQ]---------+
\ /
\ /
+-----[XREP]-----+
| RPROXY |
+-----[XREQ]-----+
/ \
/ \
#Check if a pid is running
def pid_running?(pid)
begin
Process.getpgid(pid)
rescue Errno::ESRCH
false
else
true
end
end
export RUBY_GC_MALLOC_LIMIT=60000000
export RUBY_HEAP_MIN_SLOTS=1000000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=3
export RUBY_HEAP_SLOTS_INCREMENT=500000
export RUBY_HEAP_FREE_MIN=100000
#New Style:
DripDrop::Node.new
addr = 'tcp://127.0.0.1:2222';
zmq_sub(addr,:bind) do |message|
end
pub = zmq_pub(addr,:connect)
pub.send_message(:name => 'test', :body => 'hi')
end.start!
#BAD: Rescuing a very generic error class like StandardError or ArgumentError without a logger statement. makes it harder to debug code #If someone modifies Foo#baz later, they'll be wondering why their errors aren't being caught.
begin
Foo.calculate_baz
rescue StandardError => e
return 5
end
#BAD: Never rescue Exception, use StandardError instead. Exception catches some signals as Exceptions.
begin
...