views:

131

answers:

2

I'm writing an internal service that needs to touch a mod_perl2 instance for a long-running-process. The job is fired from a HTTP POST, and them mod_perl handler picks it up and does the work. It could take a long time, and is ready to be handled asynchronously, so I was hoping I could terminate the HTTP connection while it is running.

PHP has a function ignore_user_abort(), that when combined with the right headers, can close the HTTP connection early, while leaving the process running (this technique is mentioned here on SO a few times).

Does Perl have an equivalent? I haven't been able to find one yet.

A: 

You may want to look at using a job queue for this. Here is one provided by Zend that will let you start background processing jobs. There should be a number of these to choose from for php and perl.

Here's another thread that talks about this problem and an article on some php options. I'm not perl monk, so I'll leave suggestions on those tools to others.

Dana the Sane
+2  A: 

Ok, I figured it out.

Mod_perl has the 'opposite' problem of PHP here. By default, mod_perl processes are left open, even if the connection is aborted, where PHP by default closes the process.

The Practical mod_perl book says how to deal with aborted connections.

(BTW, for the purposes of this specific problem, a job queue was lower on the list than a 'disconnecting' http process)

#setup headers
$r->content_type('text/html');
$s = some_sub_returns_string();

$r->connection->keepalive(Apache2::Const::CONN_CLOSE);
$r->headers_out()->{'Content-Length'} = length($s);

$r->print($s);
$r->rflush();

#
# !!! at this point, the connection will close to the client
#

#do long running stuff
do_long_running_sub();
Justin