File Coverage

blib/lib/Supervisord/Client.pm
Criterion Covered Total %
statement 48 54 88.8
branch 3 10 30.0
condition 2 6 33.3
subroutine 14 14 100.0
pod 1 2 50.0
total 68 86 79.0


line stmt bran cond sub pod time code
1             package Supervisord::Client;
2             {
3             $Supervisord::Client::VERSION = '0.1_02';
4             }
5 1     1   47619 use strict;
  1         2  
  1         21  
6 1     1   3 use warnings;
  1         1  
  1         18  
7 1     1   402 use LWP::Protocol::http::SocketUnixAlt;
  1         57621  
  1         26  
8 1     1   465 use RPC::XML::Client;
  1         417127  
  1         30  
9 1     1   430 use Moo::Lax;
  1         6257  
  1         5  
10 1     1   1173 use Carp;
  1         1  
  1         42  
11 1     1   357 use Safe::Isa;
  1         288  
  1         85  
12 1     1   386 use Config::INI::Reader;
  1         7381  
  1         25  
13 1     1   5 use URI;
  1         1  
  1         348  
14              
15             LWP::Protocol::implementor(
16             supervisorsocketunix => 'LWP::Protocol::http::SocketUnixAlt' );
17              
18             has path_to_supervisor_config => (
19             is => 'ro',
20             required => 0,
21             );
22              
23             has serverurl => (
24             is => 'ro',
25             required => 0,
26             );
27              
28             has rpc => (
29             is => 'lazy',
30             handles => { ua => 'useragent' },
31             );
32             has _serverurl => ( is => 'lazy' );
33              
34             has username => (
35             is => 'ro',
36             required => 0,
37             );
38              
39             has password => (
40             is => 'ro',
41             required => 0,
42             );
43              
44              
45             sub _build__serverurl {
46 1     1   6 my $self = shift;
47 1 50       7 return $self->serverurl if $self->serverurl;
48 1         10 my $hash =
49             Config::INI::Reader->read_file( $self->path_to_supervisor_config );
50             return $hash->{supervisorctl}{serverurl}
51 1   33     1271 || croak "couldnt find serverurl in supervisorctl section of "
52             . $self->path_to_supervisor_config;
53             }
54              
55             sub _build_rpc {
56 1     1   7 my $self = shift;
57 1         16 my $url = $self->_serverurl;
58 1         8 my $uri = URI->new( $url );
59 1 50       4440 if( lc($uri->scheme) eq 'unix' ) {
60 1         76 my $socket_uri = URI->new("supervisorsocketunix:");
61 1         252 $socket_uri->path_segments( $uri->path_segments, "", "RPC2" );
62 1         85 $uri = $socket_uri;
63             } else {
64 0         0 $uri->path_segments( $uri->path_segments, "RPC2" );
65             }
66 1         32 my $cli = RPC::XML::Client->new( $uri );
67 1         5406 my $ua = $cli->useragent;
68 0 0       0 if( $self->username ) {
69 0         0 $ua->credentials( $uri->host_port, 'default', $self->username, $self->password );
70             }
71 0         0 return $cli;
72             }
73              
74             sub BUILD {
75 1     1 0 2888 my $self = shift;
76 1 50 33     13 $self->path_to_supervisor_config
77             || $self->serverurl
78             || croak "path_to_supervisor_config or serverurl required.";
79             }
80              
81             our $AUTOLOAD;
82              
83             sub AUTOLOAD {
84 1     1   454 my $remote_method = $AUTOLOAD;
85 1         4 $remote_method =~ s/.*:://;
86 1         3 my ( $self, @args ) = @_;
87 1         4 $self->send_rpc_request("supervisor.$remote_method", @args );
88             }
89             sub send_rpc_request {
90 1     1 1 2 my( $self, @params ) = @_;
91 1         22 my $ret = $self->rpc->send_request( @params );
92 0 0         return $ret->value if $ret->$_can("value");
93 0           return $ret;
94             }
95              
96             1;
97              
98             =head1 NAME
99              
100             Supervisord::Client - a perl client for Supervisord's XMLRPC.
101              
102             =head1 SYNOPSIS
103              
104             my $client = Supervisord::Client->new( serverurl => "unix:///tmp/socky.sock" );
105             #or
106             my $client = Supervisord::Client->new( serverurl => "http://foo.bar:25123" );
107             #or
108             my $client = Supervisord::Client->new( path_to_supervisor_config => "/etc/supervisor/supervisor.conf" );
109             warn $_->{description} for(@{ $client->getAllProcessInfo });
110             #or
111             warn $_->{description} for(@{ $client->send_rpc_request("supervisor.getAllProcessInfo") });
112              
113             =head1 DESCRIPTION
114              
115             This module is for people who are using supervisord ( L<http://supervisord.org/> ) to manage their daemons,
116             and ran into problems with the http over Unix socket part.
117              
118             See L<http://supervisord.org/api.html> for the API docs of what the supervisord XMLRPC supports.
119              
120             =head1 METHODS
121              
122             =head2 new
123              
124             Constructor, provided by Moo.
125              
126             =head2 rpc
127              
128             Access to the RPC::XML::Client object.
129              
130             =item ua
131              
132             Access to the LWP::UserAgent object from the RPC::XML::Client
133              
134             =head2 send_rpc_request( remote_method, @params )
135              
136              
137             =head2 AUTOLOAD
138              
139             This module uses AUTOLOAD to proxy calls to send_rpc_request. See synopsis for examples.
140              
141             =head1 CONSTRUCTOR PARAMETERS
142              
143             path_to_supervisor_config or serverurl is required.
144              
145             =over
146              
147             =item path_to_supervisor_config
148              
149             optional - ex: /tmp/super.sock
150              
151             =item serverurl
152              
153             optional - in supervisor format, ex: unix:///tmp.super.sock | http://myserver.local:8080
154              
155              
156              
157             =back
158              
159             =head1 LICENSE
160              
161             This library is free software and may be distributed under the same terms as perl itself.
162              
163             =head1 AUTHOR
164              
165             Samuel Kaufman L<skaufman@cpan.org>
166