I think this is what you need, although I have not tested it.
class RestartingUserTracker def initialize @client = TweetStream::Client.new end def handle_status(status) # do whatever it is you're going to do with the status end def fetch_users accounts.get_updated_user_list end def restart @client.stop_stream users = fetch_users @client.follow(users) do |status| handle_status(status) end end end EM.run do client = RestartingUserTracker.new client.restart EM::PeriodicTimer.new(60) do client.restart end end
Here's how it works:
TweetStream uses EventMachine internally as a way to poll the API forever and process responses. I can understand why you might have been stuck because the regular TweetStream API blocks forever and prevents you from interfering at any time. However, TweetStream allows you to tune other things in a single event loop. In your case, a timer. I found documentation on how to do this here: https://github.com/intridea/tweetstream#removal-of-on_interval-callback
By launching our own EventMachine reactor, we can enter our own code into the reactor, as well as use TweetStream. In this case, we use a simple timer that restarts the client every 60 seconds.
EventMachine is an implementation of what is called a reactor template. If you want to fully understand and keep this code, it will be useful for you to find some resources and get a full understanding. The reactor design is very powerful, but can hardly be understood at first.
However, this code should get started. In addition, I would consider renaming RestartingUserTracker to something more suitable.
source share