in reply to YAML file update issue under mod_perl
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;
|
|---|