user2490003
user2490003

Reputation: 11900

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!

Upvotes: 0

Views: 1866

Answers (2)

spickermann
spickermann

Reputation: 106922

  1. Just create a Cron task that runs periodically. That Cron task starts a shell stript that just does all the step you would run manually.

  2. There is a gem (https://github.com/biola/turnout) at helps with the maintainance page. It provides rake tasks like rake maintenance:start and rake maintenance:end.

  3. I think it is not necessary to drop the tables. Usually it should be enough to just delete all records and then create new records. If you really have to drop the database, it might be faster to just restore the database schema from a structure dump.

Upvotes: 1

Chris Cherry
Chris Cherry

Reputation: 28554

  1. There is not a 'rails way' to reload a rails app every so often, since starting and stopping a rails app is outside the context of the app itself. A cron job is a fine way to go.
  2. Typically a web server is running "in front" of the rails app, apache or nginix are common. The maintenance page would need to be implemented on that level (since the rails app is down for a moment remember), something like a temporary change to the configs and a reload should suffice. Then when bringing that app back online, restore the config to point to the rails app and reload again.
  3. Using the rake tasks you have is fine, set the environment variable RAILS_ENV=production so they hit the right sqlitedb file. Don't bother attempting to call the rake tasks at rails start up, call them from the script called by your cron job, and then start the app after that.

Upvotes: 1

Related Questions