3

I feel like this is a dumb question, because everyone seems to gloss over this aspect of things.

My need is simple - I'm running some reports using Ruport on Heroku. Complicated reports take a long time, and they time out. I've decided to use delayed_job - but I could use something else instead. Once I know what I'm doing, I might use it for other things as well.

I see lots of examples, railscasts, documentation about how to get delayed_job and its brethren up and running, how to manage your job queue, etc. I might have some questions once I get to that point, but for now...

How the heck do I get back to my queued job?

Right now, I just generate the output like this:

@report_output     = @report_controller.render(@report_format,@report_params)
send_data @report_output, :filename => "#{@report_type}.#{@report_format}", :type => "application/#{@report_format}" unless :html == @report_format

(and then if it is an html report, it just renders the page normally)

it works fine in development, times out in production. I can see from the documentation it would even be pretty easy to just generate the output and mail it to the user, then I don't have to worry about getting the job back.

But I don't want that. I just want to show the user a progress page, poll the server regularly until it's done, and then redirect them to see the HTML or PDF output. I'm expecting this to return some sort of ID of the job and hand it back to the user so they can check on it, but I don't see where that comes from. I suppose I could just look in the table, but that seems silly. I could possibly even show the user a list of queued/completed jobs and let them choose one manually, but that's overkill. They just want to download a report, and they don't mind if it takes a minute or two.

I'm not actually using a table for my reports, no need, they're not being saved - but I suppose could give them a table and an ActiveRecord model if that helped. It doesn't seem like I should have to do that. I'm using Rails 2.3, but it doesn't look like that's relevant to my question either.

It seems like what I'm asking is so obvious that no one has bothered to write it down, but I can't seem to find it. Is there a sample app? What am I missing? I feel foolish.

stephan.com
  • 1,518
  • 18
  • 26
  • did you manage to solve this problem? I'm facing the exact same problem that you faced. there is an ocean of code examples but i can't find any which solves this problem – BenKoshy May 29 '20 at 02:10

2 Answers2

1

You could use a hosted background processing service like IronWorker (disclaimer: I work for Iron.io). You would pass the job to IronWorker and have the worker update the status while it's processing the job in the background. You could then poll IronWorker to show the status to the user and when it's finished update the page. You can also use callbacks from the worker to sync up between worker and application. Happy to help with this.

Ken Fromm
  • 91
  • 2
  • 1
    Hi @stephan.com , this sample app here shows how to set and get progress: https://github.com/iron-io/heroku_sinatra_example See tweet_worker.rb for how it sets it and hello.rb for how it gets it. There's some ajax in hello.erb that shows how to display it in the UI. – Travis Reeder Apr 03 '12 at 08:10
0

where are you storing the result of the ruport generation? if you put it somewhere on disc, like an S3 storage, you could pregenerate the name of your report and hand that back to the client. than you can poll the S3 storage if the file is present and then stream it to the client once the creation finished.

EDIT

it looks like you get back the id of the job when putting it on the queue: polling with delayed_job

Community
  • 1
  • 1
phoet
  • 18,688
  • 4
  • 46
  • 74
  • well, yeah, I could - that's sort of what I meant by "I suppose could give them a table and an ActiveRecord model if that helped" - I could generate, say, a ReportGen record, hand back the ID of that new record, store the state and the filename there, and then the user can poll that. but (a) that sounds like more than I should have to do and (b) why can't I find any sample code of someone else doing something like that? I'd expect THAT record to have some sort of ID number for the job, so I can go kill/clean up hung jobs later? Where do I get that from, and why can't I just hand that back? – stephan.com Mar 07 '12 at 21:07