Helm/Log/Channel/email.pm untested

filter: Statementstmt 0.0% 0 / 145 Branchbran 0.0% 0 / 36 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 14 totaltotal 0.0% 0 / 227 CC 19 SCAR 26.2
CC
19
Cov
0%
BEGIN
30.0
parallelize
30.0
_append_body
30.0
CRAP 13.8
1 package Helm::Log::Channel::email;
2 use strict;
3 use warnings;
4 use Moose;
5 use namespace::autoclean;
6 use DateTime;
7  
8 BEGIN {
9 eval { require Email::Simple };
10 die "Could not load Email::Simple. It must be installed to use Helm's email logging" if $@;
11 eval { require Email::Simple::Creator };
12 die "Could not load Email::Simple::Creator. It must be installed to use Helm's email logging" if $@;
13 eval { require Email::Sender::Simple };
14 die "Could not load Email::Sender::Simple. It must be installed to use Helm's email logging" if $@;
15 eval { require Email::Valid };
16 die "Could not load Email::Valid::Simple. It must be installed to use Helm's email logging" if $@;
17 }
18  
19 extends 'Helm::Log::Channel';
20 has email_address => (is => 'ro', writer => '_email_address', isa => 'Str');
21 has email_body => (is => 'ro', writer => '_email_body', isa => 'Str', default => '');
22 has from => (is => 'ro', writer => '_from', isa => 'Str', default => '');
23 has is_parallel => (is => 'ro', writer => '_is_parallel', isa => 'Bool', default => 0);
24 has is_parent => (is => 'ro', writer => '_is_parent', isa => 'Bool', default => 0);
25 has pipes => (is => 'ro', writer => '_pipes', isa => 'HashRef', default => sub { {} });
26  
27 # fork off an IO worker which has a pipe for each server we're going to do work
28 # on. Use that pipe to communicate with the process doing the parallel work
29 # on that server. So when another child process does $helm->log->info... that
30 # output will end up going from that child process over a pipe reserved for that
31 # process's server, to this IO worker process which will then append it to the email body.
32 sub parallelize {
33 my ($self, $helm) = @_;
34 $self->_is_parallel(1);
35  
36 # if we're going to do parallel stuff, then create a pipe for each server now
37 # that we can use to communicate with the child processes later
38 Helm->debug("Creating a pipe for each server target for multiplexing output");
39 my %pipes = map { $_->name => IO::Pipe->new } (@{$helm->servers});
40  
41 # and one for the parent so it's handled like everything else
42 Helm->debug("Parent process should also communicate over multiplexing pipes");
43 my $parent_pipe = IO::Pipe->new();
44 $pipes{parent} = $parent_pipe;
45  
46 $self->_pipes(\%pipes);
47  
48 # fork off an IO worker process
49 my $pid = fork();
50 $helm->die("Couldn't fork email IO worker process") if !defined $pid;
51 if ($pid) {
52 # parent here
53 $self->_is_parent(1);
54 Helm->debug("Parent process pipe is a writer");
55 $parent_pipe->writer;
56 $parent_pipe->autoflush(1);
57 } else {
58 # child here
59 my %pipe_cleaners;
60 my $all_clean = AnyEvent->condvar;
61 Helm->debug("Child process pipes are readers");
62 foreach my $server (keys %pipes) {
63 my $pipe = $pipes{$server};
64 $pipe->reader;
65  
66 # create an IO watcher for this pipe
67 Helm->debug("Setting up reading event for IO worker pipe for $server");
68 $pipe_cleaners{$server} = AnyEvent->io(
69 fh => $pipe,
70 poll => 'r',
71 cb => sub {
72 my $msg = <$pipe>;
73 if ($msg) {
74 Helm->debug("IO Worker Appending text to email body: $msg");
75 $self->_email_body($self->email_body . $msg);
76 } else {
77 Helm->debug("Pipe for $server has been disconnected");
78 delete $pipe_cleaners{$server};
79 # tell the main program we're done if this is the last pipe
80 $all_clean->send unless %pipe_cleaners;
81 }
82 },
83 );
84 }
85  
86 Helm->debug("Waiting on child pipes to receive data");
87 $all_clean->recv;
88 Helm->debug("All child pipes have been read");
89 Helm->debug("IO Worker sending email");
90 $self->_send_email;
91 exit(0);
92 }
93 }
94  
95 sub forked {
96 my ($self, $type) = @_;
97  
98 if ($type eq 'child') {
99 my $pipes = $self->pipes;
100 my $server_name = $self->current_server->name;
101 my $pipe = $pipes->{$server_name};
102 Helm->debug("Console output now goes over writer pipe for $server_name");
103 $pipe->writer();
104 $pipe->autoflush(1);
105 }
106 }
107  
108  
109 sub initialize {
110 my ($self, $helm) = @_;
111  
112 # file the file and open it for appending
113 my $uri = $self->uri;
114 my $email = $uri->to;
115 my %headers = $uri->headers();
116 my $from = $headers{from} || $headers{From} || $headers{FROM};
117 $helm->die(qq(No "From" specified in mailto URI $uri)) unless $from;
118  
119 # remove possible leading double slash if someone does "mailto://" instead of "mailto:"
120 $email =~ s/^\/\///;
121  
122 $helm->die(qq("$email" is not a valid email address)) unless Email::Valid->address($email);
123 $self->_email_address($email);
124 $self->_from($from);
125 }
126  
127 sub finalize {
128 my ($self, $helm) = @_;
129  
130 # the IO worker process will take care of sending the email
131 # when we're in parallel mode
132 $self->_send_email() unless $self->is_parallel;
133 }
134  
135 sub start_server {
136 my ($self, $server) = @_;
137 $self->SUPER::start_server($server);
138  
139 if( $self->is_parallel ) {
140 $self->_append_body("Starting task on $server");
141 } else {
142 my $line = '=' x 70;
143 $self->_append_body("$line\n$server\n$line\n");
144 }
145 }
146  
147 sub end_server {
148 my ($self, $server) = @_;
149 $self->SUPER::end_server($server);
150  
151 if(!$self->is_parallel) {
152 $self->_append_body("\n\n");
153 }
154 }
155  
156 sub debug {
157 my ($self, $msg) = @_;
158 $self->_append_body("[debug] $msg\n");
159 }
160  
161 sub info {
162 my ($self, $msg) = @_;
163 $self->_append_body("$msg\n");
164 }
165  
166 sub warn {
167 my ($self, $msg) = @_;
168 $self->_append_body("[warn] $msg\n");
169 }
170  
171 sub error {
172 my ($self, $msg) = @_;
173 $self->_append_body("[error] $msg\n");
174 }
175  
176 sub _prefix {
177 my $self = shift;
178 }
179  
180 sub _append_body {
181 my ($self, $text) = @_;
182 chomp($text);
183
184 my $prefix;
185 if( $self->is_parallel ) {
186 my $ts = DateTime->now->strftime('%a %b %d %H:%M:%S %Y');
187 my $server = $self->current_server->name;
188 $prefix = "[$ts] [$server] ";
189 } else {
190 # indent content under the server label
191 $prefix = $self->current_server ? ' ' : '';
192 }
193  
194 if ($self->is_parallel) {
195 my $pipe;
196 if( $self->is_parent ) {
197 Helm->debug("Sending text to IO worker over parent pipe: $text");
198 $pipe = $self->pipes->{parent};
199 } else {
200 my $server = $self->current_server->name;
201 Helm->debug("Sending text to IO worker over $server pipe: $text");
202 $pipe = $self->pipes->{$server};
203 }
204 $pipe->print("$prefix$text\n");
205 } else {
206 Helm->debug("Appending text to email body: $text");
207 $self->_email_body($self->email_body . $prefix . $text . "\n");
208 }
209 }
210  
211 sub _send_email {
212 my $self = shift;
213 # send the email
214 Helm->debug("Sending email from " . $self->from . " to " . $self->email_address);
215 my $email = Email::Simple->create(
216 header => [
217 To => $self->email_address,
218 From => $self->from,
219 Subject => 'HELM: Task ' . $self->task,
220 ],
221 body => $self->email_body,
222 );
223 Email::Sender::Simple->send($email);
224 Helm->debug("Email sent");
225 }
226  
227 __PACKAGE__->meta->make_immutable;
228  
229 1;