lihao has asked for the wisdom of the Perl Monks concerning the following question:

Hi, monks

I am using HTML::Mason + mod_perl + Apache2 under RHEL5, an YAML file is used for Marketing to maintain a complex data structure: a tree of questionnairs (HOH). The problem is that each time I update this YAML file, the change does not shown on live until I restart the Apache server. I tried:

the YAML file is loaded with YAML::Syck:

my $TREE = LoadFile('/path/to/tree.yml');

The problem sounds to be from the server side. Is there any method in the mod_perl enviromental that I can Reload the YAML file without restarting Apache? i.e. something like Apache2:Reload ReloadTouchFile on Perl files.

thanks

lihao

Update: Never mind, after more testings, I found putting the YAML::Syck::LoadFile line into <%init> tag instead of <%once> tag solved my problem. this turns out to be just a problem on how to use HTML::Mason properly. thanks again.

Replies are listed 'Best First'.
Re: YAML file update issue under mod_perl
by Anonymous Monk on Nov 07, 2010 at 00:56 UTC
    Your code is the problem, if you want your program to reload a file, you have to write a program which will do that. You can take three approaches
    • apache reload approach, which keeps stating the file to see if it has changed
    • scheduled approach, reload every 1/6/12/24 hours regardless
    • manual, tell your sever program to reload now, http://example.com/admin/ReloadFoo?ItsBeenUpdate
Re: YAML file update issue under mod_perl
by zwon (Abbot) on Nov 07, 2010 at 03:14 UTC

    Recently I had to solve similar task so maybe my solution will help you. I created singleton class, which loads YAML file during initialisation and every time you requesting structure it checks if file was changed, and reloading it if needed. Example code is follows, I would like to get any comments:

    package YAMLReload; use MooseX::Singleton; use namespace::autoclean; use YAML qw(LoadFile); has _fname => ( is => 'ro', default => '/path/to.yml' ); has _tree => ( is => 'rw' ); has _mtime => ( is => 'rw' ); has _checked => ( is => 'rw', default => sub { time } ); sub BUILD { my $self = shift; $self->_reload_tree; } sub _reload_tree { my $self = shift; $self->_mtime( ( stat( $self->_fname ) )[9] ); $self->_tree( LoadFile( $self->_fname ) ); } sub _check { my $self = shift; return if time - $self->_checked < 10; $self->_checked(time); return if $self->_mtime == ( stat( $self->_fname ) )[9]; $self->_reload_tree; } sub get_tree { my $self = shift; $self->_check; return $self->_tree; } no Moose; __PACKAGE__->meta->make_immutable; 1;