Re: Apache2::Reload problems

[prev] [thread] [next] [Date index for 2005/05/25]

From: Stephane GUIBOUD-RIBAUD
Subject: Re: Apache2::Reload problems
Date: 06:48 on 25 May 2005
This is a multi-part message in MIME format.
--------------070903060404090004020404
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 8bit

Hi Mark,

I had the same problem as you when installed the 2.0.0-RC6.
I patched the Apache/Reload.pm (version 0.07) as follow:

1/ Changed the string ${package}::FIELDS by 
${package}::APACHE_RELOAD_FIELDS (line 43, 44, 90, 92)
2/ Changed the return value of the 'handler' function to return 0 
(instead of 1) [line 139].

You will find in attachment the version I used.

I don't know if the first modification has a real impact on the error, 
but it works like this for me.

Stephane

Mark wrote:

> I have a typical situation, with a Registry script that use's
> a bunch of modules.  After Apache::Reload reloads the first module,
> I get errors suggesting that the other loaded modules have been lost.
>
> To illustrate, it is like this (fake example):
>
> Script:
>
>    #!/usr/bin/perl
>
>   use Fubar;
>   use Snafu;
>
>   $f = Fubar->new()
>   $s = Snafu->new()
>
>
> I run once, then 'touch /path/to/Fubar.pm'
>
> Then I run from client, and I get error:
>
>    Can't locate object method "new" via package "Snafu" at ...
>
>
> Makes no sense to me?  Any ideas?    Same code on Apache 1.3 no problem.
> My Reload config is:
>
> PerlModule Apache2::Reload
> PerlInitHandler Apache2::Reload
> PerlSetVar ReloadAll Off
> PerlSetVar ReloadModules "Fubar"
> PerlSetVar ReloadDebug On
>
> And I also have both Fubar and Snafu in my Apache PerlRequire file.
>
> Thanks.
>
>
>
>


        -- 
        Stéphane GUIBOUD-RIBAUD                            STMicroelectronics
HPC/PMG/AP3                                                  Grenoble
Tel.: +33 (4) 76 585 246                               TINA: 041 5246


--------------070903060404090004020404
Content-Type: text/plain;
 name="Reload.pm"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="Reload.pm"

# $Id: Reload.pm,v 1.16 2001/04/22 18:09:59 matt Exp $

package Apache::Reload;

use strict;

$Apache::Reload::VERSION = '0.07.1';

use vars qw(%INCS %Stat $TouchTime %UndefFields);

%Stat = ($INC{"Apache/Reload.pm"} => time);

$TouchTime = time;

sub import {
    my $class = shift;
    my ($package,$file) = (caller)[0,1];
    
    $class->register_module($package, $file);
}

sub package_to_module {
    my $package = shift;
    $package =~ s/::/\//g;
    $package .= ".pm";
    return $package;
}

sub register_module {
    my ($class, $package, $file) = @_;
    my $module = package_to_module($package);
    
    if ($file) {
        $INCS{$module} = $file;
    }
    else {
        $file = $INC{$module};
        return unless $file;
        $INCS{$module} = $file;
    }
    
    no strict 'refs';
    if (%{"${package}::APACHE_RELOAD_FIELDS"}) {
        $UndefFields{$module} = "${package}::APACHE_RELOAD_FIELDS";
    }
}

sub handler {
    my $r = shift;
    
    my $DEBUG = ref($r) && (lc($r->dir_config("ReloadDebug") || '') eq 'on');
    
    my $TouchFile = ref($r) && $r->dir_config("ReloadTouchFile");
    
    my $TouchModules;
    
    if ($TouchFile) {
        warn "Checking mtime of $TouchFile\n" if $DEBUG;
        my $touch_mtime = (stat($TouchFile))[9] || return 1;
        return 1 unless $touch_mtime > $TouchTime;
        $TouchTime = $touch_mtime;
        my $sym = Apache->gensym;
        open($sym, $TouchFile) || die "Can't open '$TouchFile': $!";
        $TouchModules = <$sym>;
        chomp $TouchModules;
    }
    
    if (ref($r) && (lc($r->dir_config("ReloadAll") || 'on') eq 'on')) {
        *Apache::Reload::INCS = \%INC;
    }
    else {
        *Apache::Reload::INCS = \%INCS;
        my $ExtraList = 
                $TouchModules || 
                (ref($r) && $r->dir_config("ReloadModules")) || 
                '';
        my @extra = split(/\s+/, $ExtraList);
        foreach (@extra) {
            if (/(.*)::\*$/) {
                my $prefix = $1;
                $prefix =~ s/::/\//g;
                foreach my $match (keys %INC) {
                    if ($match =~ /^\Q$prefix\E/) {
                        $Apache::Reload::INCS{$match} = $INC{$match};
                        my $package = $match;
                        $package =~ s/\//::/g;
                        $package =~ s/\.pm$//;
                        no strict 'refs';
#                        warn "checking for FIELDS on $package\n";
                        if (%{"${package}::APACHE_RELOAD_FIELDS"}) {
#                            warn "found fields in $package\n";
                            $UndefFields{$match} = "${package}::APACHE_RELOAD_FIELDS";
                        }
                    }
                }
            }
            else {
                Apache::Reload->register_module($_);
            }
        }
    }
    
    
    while (my($key, $file) = each %Apache::Reload::INCS) {
        local $^W;
        warn "Apache::Reload: Checking mtime of $key\n" if $DEBUG;
        
        my $mtime = (stat $file)[9];

        unless (defined($mtime) && $mtime) {
            for (@INC) {
                $mtime = (stat "$_/$file")[9];
                last if defined($mtime) && $mtime;
            }
        }

        warn("Apache::Reload: Can't locate $file\n"),next 
                unless defined $mtime and $mtime;
        
        unless (defined $Stat{$file}) {
            $Stat{$file} = $^T;
        }
        
        if ($mtime > $Stat{$file}) {
            delete $INC{$key};
 #           warn "Reloading $key\n";
            if (my $symref = $UndefFields{$key}) {
#                warn "undeffing fields\n";
                no strict 'refs';
                undef %{$symref};
            }
            require $key;
            warn("Apache::Reload: process $$ reloading $key\n")
                    if $DEBUG;
        }
        $Stat{$file} = $mtime;
    }
    
    return 0;
}

1;
__END__

=head1 NAME

Apache::Reload - Reload changed modules

=head1 SYNOPSIS

In httpd.conf:

  PerlInitHandler Apache::Reload
  PerlSetVar ReloadAll Off

Then your module:

  package My::Apache::Module;

  use Apache::Reload;

  sub handler { ... }

  1;

=head1 DESCRIPTION

This module is two things. First it is an adaptation of Randal
Schwartz's Stonehenge::Reload module that attempts to be a little
more intuitive and makes the usage easier. Stonehenge::Reload was
written by Randal to make specific modules reload themselves when
they changed. Unlike Apache::StatINC, Stonehenge::Reload only checked
the change time of modules that registered themselves with
Stonehenge::Reload, thus reducing stat() calls. Apache::Reload also
offers the exact same functionality as Apache::StatINC, and is thus
designed to be a drop-in replacement. Apache::Reload only checks modules
that register themselves with Apache::Reload if you explicitly turn off
the StatINC emulation method (see below). Like Apache::StatINC,
Apache::Reload must be installed as an Init Handler.

=head2 StatINC Replacement

To use as a StatINC replacement, simply add the following configuration
to your httpd.conf:

  PerlInitHandler Apache::Reload

=head2 Register Modules Implicitly

To only reload modules that have registered with Apache::Reload,
add the following to the httpd.conf:

  PerlInitHandler Apache::Reload
  PerlSetVar ReloadAll Off
  # ReloadAll defaults to On

Then any modules with the line:

  use Apache::Reload;

Will be reloaded when they change.

=head2 Register Modules Explicitly

You can also register modules explicitly in your httpd.conf file that
you want to be reloaded on change:

  PerlInitHandler Apache::Reload
  PerlSetVar ReloadAll Off
  PerlSetVar ReloadModules "My::Foo My::Bar Foo::Bar::Test"

Note that these are split on whitespace, but the module list B<must>
be in quotes, otherwise Apache tries to parse the parameter list.

=head2 Special "Touch" File

You can also set a file that you can touch() that causes the reloads to be
performed. If you set this, and don't touch() the file, the reloads don't
happen. This can be a great boon in a live environment:

  PerlSetVar ReloadTouchFile /tmp/reload_modules

Now when you're happy with your changes, simply go to the command line and
type:

  touch /tmp/reload_modules

And your modules will be magically reloaded on the next request. This option
works in both StatINC emulation mode and the registered modules mode.

=head1 PSUEDOHASHES

The short summary of this is: Don't use psuedohashes. Use an array with
constant indexes. Its faster in the general case, its more guaranteed, and
generally, it works.

The long summary is that I've done some work to get this working with
modules that use psuedo hashes, but its still broken in the case of a
single module that contains multiple packages that all use psuedohashes.

So don't do that.

=head1 AUTHOR

Matt Sergeant, matt@xxxxxxxx.xxx

=head1 SEE ALSO

Apache::StatINC, Stonehenge::Reload

=cut

--------------070903060404090004020404--

(message missing)

Apache2::Reload problems
Mark 06:42 on 25 May 2005

Re: Apache2::Reload problems
Stephane GUIBOUD-RIBAUD 06:48 on 25 May 2005

Re: Apache2::Reload problems
Mark 15:53 on 25 May 2005

Re: Apache2::Reload problems
Stas Bekman 17:37 on 25 May 2005

Should I use Package or Modual or what?
Luinrandir Insight 17:19 on 25 May 2005

Re: Should I use Package or Modual or what?
Perrin Harkins 17:16 on 25 May 2005

Re: Should I use Package or Modual or what?
Perrin Harkins 17:57 on 25 May 2005

a mystery.. need help
Luinrandir Insight 08:36 on 29 May 2005

[OT] Re: a mystery.. need help
Frank Maas 10:37 on 29 May 2005

Re: Apache2::Reload problems
Philippe M. Chiasson 09:24 on 25 May 2005

Re: Apache2::Reload problems
Mark 15:30 on 25 May 2005

Re: a mystery.. need help
David Dick 05:37 on 29 May 2005

Re: a mystery.. need help
Luinrandir Insight 02:10 on 30 May 2005

Re: a mystery.. need help
Tom Schindl 07:55 on 30 May 2005

Re: a mystery.. need help
jonathan vanasco 16:23 on 30 May 2005

Re: a mystery.. need help
Luinrandir Insight 20:15 on 30 May 2005

[MP2] How to turn off caching?
Foo Ji-Haw 04:23 on 31 May 2005

Re: [MP2] How to turn off caching?
Rodger Castle 05:11 on 31 May 2005

Re: [MP2] How to turn off caching?
Foo Ji-Haw 07:45 on 31 May 2005

Re: [MP2] How to turn off caching?
Torsten Foertsch 09:31 on 31 May 2005

Re: [MP2] How to turn off caching?
Issac Goldstand 08:50 on 31 May 2005

Generated at 20:12 on 05 Jun 2005 by mariachi v0.52