David Jones
David Jones

Reputation: 19

Need perl script to continue after user browses away

I have a perl script that generated a PDF file. It takes a long time and I get gateway timeout error.

Many websites generate large files then notify user later when ready to download.

My script dies when user browses away.

How can I run the script then notify user when download is ready?

Thank you.

Upvotes: 0

Views: 78

Answers (1)

SpiceMan
SpiceMan

Reputation: 74

Bad answer: Increase the timeout value of the frontend.

Short answer (not really a solution): don't generate PDFs in that script.

Long answer: There are several approaches, but aim to the same: to let the program (ie: your script. lets call it "A") send a response to the client (such as "we are generating your PDF and will be notified") as fast as possible, while another program (lets call it "B") generates the PDF.

B can be a server, can be a script on an while loop polling something, can be a cronjob script executed every minute that checks if some state somewhere changed.

What you do at A largely depends on how you decide to implement B.

Some random ideas:

  1. program B waits listening on a TCP or UNIX socket, A connects, sends a message that includes the relevant data (such as where to look for the data for the PDF, where to save the PDF, the ID of the user), B generates the PDF, sends it by mail to the user it looked up on the DB by user id.
  2. program A inserts a record in a database with some kind of flag that means a PDF needs to be generated. program A displays "generating PDF" and reloads every 5 secs until the "generating PDF" flag is cleared. B is executed every minute by some kind of scheduler like cronjob. B generates the PDF, clears the flag. A sees the flag cleared, redirects to some "your file is available at link" or whatever.
  3. program A touches a file, program B gets the event through inotify, generates the PDF...

you get the idea.

Upvotes: 1

Related Questions