| 1 | package Log::Handler::Output::Gearman; | ||
| 2 | |||
| 3 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 3 | ▶ | use Carp::Clan qw(^Log::Handler); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Gearman::XS::Client; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Gearman::XS qw(:constants); | ||
| 8 | use Params::Validate; | ||
| 9 | |||
| 10 | our $VERSION = '0.01003'; | ||
| 11 | our $ERRSTR = ''; | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | Log::Handler::Output::Gearman - Send log messages to Gearman workers. | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | use Log::Handler::Output::Gearman; | ||
| 20 | |||
| 21 | my $logger = Log::Handler::Output::Gearman->new( | ||
| 22 | servers => ['127.0.0.1:4731'], | ||
| 23 | worker => 'logger', | ||
| 24 | ); | ||
| 25 | |||
| 26 | my $message = 'This is a log message'; | ||
| 27 | $logger->log( $message ); | ||
| 28 | |||
| 29 | =head1 DESCRIPTION | ||
| 30 | |||
| 31 | B<This is experimental ( beta ) and should only be used in a test environment. The | ||
| 32 | API may change at any time without prior notification until this message is removed!> | ||
| 33 | |||
| 34 | =head1 METHODS | ||
| 35 | |||
| 36 | =head2 new | ||
| 37 | |||
| 38 | Takes a number of arguments, following are B<mandatory>: | ||
| 39 | |||
| 40 | =over 4 | ||
| 41 | |||
| 42 | =item * | ||
| 43 | |||
| 44 | servers | ||
| 45 | |||
| 46 | # hostname:port gearmand is running on | ||
| 47 | servers => [ | ||
| 48 | '127.0.0.1:4731', | ||
| 49 | '192.168.0.1:4735', | ||
| 50 | '192.168.0.2' # uses default port (4730) | ||
| 51 | ] | ||
| 52 | |||
| 53 | =item * | ||
| 54 | |||
| 55 | worker | ||
| 56 | |||
| 57 | # name of the worker that should process the log messages | ||
| 58 | worker => 'logger' | ||
| 59 | |||
| 60 | =back | ||
| 61 | |||
| 62 | Besides it takes also following B<optional> arguments: | ||
| 63 | |||
| 64 | =over 4 | ||
| 65 | |||
| 66 | =item * | ||
| 67 | |||
| 68 | method (default: do_background) | ||
| 69 | |||
| 70 | method => 'do_high_background' | ||
| 71 | |||
| 72 | This can be one of the following L<Gearman::XS::Client> methods: | ||
| 73 | |||
| 74 | =over 4 | ||
| 75 | |||
| 76 | =item * C<do> | ||
| 77 | |||
| 78 | =item * C<do_high> | ||
| 79 | |||
| 80 | =item * C<do_low> | ||
| 81 | |||
| 82 | =item * C<do_background> | ||
| 83 | |||
| 84 | =item * C<do_high_background> | ||
| 85 | |||
| 86 | =item * C<do_low_background> | ||
| 87 | |||
| 88 | =back | ||
| 89 | |||
| 90 | =back | ||
| 91 | |||
| 92 | =cut | ||
| 93 | |||
| 94 | sub new { | ||
| 95 | my $package = shift; | ||
| 96 | |||
| 97 | my %options = $package->_validate(@_); | ||
| 98 | |||
| 99 | my $self = bless \%options, $package; | ||
| 100 | |||
| 101 | $self->_setup_gearman; | ||
| 102 | |||
| 103 | return $self; | ||
| 104 | } | ||
| 105 | |||
| 106 | =head2 log | ||
| 107 | |||
| 108 | Takes one argument: | ||
| 109 | |||
| 110 | =over 4 | ||
| 111 | |||
| 112 | =item * | ||
| 113 | |||
| 114 | C<$message> - The log message | ||
| 115 | |||
| 116 | =back | ||
| 117 | |||
| 118 | =cut | ||
| 119 | |||
| 120 | sub log { | ||
| 121 | my ( $self, $message ) = @_; | ||
| 122 | |||
| 123 | return unless defined $message; | ||
| 124 | |||
| 125 | $message = $message->{message} if ref($message) eq 'HASH' and defined $message->{message}; | ||
| 126 | |||
| 127 | my $method = $self->{method}; | ||
| 128 | my $worker = $self->{worker}; | ||
| 129 | |||
| 130 | my $workload = $message; | ||
| 131 | |||
| 132 | my ( $ret, $job_handle ) = $self->{client}->$method( $worker, $workload ); | ||
| 133 | if ( $ret != GEARMAN_SUCCESS ) { | ||
| 134 | return $self->_raise_error( $self->{client}->error() ); | ||
| 135 | } | ||
| 136 | |||
| 137 | return 1; | ||
| 138 | } | ||
| 139 | |||
| 140 | =head2 errstr | ||
| 141 | |||
| 142 | Returns the last error message. | ||
| 143 | |||
| 144 | =cut | ||
| 145 | |||
| 146 | sub errstr { $ERRSTR } | ||
| 147 | |||
| 148 | =head2 gearman_client | ||
| 149 | |||
| 150 | Returns L<Gearman::XS::Client> instance. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub gearman_client { | ||
| 155 | return shift->{client}; | ||
| 156 | } | ||
| 157 | |||
| 158 | =head2 reload | ||
| 159 | |||
| 160 | Reload with a new configuration. | ||
| 161 | |||
| 162 | =cut | ||
| 163 | |||
| 164 | sub reload { | ||
| 165 | my $self = shift; | ||
| 166 | |||
| 167 | my %options = (); | ||
| 168 | eval { %options = $self->_validate(@_) }; | ||
| 169 | |||
| 170 | if ($@) { | ||
| 171 | return $self->_raise_error($@); | ||
| 172 | } | ||
| 173 | |||
| 174 | foreach my $key (keys %options) { | ||
| 175 | $self->{$key} = $options{$key}; | ||
| 176 | } | ||
| 177 | |||
| 178 | $self->_setup_gearman; | ||
| 179 | |||
| 180 | return 1; | ||
| 181 | } | ||
| 182 | |||
| 183 | sub _setup_gearman { | ||
| 184 | my ($self) = @_; | ||
| 185 | my $client = Gearman::XS::Client->new; | ||
| 186 | my $ret = $client->add_servers( join( ',', @{ $self->{servers} } ) ); | ||
| 187 | if ( $ret != GEARMAN_SUCCESS ) { | ||
| 188 | croak( $client->error() ); | ||
| 189 | } | ||
| 190 | $self->{client} = $client; | ||
| 191 | } | ||
| 192 | |||
| 193 | sub _raise_error { | ||
| 194 | my $self = shift; | ||
| 195 | $ERRSTR = shift; | ||
| 196 | return undef; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub _validate { | ||
| 200 | my $self = shift; | ||
| 201 | return Params::Validate::validate( | ||
| 202 | @_, | ||
| 203 | { | ||
| 204 | servers => { | ||
| 205 | type => Params::Validate::ARRAYREF, | ||
| 206 | optional => 0, | ||
| 207 | }, | ||
| 208 | worker => { | ||
| 209 | type => Params::Validate::SCALAR, | ||
| 210 | optional => 0, | ||
| 211 | }, | ||
| 212 | method => { | ||
| 213 | type => Params::Validate::SCALAR, | ||
| 214 | regex => qr/^(do|do_high|do_low|do_background|do_high_background|do_low_background)$/, | ||
| 215 | default => 'do_background', | ||
| 216 | }, | ||
| 217 | } | ||
| 218 | ); | ||
| 219 | } | ||
| 220 | |||
| 221 | =head1 AUTHOR | ||
| 222 | |||
| 223 | Johannes Plunien E<lt>plu@cpan.orgE<gt> | ||
| 224 | |||
| 225 | =head1 COPYRIGHT AND LICENSE | ||
| 226 | |||
| 227 | Copyright 2009 by Johannes Plunien | ||
| 228 | |||
| 229 | This library is free software; you can redistribute it and/or modify | ||
| 230 | it under the same terms as Perl itself. | ||
| 231 | |||
| 232 | =head1 SEE ALSO | ||
| 233 | |||
| 234 | =over 4 | ||
| 235 | |||
| 236 | =item * L<Log::Handler> | ||
| 237 | |||
| 238 | =item * L<Gearman::XS::Client> | ||
| 239 | |||
| 240 | =item * L<http://www.gearman.org/> | ||
| 241 | |||
| 242 | =back | ||
| 243 | |||
| 244 | =head1 REPOSITORY | ||
| 245 | |||
| 246 | L<http://github.com/plu/log-handler-output-gearman/> | ||
| 247 | |||
| 248 | =cut | ||
| 249 | |||
| 250 | 1; | ||