igrigorik / em-jack

An Evented Beanstalk Client
http://dj2.github.com/jack
Other
64 stars 17 forks source link

Not possible to reserve on connected reliably #9

Closed mloughran closed 13 years ago

mloughran commented 13 years ago

I wanted to reserve after the beanstalkd connection has connected successfully - however this was surprisingly hard to achieve (unless I've just missed something)

Explicitly, this is what I wanted

The EMJack::Connection object is a deferrable, however you can't do

# Doesn't work
conn = EMJack::Connection.new

conn.callback {
  # reserve on tube
}

This doesn't work because EMJack::Connection (which is a deferrable) fails on the first unsuccessful connection attempt. The Connection#disconnected method calls set_deferred_status(nil) which will clear all callbacks.

My conclusion is that the EMJack::Connection deferrable is used heavily for internal callbacks and thus it's behaviour can't be changed. Therefore an on_connect callback is required.

It's also necessary to have a connected? boolean: in the case that code needs to reserve dynamically, it needs to know whether to just reserve of reserve in an on_connect callback.

Code is attached but not tested - if you're happy with this addition then I'll add the tests for it :)

dj2 commented 13 years ago

This looks good to me. I've merged this part in, if you have time to write some tests please send them my way.