« lib/POE/Component/MessageQueue/IDGenerator/SimpleInt.pm
↑ Summary
lib/POE/Component/MessageQueue/Storage/Generic/DBI.pm »
| 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 | 3 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 21 | 3 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 22 | 3 | ▶ | use POE; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 23 | 3 | ▶ | use IO::Handle; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | sub spawn | ||||||||||||||||||||
| 26 | { | ||||||||||||||||||||
| 27 | 0 | ▶ | my $class = shift; | ||||||||||||||||||
|
Subroutine
spawn: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 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 | ▶ | $_[KERNEL]->alias_set( $self->{alias} ); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 40 | 0 | $_[KERNEL]->yield('publish'); | |||||||||||||||||||
| 41 | }, | ||||||||||||||||||||
| 42 | 'publish' => sub { | ||||||||||||||||||||
| 43 | # gets called on a regular interval | ||||||||||||||||||||
| 44 | 0 | ▶ | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 45 | 0 | my $alarm = $heap->{publish_alarm}; | |||||||||||||||||||
| 46 | 0 | ▶ | $kernel->alarm_remove($alarm) if $alarm; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 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 | ▶ | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 55 | 0 | my $alarm = $heap->{publish_alarm}; | |||||||||||||||||||
| 56 | 0 | ▶ | $kernel->alarm_remove($alarm) if $alarm; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | $self->publish(); | |||||||||||||||||||
| 58 | # Cut off circular references. | ||||||||||||||||||||
| 59 | 0 | delete @{$self}{qw(statistics session)}; | |||||||||||||||||||
| 60 | }, | ||||||||||||||||||||
| 61 | } | ||||||||||||||||||||
| 62 | 0 | ); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | sub new | ||||||||||||||||||||
| 66 | { | ||||||||||||||||||||
| 67 | 0 | ▶ | my $class = shift; | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 68 | 0 | my $self = bless { @_ }, $class; # XXX - hack | |||||||||||||||||||
| 69 | 0 | $self; | |||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | |||||||||||||||||||||
| 72 | sub publish | ||||||||||||||||||||
| 73 | { | ||||||||||||||||||||
| 74 | 0 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
publish: not called CC=5 SCAR=34.0
Pod
documented
| |||||||||||||||||||||
| 75 | 0 | my $output = $self->{output}; | |||||||||||||||||||
| 76 | 0 | my $ref = ref $output; | |||||||||||||||||||
| 77 | |||||||||||||||||||||
| 78 | 0 | ▶ | if (! $ref) { # simple string. a filename | ||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||
| 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')) { | |||||||||||||||||||
|
Condition0%0/3
$ref eq "GLOB" or $output->can("print")
MC/DC0%0/2
$ref eq "GLOB" or $output->can("print")
$ref eq "GLOB"
$output->can("print")
Truth table0%0/3
A or B
A= $ref eq "GLOB"B= $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 | ▶ | my $self = shift; | ||||||||||||||||||
|
Subroutine
shutdown: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 97 | 0 | POE::Kernel->post($self->{session}, 'shutdown'); | |||||||||||||||||||
| 98 | } | ||||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 3 | 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 | ||||||||||||||||||||