I have a controller action that generates a number of excel reports, this takes about 10 minutes to do. Sometimes I call it from my webapp, which is why it is an action.
But I also want to create a rake task to run this, so I can schedule it to automatically run once a night.
Any way to do this?
Go to Websites & Domains and click Ruby. After gems installation you can try to run a Rake task by clicking Run rake task. In the opened dialog, you can provide some parameters and click OK - this will be equivalent to running the rake utility with the specified parameters in the command line.
If you meant exiting from a rake task without causing the "rake aborted!" message to be printed, then you can use either "abort" or "exit".
Can you handle the report generation from your models? Models should be doing most of the work anyway and can be accessed from Rake tasks:
task :reports => :environment do
...
# Do stuff with your models.
end
I think you'll have to move your code into your model. Since it's bad to put knowledge about output rendering in models, I'd suggest putting all the business logic and data manipulation in the model, but then put the rendering code in your rake task. That would make the rake task analogous to the controller used on the web - maintaining separation of concerns.
You can look at ActionView::Base and work from there to figure out how to trigger rendering of templates.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With