Search code examples
perlhttpmod-perlmod-perl2

Is there a mod_perl2/Perl 5 equivalent to PHP's ignore_user_abort()?


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.


Solution

  • 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();