在Perl中寻求关于pre-forking master / worker守护进程实现的建议

时间:2011-12-10 22:39:34

标签: linux perl ipc daemon

我需要使用Perl实现一个守护进程来驱动网络服务监控探测器。

守护程序应预先分配已配置数量的工作程序和主进程,以从数据库中获取计划的探测,并将消息传递给将运行探测并将结果插入数据库的工作程序。从主人到工人的单向沟通应该足够了。

我使用Proc :: Daemon和IO :: Pipe为一对一的IPC玩过一些代码,但是每次尝试都以挫败感结束。对于它的价值,我没有任何例子可以呈现,他们可能只会分散真正的问题。

我的设计是否合理?

我已经看过几个POD页面和教程,涵盖了我的要求的一些部分,但没有一个填补了主从工作IPC的空白。任何人都可以提供指向可能有助于我了解如何实现这一目标的文章的链接吗?

1 个答案:

答案 0 :(得分:2)

听起来像HTTP的工作。为什么CPAN上有什么东西时才实现预分叉服务器?


master.pl ,来自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 ,使用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']];
}