| 1 | package Helm::Log::Channel::console; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Moose; | ||
| 5 | use Term::ANSIColor qw(colored color); | ||
| 6 | use IO::Pipe; | ||
| 7 | use AnyEvent; | ||
| 8 | use namespace::autoclean; | ||
| 9 | |||
| 10 | extends 'Helm::Log::Channel'; | ||
| 11 | has pipes => (is => 'ro', writer => '_pipes', isa => 'HashRef'); | ||
| 12 | has fh => (is => 'ro', writer => '_fh'); | ||
| 13 | has is_parallel => (is => 'rw', isa => 'Bool', default => 0); | ||
| 14 | |||
| 15 | my $LINE = '-' x 70; | ||
| 16 | |||
| 17 | sub initialize { | ||
| 18 | my ($self, $helm) = @_; | ||
| 19 | |||
| 20 | # default FH is STDERR | ||
| 21 | Helm->debug("Initializing output handle to STDERR"); | ||
| 22 | my $fh = IO::Handle->new_from_fd(fileno(STDERR), 'w'); | ||
| 23 | $self->_fh($fh); | ||
| 24 | } | ||
| 25 | |||
| 26 | sub finalize { | ||
| 27 | my ($self, $helm) = @_; | ||
| 28 | # make sure the terminal is reset | ||
| 29 | print color 'reset'; | ||
| 30 | } | ||
| 31 | |||
| 32 | sub start_server { | ||
| 33 | my ($self, $server) = @_; | ||
| 34 | $self->SUPER::start_server($server); | ||
| 35 | |||
| 36 | if ($self->is_parallel) { | ||
| 37 | $self->output("Starting task on $server", 'blue'); | ||
| 38 | } else { | ||
| 39 | $self->output("$LINE\n$server\n$LINE", 'blue'); | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | sub end_server { | ||
| 44 | my ($self, $server) = @_; | ||
| 45 | $self->SUPER::end_server($server); | ||
| 46 | if (!$self->is_parallel) { | ||
| 47 | $self->fh->print("\n"); | ||
| 48 | } | ||
| 49 | } | ||
| 50 | |||
| 51 | sub debug { | ||
| 52 | my ($self, $msg) = @_; | ||
| 53 | $self->output("[DEBUG] $msg", 'bright_blue'); | ||
| 54 | } | ||
| 55 | |||
| 56 | sub info { | ||
| 57 | my ($self, $msg) = @_; | ||
| 58 | $self->output($msg, 'bright_green'); | ||
| 59 | } | ||
| 60 | |||
| 61 | sub warn { | ||
| 62 | my ($self, $msg) = @_; | ||
| 63 | $self->output("[WARN] $msg", 'yellow'); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub error { | ||
| 67 | my ($self, $msg) = @_; | ||
| 68 | $self->output("[ERROR] $msg", 'red'); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub output { | ||
| 72 | my ($self, $msg, @colors) = @_; | ||
| 73 | my $prefix = $self->is_parallel ? '[' . $self->current_server->name . '] ' : ''; | ||
| 74 | $msg = "$prefix$msg\n"; | ||
| 75 | $msg = colored($msg, @colors) if @colors; | ||
| 76 | $self->fh->print($msg); | ||
| 77 | } | ||
| 78 | |||
| 79 | # fork off an IO worker which has a pipe for each server we're going to do work | ||
| 80 | # on. Use that pipe to communicate with the process doing the parallel work | ||
| 81 | # on that server. So when another child process does $helm->log->info... that | ||
| 82 | # output will end up going from that child process over a pipe reserved for that | ||
| 83 | # process's server, to this IO worker process which will then send it to the console. | ||
| 84 | sub parallelize { | ||
| 85 | my ($self, $helm) = @_; | ||
| 86 | $self->is_parallel(1); | ||
| 87 | |||
| 88 | # if we're going to do parallel stuff, then create a pipe for each server now | ||
| 89 | # that we can use to communicate with the child processes later | ||
| 90 | Helm->debug("Creating a pipe for each server target for multiplexing output"); | ||
| 91 | my %pipes = map { $_->name => IO::Pipe->new } (@{$helm->servers}); | ||
| 92 | |||
| 93 | # and one for the parent so it's handled like everything else | ||
| 94 | Helm->debug("Parent process should also communicate over multiplexing pipes"); | ||
| 95 | my $parent_pipe = IO::Pipe->new(); | ||
| 96 | $pipes{parent} = $parent_pipe; | ||
| 97 | |||
| 98 | $self->_pipes(\%pipes); | ||
| 99 | |||
| 100 | # fork off an IO worker process | ||
| 101 | my $pid = fork(); | ||
| 102 | $helm->die("Couldn't fork console IO worker process") if !defined $pid; | ||
| 103 | if ($pid) { | ||
| 104 | # parent here | ||
| 105 | Helm->debug("Parent process pipe is a writer"); | ||
| 106 | $parent_pipe->writer; | ||
| 107 | $parent_pipe->autoflush(1); | ||
| 108 | $self->_fh($parent_pipe); | ||
| 109 | } else { | ||
| 110 | # child here | ||
| 111 | my %pipe_cleaners; | ||
| 112 | my $all_clean = AnyEvent->condvar; | ||
| 113 | Helm->debug("Child process pipes are readers"); | ||
| 114 | foreach my $server (keys %pipes) { | ||
| 115 | my $pipe = $pipes{$server}; | ||
| 116 | $pipe->reader; | ||
| 117 | |||
| 118 | # create an IO watcher for this pipe | ||
| 119 | Helm->debug("Setting up reading event for IO worker pipe for $server"); | ||
| 120 | $pipe_cleaners{$server} = AnyEvent->io( | ||
| 121 | fh => $pipe, | ||
| 122 | poll => 'r', | ||
| 123 | cb => sub { | ||
| 124 | my $msg = <$pipe>; | ||
| 125 | if ($msg) { | ||
| 126 | print STDERR $msg; | ||
| 127 | } else { | ||
| 128 | Helm->debug("Pipe for $server has been disconnected"); | ||
| 129 | delete $pipe_cleaners{$server}; | ||
| 130 | # tell the main program we're done if this is the last pipe | ||
| 131 | $all_clean->send unless %pipe_cleaners; | ||
| 132 | } | ||
| 133 | }, | ||
| 134 | ); | ||
| 135 | } | ||
| 136 | |||
| 137 | Helm->debug("Waiting on child pipes to receive data"); | ||
| 138 | $all_clean->recv; | ||
| 139 | Helm->debug("All child pipes have been read"); | ||
| 140 | exit(0); | ||
| 141 | } | ||
| 142 | } | ||
| 143 | |||
| 144 | # we've been forked, and if it's a child we want to initialize the pipe | ||
| 145 | # for this worker child's server | ||
| 146 | sub forked { | ||
| 147 | my ($self, $type) = @_; | ||
| 148 | |||
| 149 | if ($type eq 'child') { | ||
| 150 | my $pipes = $self->pipes; | ||
| 151 | my $server_name = $self->current_server->name; | ||
| 152 | my $pipe = $pipes->{$server_name}; | ||
| 153 | Helm->debug("Console output now goes over writer pipe for $server_name"); | ||
| 154 | $pipe->writer(); | ||
| 155 | $pipe->autoflush(1); | ||
| 156 | $self->_fh($pipe); | ||
| 157 | } | ||
| 158 | } | ||
| 159 | |||
| 160 | __PACKAGE__->meta->make_immutable; | ||
| 161 | |||
| 162 | 1; |