Reputation: 11900
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 -
rake db:drop; rake db:create; rake db:migrate
)rake myApp:update
)This brings up a few questions
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
Reputation: 106922
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.
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
.
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
Reputation: 28554
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