Hello, I''m developing a rails 2 app with a custom database for persistence (not activerecord, not sql). I need to open the database once at application startup and close it once at application shutdown. I figured out how to add an application startup hook by putting a script in config/initializers. I can''t figure out how to add a shutdown hook. Can someone point me in the right direction? I really need to close the database properly at shutdown time. Thanks a bunch! Michael --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Michael, Michael Allman wrote:> > I''m developing a rails 2 app with a custom database for persistence > (not activerecord, not sql). I need to open the database once at > application startup and close it once at application shutdown. I > figured out how to add an application startup hook by putting a script > in config/initializers. I can''t figure out how to add a shutdown > hook. Can someone point me in the right direction? I really need to > close the database properly at shutdown time.You may be misunderstanding one of the basics of Rails applications. Rails apps are not daemons that start up and then sit there running, waiting for a next request. A Rails app exists for exactly one request - response cycle. It starts up when it receives a request, and it shuts down after it delivers its response. When it receives another request the cycle is repeated. Easiest thing in your case could be to define a start up method and a shut down method. Your start up method could be called based on a global or a class singleton. Your shut down method will probably have to be invoked explicitly though. HTH, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m confused. The config/initializers script does exactly what I want for opening the database, running once at app startup. Surely there''s a comparable way to execute a shutdown script. Or, what method(s) get called when I hit ctrl-c after I''ve started a server or console instance? That''s where I need to close the database. Thanks. Michael On Sep 7, 8:41 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Hi Michael, > > Michael Allman wrote: > > > I''m developing a rails 2 app with a custom database for persistence > > (not activerecord, not sql). I need to open the database once at > > application startup and close it once at application shutdown. I > > figured out how to add an application startup hook by putting a script > > in config/initializers. I can''t figure out how to add a shutdown > > hook. Can someone point me in the right direction? I really need to > > close the database properly at shutdown time. > > You may be misunderstanding one of the basics of Rails applications. Rails > apps are not daemons that start up and then sit there running, waiting for a > next request. A Rails app exists for exactly one request - response cycle. > It starts up when it receives a request, and it shuts down after it delivers > its response. When it receives another request the cycle is repeated. > Easiest thing in your case could be to define a start up method and a shut > down method. Your start up method could be called based on a global or a > class singleton. Your shut down method will probably have to be invoked > explicitly though. > > HTH, > Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 7 Sep 2008, at 21:04, Michael Allman <javathehutt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I''m confused. The config/initializers script does exactly what I want > for opening the database, running once at app startup. Surely there''s > a comparable way to execute a shutdown script. >Don''t think there''s anything rails specific. Ruby provides at_exit which allows you to register callbacks called on exit.> Or, what method(s) get called when I hit ctrl-c after I''ve started a > server or console instance? That''s where I need to close the > database. > > Thanks. > > Michael > > On Sep 7, 8:41 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote: >> Hi Michael, >> >> Michael Allman wrote: >> >>> I''m developing a rails 2 app with a custom database for persistence >>> (not activerecord, not sql). I need to open the database once at >>> application startup and close it once at application shutdown. I >>> figured out how to add an application startup hook by putting a >>> script >>> in config/initializers. I can''t figure out how to add a shutdown >>> hook. Can someone point me in the right direction? I really need >>> to >>> close the database properly at shutdown time. >> >> You may be misunderstanding one of the basics of Rails >> applications. Rails >> apps are not daemons that start up and then sit there running, >> waiting for a >> next request. A Rails app exists for exactly one request - >> response cycle. >> It starts up when it receives a request, and it shuts down after it >> delivers >> its response. When it receives another request the cycle is >> repeated. >> Easiest thing in your case could be to define a start up method and >> a shut >> down method. Your start up method could be called based on a >> global or a >> class singleton. Your shut down method will probably have to be >> invoked >> explicitly though. >> >> HTH, >> Bill > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That''s wonderful, thank you. I was just about to use Signal.trap(0), but I think your solution looks better. Thanks again. Michael On Sep 7, 1:20 pm, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On 7 Sep 2008, at 21:04, Michael Allman <javatheh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I''m confused. The config/initializers script does exactly what I want > > for opening the database, running once at app startup. Surely there''s > > a comparable way to execute a shutdown script. > > Don''t think there''s anything rails specific. Ruby provides at_exit > which allows you to register callbacks called on exit. > > > Or, what method(s) get called when I hit ctrl-c after I''ve started a > > server or console instance? That''s where I need to close the > > database. > > > Thanks. > > > Michael > > > On Sep 7, 8:41 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote: > >> Hi Michael, > > >> Michael Allman wrote: > > >>> I''m developing a rails 2 app with a custom database for persistence > >>> (not activerecord, not sql). I need to open the database once at > >>> application startup and close it once at application shutdown. I > >>> figured out how to add an application startup hook by putting a > >>> script > >>> in config/initializers. I can''t figure out how to add a shutdown > >>> hook. Can someone point me in the right direction? I really need > >>> to > >>> close the database properly at shutdown time. > > >> You may be misunderstanding one of the basics of Rails > >> applications. Rails > >> apps are not daemons that start up and then sit there running, > >> waiting for a > >> next request. A Rails app exists for exactly one request - > >> response cycle. > >> It starts up when it receives a request, and it shuts down after it > >> delivers > >> its response. When it receives another request the cycle is > >> repeated. > >> Easiest thing in your case could be to define a start up method and > >> a shut > >> down method. Your start up method could be called based on a > >> global or a > >> class singleton. Your shut down method will probably have to be > >> invoked > >> explicitly though. > > >> HTH, > >> Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 7 Sep 2008, at 16:41, Bill Walton wrote:> > Hi Michael, > > Michael Allman wrote: >> >> I''m developing a rails 2 app with a custom database for persistence >> (not activerecord, not sql). I need to open the database once at >> application startup and close it once at application shutdown. I >> figured out how to add an application startup hook by putting a >> script >> in config/initializers. I can''t figure out how to add a shutdown >> hook. Can someone point me in the right direction? I really need to >> close the database properly at shutdown time. > > You may be misunderstanding one of the basics of Rails > applications. Rails > apps are not daemons that start up and then sit there running, > waiting for a > next request. A Rails app exists for exactly one request - response > cycle. > It starts up when it receives a request, and it shuts down after it > delivers > its response.Um what? Something like a mongrel instance is exactly something that sits there and waits for requests to come in. Fred> When it receives another request the cycle is repeated. > Easiest thing in your case could be to define a start up method and > a shut > down method. Your start up method could be called based on a global > or a > class singleton. Your shut down method will probably have to be > invoked > explicitly though. > > HTH, > Bill > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I noticed Rails reloads my classes on every request. My database can''t handle that. My database depends on classes maintaining their identity. That is, given a class "Person", Person.object_id must not vary across requests. Is there a way to turn off this "reload per request" behavior? And why does it do this in the first place? Seems rather strange. Thanks. Michael On Sep 7, 8:41 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Hi Michael, > > Michael Allman wrote: > > > I''m developing a rails 2 app with a custom database for persistence > > (not activerecord, not sql). I need to open the database once at > > application startup and close it once at application shutdown. I > > figured out how to add an application startup hook by putting a script > > in config/initializers. I can''t figure out how to add a shutdown > > hook. Can someone point me in the right direction? I really need to > > close the database properly at shutdown time. > > You may be misunderstanding one of the basics of Rails applications. Rails > apps are not daemons that start up and then sit there running, waiting for a > next request. A Rails app exists for exactly one request - response cycle. > It starts up when it receives a request, and it shuts down after it delivers > its response. When it receives another request the cycle is repeated. > Easiest thing in your case could be to define a start up method and a shut > down method. Your start up method could be called based on a global or a > class singleton. Your shut down method will probably have to be invoked > explicitly though. > > HTH, > Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Fred, Frederick Cheung wrote:> Um what? Something like a mongrel instance is exactly > something that sits there and waits for requests to come in.That''s true. But Mongrel is not your Rails app. The OP said "I need to open the database once at application startup and close it once at application shutdown.". I was trying to separate the issues / components. Perhaps I should have said more about the other components of the Rails application "ecosystem." Best regards, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Nevermind. I figured it out: config.cache_classes = true Michael On Sep 7, 4:32 pm, Michael Allman <javatheh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I noticed Rails reloads my classes on every request. My database > can''t handle that. My database depends on classes maintaining their > identity. That is, given a class "Person", Person.object_id must not > vary across requests. Is there a way to turn off this "reload per > request" behavior? And why does it do this in the first place? Seems > rather strange. > > Thanks. > > Michael > > On Sep 7, 8:41 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote: > > > Hi Michael, > > > Michael Allman wrote: > > > > I''m developing a rails 2 app with a custom database for persistence > > > (not activerecord, not sql). I need to open the database once at > > > application startup and close it once at application shutdown. I > > > figured out how to add an application startup hook by putting a script > > > in config/initializers. I can''t figure out how to add a shutdown > > > hook. Can someone point me in the right direction? I really need to > > > close the database properly at shutdown time. > > > You may be misunderstanding one of the basics of Rails applications. Rails > > apps are not daemons that start up and then sit there running, waiting for a > > next request. A Rails app exists for exactly one request - response cycle. > > It starts up when it receives a request, and it shuts down after it delivers > > its response. When it receives another request the cycle is repeated. > > Easiest thing in your case could be to define a start up method and a shut > > down method. Your start up method could be called based on a global or a > > class singleton. Your shut down method will probably have to be invoked > > explicitly though. > > > HTH, > > Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Michael, Michael Allman wrote:> I noticed Rails reloads my classes on every request.Only in development mode. That''s so we don''t have to stop / restart the web server for every little change. Saves a lot of development time. HTH, Bill --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Mon, Sep 8, 2008 at 1:32 AM, Michael Allman <javathehutt-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I noticed Rails reloads my classes on every request. My database > can''t handle that. My database depends on classes maintaining their > identity. That is, given a class "Person", Person.object_id must not > vary across requests. Is there a way to turn off this "reload per > request" behavior? And why does it do this in the first place? Seems > rather strange.This is done to be able to refresh changes automatically while you are developing, since normally that''s more handy than restarting the server by hand to load changes. You can turn it off setting config.cache_classes = true in config/environments/development.rb. Take into account that if you restart the application object IDs for the same classes are not guaranteed to be the same (in general they won''t be). Also take into account that in the typical multi-process setup there are as many interpreters running as processes, and thus those handlers are going to run those many times. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On Sep 8, 12:49 am, "Bill Walton" <bill.wal...-xwVYE8SWAR3R7s880joybQ@public.gmane.org> wrote:> Hi Fred, > > Frederick Cheung wrote: > > Um what? Something like a mongrel instance is exactly > > something that sits there and waits for requests to come in. > > That''s true. But Mongrel is not your Rails app. The OP said "I need to > open the database once at application startup and close it once at > application shutdown.". I was trying to separate the issues / components. > Perhaps I should have said more about the other components of the Rails > application "ecosystem." >That still seems a little murky (other than in dev mode obviously) application level stuff does persist across requests. Fred> Best regards, > Bill--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---