ControlFreak

 view release on metacpan or  search on metacpan

lib/ControlFreak/Proxy.pm  view on Meta::CPAN

a C<ControlFreak::Proxy> separate process that will:

=over 4

=item * load the application code once and for all

=item * take commands from the main C<controller> (over pipes)

=item * fork children when instructed, that exec some user defined commands

=back

=head1 SYNOPSIS

  $proxy = ControlFreak::Proxy->new(
      ctrl => $ctrl,
      cmd  => '/usr/bin/cfk-share-mem-proxy.pl --preload Some::Module',

  );
  $proxy->add_service($svc);
  $proxy->destroy_service($svc);
  $proxy->run;
  $proxy->start_service($svc);
  $proxy->stop_service($svc);
  @list = $proxy->services;
  $proxy->shutdown;
  $proxy->is_running;

=head1 METHODS

=head2 new(%param)

=cut

sub new {
    my $class = shift;
    my %param = @_;

    my $ctrl = $param{ctrl};
    unless ($ctrl) {
        warn "Proxy creation attempt without ctrl";
        return;
    }

    unless ($param{name}) {
        $ctrl->log->error("Proxy creation attempt without a name");
        return;
    }

    my $proxy = $class->SUPER::new(%param);
    $proxy->{ctrl} = $ctrl;
    $proxy->{servicemap} = {};
    $proxy->{env}  ||= {};
    unless (defined $param{auto}) {
        $proxy->{auto} = 1; # proxy is 'auto' by default
    }
    unless ($ctrl->add_proxy($proxy)) {
        $ctrl->log->error("A proxy by that name already exists");
        return;
    }
    Scalar::Util::weaken($proxy->{ctrl});
    return $proxy;
}

=head2 status_as_text

Returns the status of the proxy, including its eventual pid in one line of
text, where the following fields are seperated with tabs:

=over 4

=item * name

=item * status ('up' or 'down')

=item * pid, if proxy is up

=back

=cut

sub status {
    my $proxy = shift;
    return $proxy->is_running ? "up" : "down";
}

sub status_as_text {
    my $proxy = shift;
    return join "\t", map { $proxy->$_ || "" } qw/name status pid/;
}

sub _err { ControlFreak::Util::error(@_) }

=head2 services

Returns a list of L<ControlFreak::Service> objects related to the
proxy.

=cut
sub services {
    my $proxy = shift;
    return values %{ $proxy->{servicemap} };
}

=head2 add_service($svc)

Declares a service under the control of the proxy.

=cut

sub add_service {
    my $proxy = shift;
    my $svc   = shift;
    $proxy->{servicemap}->{$svc->name} = $svc;
    $svc->assign_proxy($proxy);
    return 1;
}

=head2 start_service

Given a L<ControlFreak::Service>, check that it is effectively



( run in 0.892 second using v1.01-cache-2.11-cpan-39bf76dae61 )