converter42
converter42

Reputation: 7526

Seeking advice on implementation of pre-forking master/worker daemon in Perl

I need to implement a daemon to drive network service monitoring probes using Perl.

The daemon should pre-fork a configured number of workers and a master process to fetch scheduled probes from the database and pass messages to the workers who will run the probes and insert the results into the database. One-way communication from master to worker should be sufficient.

I've played with some code using Proc::Daemon and IO::Pipe for master-to-worker IPC, but every attempt has ended in frustration. For what it's worth, I have no examples to present and they would probably only distract from the real question anyway.

Is my design, such as it is, sound?

I've seen several POD pages and tutorials covering bits and pieces of my requirements, but none that filled in the blanks on master-to-worker IPC. Can anyone offer links to articles that might help me understand how to implement this?

Upvotes: 1

Views: 338

Answers (1)

daxim
daxim

Reputation: 39158

Sounds like a job for HTTP. Why implement a pre-forking server when there is something on CPAN already?


master.pl, schedule from cron

use LWP::UserAgent;
sub fetch_probes { ... };

my %probe_data = fetch_probes;
my $ua = LWP::UserAgent->new;
my $res = $ua->post('http://localhost:5000', \%probe_data);
die $res->status_line unless $res->is_success;

worker.psgi, start app with starman --workers 32 --listen :5000

use Plack::Request;

sub run_probe { ... };
sub insert_db { ... }; # DBIx::Class magic goes here!

my $app = sub {
    my ($env) = @_;
    my $req = Plack::Request->new($env);
    my %probe_data = %{ $req->parameters };
    my $results = run_probe(%probe_data);
    return [200, ['Content-Type' => 'text/plain'], ['ok']] if insert_db($results);
    return [500, ['Content-Type' => 'text/plain'], ['the error message']];
}

Upvotes: 2

Related Questions