You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Michael Schout <ms...@gkg.net> on 2007/12/20 20:09:37 UTC

register_cleanup and %ENV

Hi folks.

I'm in the midst of porting a fairly large application over from
mod_perl 1 to mod_perl 2.0.  The app I am currently working on runs as a
 "perl-script" handler using PerlResponseHandler My::Package::Name under
mod_perl 2.

One situation I have is that I sometimes have to do a "long running
process" that may take up to 1-2 minutes to complete.  Under MP1, I
registered a cleanup handler to do this, and sent the user to a "status"
page that would refresh every 5 seconds until the cleanup handler
finished and spit out the results (which the cleanup handler saved in
the db). Getting the cleanup registered and getting it to fire under
mod_perl 2 was simply a matter of changing $r->post_connection(sub { ...
}) to $r->pool->cleanup_register(sub { ... }). So far so good.

The problem with this is I an %ENV variable called "TEST_MODE", set at
startup time (from startup.pl) which enables certain behavior (e.g.:
allows fake credit card numbers, enables some extra logging statements
etc).  mod_perl 2 unfortunately scrubs %ENV before my cleanup handler
gets called, so $ENV{TEST_MODE} is never set when the cleanup handler
runs.  I don't really see any way to prevent this from happening from
what the docs say.  If anyone has any ideas on how to prevent %ENV from
getting wiped before my cleanup handler fires, or has any suggestions
about how to do a "long running process" under mod_perl 2 while
preserving %ENV from the request, I'd love to hear them ;).

Thanks
Michael Schout

Re: register_cleanup and %ENV

Posted by Michael Schout <ms...@gkg.net>.
Perrin Harkins wrote:

> Have you considered using your own global instead?  Or PerlSetVar in httpd.conf?

Yeah.  Unfortunately that really doesn't work for me.  I can get
TEST_MODE that way, but there are other  %ENV vars that I need also
(such as HTML_TEMPLATE_ROOT for example).  I did manage to get it
working by registering the cleanup like this:

my %renv = %ENV;
my $sub = sub {
  my %orig_env = %ENV;
  %ENV = %renv;  # copy request-time %ENV

  &$cleanup_func();

  %ENV = %orig_env; # restore %ENV
};
$r->pool->cleanup_register($sub);

So I just cheated and reset the environment before the cleanup function
is called.  I realize I shouldn't rely on %ENV under MP2 for thread
safety reasons, but this app is never going to run on anything other
than a prefork MPM and I can live with that :).

Regards,
Michael Schout

Re: register_cleanup and %ENV

Posted by Perrin Harkins <pe...@elem.com>.
On Dec 20, 2007 2:09 PM, Michael Schout <ms...@gkg.net> wrote:
> mod_perl 2 unfortunately scrubs %ENV before my cleanup handler
> gets called, so $ENV{TEST_MODE} is never set when the cleanup handler
> runs.

Have you considered using your own global instead?  Or PerlSetVar in httpd.conf?

- Perrin