Reputation: 20222
I want my app to execute a task (download CSV files from another website & parse them) just before the server starts.
In which file exactly should I put the code for this?(maybe db/seeds.rb
?)
Also, any idea on how to test this with RSpec?
Upvotes: 1
Views: 748
Reputation: 1060
Inside initializers
folder, can create any .rb
file and can write any logical code in that file. It will be executed on start.
But it may slow down server start, because in your case csv download and parsing may take time to execute.
http://guides.rubyonrails.org/configuring.html#running-code-before-rails
Upvotes: 1
Reputation: 1504
One solution, this may be not right
in lib/tasks/say_hello_rake.rake
namespace :sample do
task :your_task_name do
puts 'Hello my task ... '
end
end
before rails s
rake sample:your_task_name
Upvotes: 0
Reputation: 15515
From the Rails Guides:
In the rare event that your application needs to run some code before Rails itself is loaded, put it above the call to require 'rails/all' in config/application.rb.
Upvotes: 2
Reputation: 4633
The initializers
folder is the proper place to set this tasks.
Just create a file with .rb
extension, and everything else will be performed on start.
http://guides.rubyonrails.org/configuring.html#initializers
Upvotes: 5