cloudfoundry/bosh — pull request #674 by mariash (2014-10-12 00:09:13+00:00)
Title: Run NATS.connect on EM thread

I was able to reproduce the issue running:
```
while bundle exec rspec spec/integration/cli_vms_spec.rb:6; do echo ok; done
```
Roughly once in 10 times the compilation will be stuck since EM thread was dying.

Adding more logs to NATS revealed that NATS was sending EM.connect which was resulting in ConnectionNotBound error.

```
sending command CONNECT {"verbose":false,"pedantic":false}
Error raised during event loop: #
```

Every time it was the same error.

Based on this suggestion [1] I moved NATS.connect to run on EM thread. As suggested here [2] it was added to EM#run. The test is now running for 4+ hours and it never failed.

+ Verify that current thread is reactor thread when connecting to NATS.

+ This is only an issue in worker and not in director since Thin processes requests on EM thread.

[1] https://github.com/eventmachine/eventmachine/wiki/FAQ#does-em-work-with-other-ruby-threads-running
[2] https://github.com/eventmachine/eventmachine/blob/master/lib/eventmachine.rb#L98