Ruby Rack: startup and teardown operations (Tokyo Cabinet connection)

Posted by clint.tseng on Stack Overflow See other posts from Stack Overflow or by clint.tseng
Published on 2010-04-06T05:28:54Z Indexed on 2010/04/06 5:33 UTC
Read the original article Hit count: 328

Filed under:
|
|
|
|

I have built a pretty simple REST service in Sinatra, on Rack. It's backed by 3 Tokyo Cabinet/Table datastores, which have connections that need to be opened and closed. I have two model classes written in straight Ruby that currently simply connect, get or put what they need, and then disconnect. Obviously, this isn't going to work long-term.

I also have some Rack middleware like Warden that rely on these model classes.

What's the best way to manage opening and closing the connections? Rack doesn't provide startup/shutdown hooks as I'm aware. I thought about inserting a piece of middleware that provides reference to the TC/TT object in env, but then I'd have to pipe that through Sinatra to the models, which doesn't seem efficient either; and that would only get be a per-request connection to TC. I'd imagine that per-server-instance-lifecycle would be a more appropriate lifespan.

Thanks!

© Stack Overflow or respective owner

Related posts about ruby

Related posts about rack