mardi 23 décembre 2014

Automatically restarting a Rails app and running Rake tasks

I have a Rails application that pulls information from various internal APIs and stores in a local SQLite DB. My rails application is essentially a glorified UI on top of this data pulled from multiple APIs.


For reasons outside the scope of this question, it is not straightforward to simply update the information in the DB on a periodic basis by re-querying the API. I'm basically forced to recreate the DB from scratch.


In essence I want to do something like this every X hours -



  1. Automatically shut down the rails application

  2. Put up a maintenance page ("Sorry, we'll be back in a few mins")

  3. Drop the db, recreate it, and re-migrate it (rake db:drop; rake db:create; rake db:migrate)

  4. Run my custom rake task that populates the tables again (rake myApp:update)

  5. Re-start the application


This brings up a few questions



  1. How do I have the app restart automatically every X hours? Should I schedule this externally using a cron job? Is there a rails way I can accomplish this?

  2. How do I display a maintenance page if the app is down? Again, is this also an external re-direct I need to manage?

  3. Most importantly, is there a good way to drop the tables and recreate them or should I be using rake tasks? Is there a way to call rake tasks at startups? I guess I could create a .rb process under config/initalizers that would run at startup (but only when Rails.env == 'production')?


Thanks for the help!


Aucun commentaire:

Enregistrer un commentaire