You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Rolf Schaufelberger <rs...@plusw.de> on 2007/12/12 20:05:13 UTC

register_cleanup

Hi, 

I'm on the way moving my app from mod_perl1 to mod_perl2.
The app is build with HTML::Mason an MasonX::WebApp.
Now I have a problem when trying to install a cleanup_handler.
My expectations are, that the funtcion I call returns to the browser and move 
some long time computing to a cleanup_handler.
The code I use (sample):

================
use APR::Pool (); 
 
sub cleanup { 
  my $arg = shift; 
  for (1 .. $arg->{cnt} ) { 
    print STDERR "$_ = ", $arg->{name}, "\n"; 
    sleep(1); 
  } 
} 
 
sub T_cleanup { 
  my $self= shift; 
 
 # either
  my $r = $self->{__apache_req__}; 
  $r->pool->cleanup_register (\&cleanup, {name=> 'test', cnt=>10}); 

# nor this works
#   my $p = APR::Pool->new();  
#  $p->cleanup_register (\&cleanup, {name=> 'test', cnt=>10}); 
 
  $self->redirect(path=>'/index.html'); 
} 
=================

So when I call my method T_cleanup, the redirect is done AFTER cleanup has 
counted down , i.e. after 10 seconds. I would expect that it returns at once, 
an cleanup starts working AFTER the redirect. 
The docs says, it would start the executen "after the pool goes out of scope", 
which should be after the request object gets detroyed. Right ? 

So what am I doing wrong ?

-- 
Rolf Schaufelberger
www.plusw.de


Re: register_cleanup

Posted by Torsten Foertsch <to...@gmx.net>.
On Wed 12 Dec 2007, Rolf Schaufelberger wrote:
> I'm on the way moving my app from mod_perl1 to mod_perl2.
> The app is build with HTML::Mason an MasonX::WebApp.
> Now I have a problem when trying to install a cleanup_handler.
> My expectations are, that the funtcion I call returns to the browser and
> move some long time computing to a cleanup_handler.
> The code I use (sample):
>
> ================
> use APR::Pool ();
>  
> sub cleanup {
>   my $arg = shift;
>   for (1 .. $arg->{cnt} ) {
>     print STDERR "$_ = ", $arg->{name}, "\n";
>     sleep(1);
>   }
> }
>  
> sub T_cleanup {
>   my $self= shift;
>  
>  # either
>   my $r = $self->{__apache_req__};
>   $r->pool->cleanup_register (\&cleanup, {name=> 'test', cnt=>10});

This should work, see below.

> # nor this works
> #   my $p = APR::Pool->new();  
> #  $p->cleanup_register (\&cleanup, {name=> 'test', cnt=>10});

and it should not. $p is destroyed at the end of the subroutine. Hence the 
cleanup is run at that time, that means before Perl returns to mod_perl.

>   $self->redirect(path=>'/index.html');
> }
> =================
>
> So when I call my method T_cleanup, the redirect is done AFTER cleanup has
> counted down , i.e. after 10 seconds. I would expect that it returns at
> once, an cleanup starts working AFTER the redirect.
> The docs says, it would start the executen "after the pool goes out of
> scope", which should be after the request object gets detroyed. Right ?
>
> So what am I doing wrong ?

This one works for me:

<Perl>
package My::Test;
use strict;
use Apache2::RequestRec ();
use APR::Pool ();
use Apache2::Const qw/-compile OK REDIRECT/;

sub cleanup { 
  my $arg = shift; 
  for (1 .. $arg->{cnt} ) { 
    print STDERR "$_ = ", $arg->{name}, "\n"; 
    sleep(1); 
  } 
}
sub handler {
  my $r=$_[0];

  $r->pool->cleanup_register(\&cleanup, {name=> 'test', cnt=>10});
  $r->headers_out->{Location}='http://blah.blah/';
  return Apache2::Const::REDIRECT;
}
</Perl>
<Location /cleanup-test>
SetHandler modperl
PerlResponseHandler My::Test
</Location>


Torsten

--
A: It reverses the normal flow of conversation.
Q: What's wrong with top-posting?
A: Top-posting.
Q: What's the biggest scourge on plain text email discussions?

Re: register_cleanup

Posted by Jonathan Vanasco <jv...@2xlp.com>.
I'm surprised you're even getting a redirect, this doesn't make sense  
to me.   the MP cleanup handler is supposed to happen after the  
request is served / client connection is terminated (docs below)

if you're trying to do a redirect after processing, try a stacked  
handler

http://perl.apache.org/docs/2.0/user/handlers/ 
http.html#PerlCleanupHandler
	There is no cleanup Apache phase, it exists only inside mod_perl. It  
is used to execute some code immediately after the request has been  
served (the client went away) and before the request object is  
destroyed.
	There are several usages for this use phase. The obvious one is to  
run a cleanup code, for example removing temporarily created files.  
The less obvious is to use this phase instead of PerlLogHandler if  
the logging operation is time consuming. This approach allows to free  
the client as soon as the response is sent.




On Dec 13, 2007, at 4:18 PM, Rolf Schaufelberger wrote:

> On Wednesday 12 December 2007 23:44:40 Perrin Harkins wrote:
>> On Dec 12, 2007 2:05 PM, Rolf Schaufelberger <rs...@plusw.de> wrote:
>>>   my $r = $self->{__apache_req__};
>>
>> Yikes, be careful of storing Apache2::RequestRec objects.  Terrible
>> things will happen if you try to access one from a previous request.
>>
>>>   $r->pool->cleanup_register (\&cleanup, {name=> 'test', cnt=>10});
>>
>> Try $r->push_handlers(PerlCleanupHandler => \&cleanup);
>>
>> - Perrin
>
> tried this, same result (redirect takes place after cleanup  
> function has
> finished).
>
> -- 
> Rolf Schaufelberger


Re: register_cleanup

Posted by Rolf Schaufelberger <rs...@plusw.de>.
On Wednesday 12 December 2007 23:44:40 Perrin Harkins wrote:
> On Dec 12, 2007 2:05 PM, Rolf Schaufelberger <rs...@plusw.de> wrote:
> >   my $r = $self->{__apache_req__};
>
> Yikes, be careful of storing Apache2::RequestRec objects.  Terrible
> things will happen if you try to access one from a previous request.
>
> >   $r->pool->cleanup_register (\&cleanup, {name=> 'test', cnt=>10});
>
> Try $r->push_handlers(PerlCleanupHandler => \&cleanup);
>
> - Perrin

tried this, same result (redirect takes place after cleanup function has 
finished). 

-- 
Rolf Schaufelberger

Re: register_cleanup

Posted by Perrin Harkins <pe...@elem.com>.
On Dec 12, 2007 2:05 PM, Rolf Schaufelberger <rs...@plusw.de> wrote:
>   my $r = $self->{__apache_req__};

Yikes, be careful of storing Apache2::RequestRec objects.  Terrible
things will happen if you try to access one from a previous request.

>   $r->pool->cleanup_register (\&cleanup, {name=> 'test', cnt=>10});

Try $r->push_handlers(PerlCleanupHandler => \&cleanup);

- Perrin