Description
Backstory
I am implementing the PubNub gem in my Rails app. I use ActiveRecord callbacks as a hook to publish events when a model is created/updated/destroyed/etc. So I've been using the Rails console extensively as I develop to chain together a series of statements that create a variety of model instances, save them and then I monitor the PubNub dev console for my published event (the PubNub dev console is very helpful, btw).
pubnub gem 3.5.1
Rails/Puma
Issue
Sometimes when publishing a message I would get an exception noting that the Event Machine hadn't started.
eventmachine not initialized: evma_install_oneshot_timer
The same set of statements would sometimes result in the exception, sometimes not. Based on some quick research and a bit of conjecture, it seems that the first publish invocation would prompt EM to start, but before it completed initializing, another publish invocation occurs and results in the exception. Like I said, just speculation on my part.
Fix/Workaround
Based on a search of the error message, I came across a fix where as soon as you start EM, have that thread wait until EM is up and running before continuing. I put this in a rails initializer and I haven't had the problem since. Cheers
Thread.new { EM.run } unless EM.reactor_running?
Thread.pass until EM.reactor_running?