File Coverage

blib/lib/Supervisord/Client.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


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