Long-term TCP socket monitoring in Ruby
My day jobs for years have been full(ish) stack developer in Ruby on Rails, so I'm used to very short-term web requests, and occasional longer-running processes in Resque/Sidekiq. I've written plenty of code which connects to remote APIs, authenticates, perform a single action, then disconnect.
I'm working on a project for a 8-hour event later this year. A vendor will be continuously outputting event-related data via a TCP server. I will need to be connected and monitoring nearly 100% of the time (accounting for downtime in case the socket gets disconnected). With a very basic script in IRB, I can successfully connect to their server using the TCPSocket class and get what I need from their feed.
What are typical Ruby tools for this type of longer-term process? Sure, I could just keep an IRB console open all day and grab what I need, but surely there are better tools to do this.
Related, anyone know of a robust TCP client library/gem which might handle reconnections and whatnot?
This will be running on my laptop all day, so I don't necessarily need to worry about hosting this.
Posted Apr 7, 2016, 6:22 PM UTC
A pure ruby solution is to use a daemon wrapper,
daemonsis probably the most popular: https://github.com/thuehlinger/daemonshttps://github.com/drbrain/net-http-persistent works well, and if you want a higher level API Faraday supports it as a backend.
Persistent HTTP connections will not avail you if the connection drops or is reset. Servers recycle connections periodically anyways (see
max-age).The network is unreliable, plan around it.
Did you look at the library? Part of the point of it's existence is that maintains the connection, detecting failures and reconnecting as needed.
Even if it detects that failures occurred, it can't detect if the HTTP messages were actually posted. Which means messages will often arrive more than once on the other end.
There is a fair amount of complexity to faithfully reconstituting a bytestream over unstable TCP connections.
ah I'd forgotten about daemon - I used it to write a mail client (believe it or not) years ago.