2

I have an action in my controller that generates reports of site usage based on date range selected (Start and End Date) with the condition that the start and end date has to be within the current quarter.

I want to implement a background job that will generate this report at the end of each quarter and make it available for download as a link on the view page.

Each quarter is 3 months and so for example - On March 31st 11:59 PM, I want it to generate the report of site usage from Jan 1st to March 31st and make it available for download in the view page.

I prefer using the whenever gem if possible, since i am already using it for re-indexing the models at definite intervals. All possible suggestions are welcomed !!

Please help me with how to go about this !!

Charles
  • 50,943
  • 13
  • 104
  • 142
Mithun Sasidharan
  • 20,572
  • 10
  • 34
  • 52

2 Answers2

2

I would recommend the Gems DelayedJob (2.1 for Rails 3.0+) or Resque for creating background jobs, placing them on multiple queues, and processing them later. There are good Railscasts for both.

0x4a6f4672
  • 27,297
  • 17
  • 103
  • 140
1

You can add class ReportGenerator and use it in rails runner. This class will be calculate a date range, generate report and saves in under public direcotory of your application. You must notice that every file in public direcotry is accessible for anybody. If you wan't some authorization to access this file you can put in other directory for example - unpublic and serve it via your app (auth purpose) using X-Sendfile http header for Apache.

ReportGenerator.new(:quater => 4).generate

For recognize which quater you have:

quaters => {
  1 => "definition of 1st quater (date ranges)",
  2 => "definition of 2nd quater (date ranges)",
  3 => "definition of 3rd quater (date ranges)",
  4 => "definition of 4th quater (date ranges)".
}
quater = Date.today.month / 3
Sebastian
  • 2,618
  • 3
  • 25
  • 32
  • Thanks for the help... i dnt have a report model i have a controller and view. and in the controller i have defined a method tht accepts two dates and generates a xls report for download in the window on submit. How can i go about getting this as background in the way u mentioned.. could you pls provide some light into this? – Mithun Sasidharan Dec 16 '11 at 09:02
  • I didn't mention any Report model. Just a Ruby class, without ActiveRecord which go the same as your controller-action with one difference. You don't pass date range explicit but only a quater nunber. And you add something like every 3.months { runner "ReportGenerator.new(:quater => Date.today.month / 3).generate" } – Sebastian Dec 16 '11 at 09:17