File Coverage

blib/lib/POE/Component/MessageQueue/Statistics/Publish.pm
Criterion Covered Total %
statement 12 43 27.9
branch 0 10 0.0
condition 0 3 0.0
subroutine 4 11 36.3
pod 2 4 50.0
total 18 71 25.3


line stmt bran cond sub pod time code
1             # $Id$
2             #
3             # Copyright (c) 2007 Daisuke Maki <daisuke@endeworks.jp>
4             # All rights reserved.
5             #
6             # This program is free software: you can redistribute it and/or modify
7             # it under the terms of the GNU General Public License as published by
8             # the Free Software Foundation, either version 2 of the License, or
9             # (at your option) any later version.
10             #
11             # This program is distributed in the hope that it will be useful,
12             # but WITHOUT ANY WARRANTY; without even the implied warranty of
13             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14             # GNU General Public License for more details.
15             #
16             # You should have received a copy of the GNU General Public License
17             # along with this program. If not, see <http://www.gnu.org/licenses/>.
18              
19             package POE::Component::MessageQueue::Statistics::Publish;
20 1     1   8 use strict;
  1         2  
  1         23  
21 1     1   5 use warnings;
  1         40  
  1         38  
22 1     1   7 use POE;
  1         3  
  1         8  
23 1     1   327 use IO::Handle;
  1         2  
  1         350  
24              
25             sub spawn
26             {
27 0     0 0   my $class = shift;
28 0           my $self = $class->new(
29             alias => 'MQ-Publish',
30             interval => 10,
31             @_
32             );
33              
34 0           $self->{statistics}->add_publisher($self);
35             $self->{session} = POE::Session->create(
36             heap => {},
37             inline_states => {
38             '_start' => sub {
39 0     0     $_[KERNEL]->alias_set( $self->{alias} );
40 0           $_[KERNEL]->yield('publish');
41             },
42             'publish' => sub {
43             # gets called on a regular interval
44 0     0     my ($kernel, $heap) = @_[KERNEL, HEAP];
45 0           my $alarm = $heap->{publish_alarm};
46 0 0         $kernel->alarm_remove($alarm) if $alarm;
47              
48 0           $self->publish();
49              
50             $heap->{publish_alarm} =
51 0           $kernel->alarm_set('publish', time() + $self->{interval});
52             },
53             'shutdown' => sub {
54 0     0     my ($kernel, $heap) = @_[KERNEL, HEAP];
55 0           my $alarm = $heap->{publish_alarm};
56 0 0         $kernel->alarm_remove($alarm) if $alarm;
57 0           $self->publish();
58             # Cut off circular references.
59 0           delete @{$self}{qw(statistics session)};
  0            
60             },
61             }
62 0           );
63             }
64              
65             sub new
66             {
67 0     0 1   my $class = shift;
68 0           my $self = bless { @_ }, $class; # XXX - hack
69 0           $self;
70             }
71              
72             sub publish
73             {
74 0     0 1   my $self = shift;
75 0           my $output = $self->{output};
76 0           my $ref = ref $output;
77              
78 0 0 0       if (! $ref) { # simple string. a filename
    0          
    0          
79 0           $self->publish_file( $output );
80             } elsif ($ref eq 'CODE') {
81 0           $self->publish_code( $output );
82             } elsif ($ref eq 'GLOB' || $output->can('print')) {
83 0           $self->publish_handle( $output );
84             } else {
85             # don't know what it is. subclasses may detect that we were
86             # unable to determine the output type by checking for flase
87             # return values from this subroutine
88 0           return ();
89             }
90              
91 0           return 1;
92             }
93              
94             sub shutdown
95             {
96 0     0 0   my $self = shift;
97 0           POE::Kernel->post($self->{session}, 'shutdown');
98             }
99              
100             1;
101              
102             __END__
103              
104             =head1 NAME
105              
106             POE::Component::MessageQueue::Statistics::Publish - Base Statistics Publish Class
107              
108             =head1 METHODS
109              
110             =head2 new(%args)
111              
112             Creates a new instance. You must pass in an instance of POE::Component::MessageQueue::Statistics, and an output destination
113              
114             # initialized elsewhere
115             my $stats = POE::Component::MessageQueue::Statistics->new;
116              
117             my $publish = POE::Component::MessageQueue::Statistics::Publish::YAML->new(
118             output => \*STDERR,
119             statistics => $stats,
120             interval => 10, # dump every 10 seconds
121             );
122              
123             =head2 publish()
124              
125             Publishes the current state of the statistics. This is actually a dispatcher
126             that dispatches to the appropriate method calls (described below) that are
127             specific to a particular output type.
128              
129             Your subclass should implement the appropriate methods (output types) that
130             you want to support.
131              
132             =head2 publish_file($filename)
133              
134             Receives a filename to dump the statistics.
135              
136             =head2 publish_handle($handle)
137              
138             Receives a handle to dump the statistics.
139              
140             =head2 publish_code($code)
141              
142             Receives a subroutine reference. Your code should simply pass the result
143             output to $code and execute it:
144              
145             sub publish_code
146             {
147             my ($self, $code) = @_;
148             my $output = ....; # generate output here
149             $code->( $output );
150             }
151              
152             =head1 SEE ALSO
153              
154             L<POE::Component::MessageQueue::Statistics>,
155             L<POE::Component::MessageQueue::Statistics::Publish::YAML>
156              
157             =head1 AUTHOR
158              
159             Daisuke Maki E<lt>daisuke@endeworks.jpE<gt>
160              
161             =cut