Mithun Sasidharan
Mithun Sasidharan

Reputation: 20950

Make Background Job generated files available for Download

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 !!

Upvotes: 2

Views: 658

Answers (2)

0x4a6f4672
0x4a6f4672

Reputation: 28245

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.

Upvotes: 2

Sebastian
Sebastian

Reputation: 2786

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

Upvotes: 1

Related Questions