script/eris-stdin-listener.pl untested

filter: Statementstmt 0.0% 0 / 57 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 5 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 93 CC 6 SCAR 19.3
CC
6
Cov
0%
main_stats
20.0
main_start
6.0
worker_stdout
6.0
CRAP 6.9
1 #!/usr/local/bin/perl
2 # PODNAME: eris-stdin-listener.pl
3 # ABSTRACT: Simple wrapper to spawn workers for handling syslog stream
4 ## no critic (RequireEndWithOne)
5 use strict;
6 use warnings;
7  
8 use FindBin;
9 use Hash::Merge::Simple qw(clone_merge);
10 use Getopt::Long::Descriptive;
11 use Path::Tiny;
12  
13 use POE qw(
14 Component::WheelRun::Pool
15 Wheel::ReadWrite
16 Filter::Line
17 );
18  
19 my ($opt,$usage) = describe_options('%c - %o',
20 [ 'config=s', 'Eris YAML config file, required', { required => 1, validate => { "Must be a readable file." => sub { -r $_[0] } } } ],
21 [],
22 [ 'help', 'Display this help' ],
23 );
24 if( $opt->help ) {
25 print $usage->text;
26 exit 0;
27 }
28  
29 my $main_session = POE::Session->create(
30 inline_states => {
31 _start => \&main_start,
32 _stop => \&main_stop,
33 stats => \&main_stats,
34  
35 syslog_input => \&syslog_input,
36 syslog_error => \&syslog_error,
37 worker_stdout => \&worker_stdout,
38 },
39 heap => {
40 stats => {},
41 },
42 );
43  
44 POE::Kernel->run();
45  
46 sub main_stop { }
47  
48 sub main_start {
49 my ($kernel,$heap) = @_[KERNEL,HEAP];
50  
51 # Handle to the syslog daemon
52 $heap->{io} = POE::Wheel::ReadWrite->new(
53 InputHandle => \*STDIN,
54 OutputHandle => \*STDOUT,
55 InputFilter => POE::Filter::Line->new(),
56 InputEvent => 'syslog_input',
57 ErrorEvent => 'syslog_error',
58 );
59  
60 my $bindir = path( "$FindBin::RealBin" );
61  
62 $heap->{workers} = POE::Component::WheelRun::Pool->spawn(
63 Alias => 'pool',
64 Program => $^X,
65 ProgramArgs => [
66 $bindir->child('eris-es-indexer.pl')->stringify,
67 '--config', $opt->config,
68 ],
69 StdinFilter => POE::Filter::Line->new(),
70 StdoutFilter => POE::Filter::Reference->new(),
71 StatsHandler => sub {
72 my ($stats) = @_;
73 if( is_hashref($stats) ) {
74 $heap->{stats} = clone_merge( $stats, $heap->{stats} );
75 }
76 },
77 StdoutHandler => sub {
78 $kernel->yield(worker_stdout => @_);
79 }
80 );
81  
82 $kernel->delay(stats => 60);
83 }
84  
85 sub main_stats {
86 my ($kernel,$heap) = @_[KERNEL,HEAP];
87  
88 my $stats = exists $heap->{stats} ? delete $heap->{stats} : {};
89  
90 if( exists $heap->{graphite} ) {
91 # output to graphite
92 }
93  
94 # Reschedule ourselves;
95 $heap->{stats} = {};
96 $kernel->delay( stats => 60 ) unless exists $heap->{_shutdown};
97 }
98  
99 sub worker_stdout {
100 my ($kernel,$heap,$stats) = @_[KERNEL,HEAP,ARG0];
101  
102 # Make sure we have stats
103 return unless defined $stats && is_hashref($stats);
104  
105 # Aggregate stats from all our workers
106 foreach my $s (keys %{ $stats }) {
107 $heap->{stats}{$s} ||= 0;
108 $heap->{stats}{$s} += $stats->{$s};
109 }
110 }
111  
112 sub syslog_input {
113 my ($kernel,$heap,$msg) = @_[KERNEL,HEAP,ARG0];
114 $kernel->post( pool => dispatch => $msg );
115 }
116  
117 sub syslog_error {
118 my ($kernel,$heap) = @_[KERNEL,HEAP];
119 delete $heap->{io};
120 $heap->{_shutdown} = 1;
121 }
122  
123 __END__
124  
125 =pod
126  
127 =encoding UTF-8
128  
129 =head1 NAME
130  
131 eris-stdin-listener.pl - Simple wrapper to spawn workers for handling syslog stream
132  
133 =head1 VERSION
134  
135 version 0.008
136  
137 =head1 AUTHOR
138  
139 Brad Lhotsky <brad@divisionbyzero.net>
140  
141 =head1 COPYRIGHT AND LICENSE
142  
143 This software is Copyright (c) 2015 by Brad Lhotsky.
144  
145 This is free software, licensed under:
146  
147 The (three-clause) BSD License
148  
149 =cut