Jim,


On donderdag, maa 31, 2005, at 21:06 Europe/Amsterdam, Jim wrote:

If you feel like giving me an example that accomplishes what I want to do
below, I'd be glad to use yours too. :-)

The concept is fairly simple: instead of connecting to your database in your 'client_input' state, push the input data at the end of a global list:


sub client_input {

# Get the syslog hash
my $msg = $_[ARG0];
my $databaseHandle;
my $connectFailureFlag = 0;
my $search;
my $sth;
my $dbName = "vpn";
# Parse the data for DB entry
$msg->{'msg'} =~ /^(.*?) (\d+)\/(\d+)\/(\d+) (\d+):(\d+):(\d+)\.(\d+)SEV=. (.*?)$/;


        ## Don't connect to db, just push it on the queue

        push @$queue, $msg;
}

And before the 'spawn' of POE::Component::Server::Syslog, create a new session:

my $queue;  ## global, holds queue elements

POE::Session->create(
        inline_states =>
        { _start => sub {
                $_[KERNEL]->delay( tick => 5 );
        },

        tick => 'doTick'
  );
);

sub doTick
{
        my ($kernel) = @_[KERNEL];

        ## connect to db

        ## write all elements in the queue to the database

        foreach my $element (@$queue) {
                ## $element->{msg} holds your message
        }

        ## disconnect from db

        ## All done now, empty queue

        undef $queue;

        $_[KERNEL]->delay(doTick => 5);
}

The delay will cause your doTick to be called every 5 seconds, so you only connect once every 5 seconds instead of connecting on each request.

You might just as well open the db connection when your tick session starts, and hold a reference to it in the heap of that session. Then you'd have to be prepared for the connection getting lost, and reconnect when it is lost.

Cheers,

Bas.

ps. read about delay here: http://poe.perl.org/?POE_Cookbook/Recurring_Alarms



Reply via email to