File Coverage

blib/lib/Term/YAP/Process.pm
Criterion Covered Total %
statement 27 28 96.4
branch 2 4 50.0
condition 1 3 33.3
subroutine 8 8 100.0
pod n/a
total 38 43 88.3


line stmt bran cond sub pod time code
1             package Term::YAP::Process;
2              
3 8     8   523785 use strict;
  8         16  
  8         208  
4 8     8   41 use warnings;
  8         15  
  8         288  
5 8     8   5842 use Types::Standard qw(Int Bool);
  8         461414  
  8         120  
6 8     8   6436 use Config;
  8         16  
  8         332  
7 8     8   42 use Carp qw(confess);
  8         10  
  8         491  
8 8     8   6247 use Moo;
  8         95087  
  8         43  
9 8     8   56693 use namespace::clean;
  8         43422  
  8         44  
10              
11             extends 'Term::YAP';
12              
13             =head1 NAME
14              
15             Term::YAP::Process - process based subclass of Term::YAP
16              
17             =cut
18              
19             =head1 SYNOPSIS
20              
21             See parent class.
22              
23             =head1 DESCRIPTION
24              
25             This module is a C<fork> base implementation of L<Term::YAP>.
26              
27             =head1 ATTRIBUTES
28              
29             All from parent class plus the described below.
30              
31             =head2 child_pid
32              
33             The PID from the child process created to start the pulse.
34              
35             This is a read-only attribute and it's value is set after invoking the C<start> method.
36              
37             =cut
38              
39             has child_pid => (
40             is => 'ro',
41             isa => Int,
42             reader => 'get_child_pid',
43             writer => '_set_child_pid'
44             );
45              
46             =head2 usr1
47              
48             This class uses a USR1 signal to stop the child process of printing the pulse bar.
49              
50             This read-only attribute holds the signal number (an integer) that is built during class instantiation depending
51             on the platform where is executed.
52              
53             =cut
54              
55             has usr1 => (
56             is => 'ro',
57             isa => Int,
58             reader => 'get_usr1',
59             writer => '_set_usr1',
60             builder => \&_define_signal
61             );
62              
63             =head2 enough
64              
65             This read-only attribute is a boolean used by the child process to check if it should stop printing the pulse bar.
66              
67             You probably won't need to use externally this attribute.
68              
69             =cut
70              
71             has enough => (
72             is => 'ro',
73             isa => Bool,
74             default => 0,
75             reader => 'is_enough',
76             writer => '_set_enough'
77             );
78              
79             =head1 METHODS
80              
81             Some parent methods are overriden:
82              
83             =over
84              
85             =item *
86              
87             start
88              
89             =item *
90              
91             stop
92              
93             =back
94              
95             And some others are implemented by this class.
96              
97             =head2 is_enough
98              
99             Getter for C<enough> attribute.
100              
101             =head2 get_usr1
102              
103             Returns the value of C<usr1> attribute.
104              
105             =head2 get_child_pid
106              
107             Returns the value of the C<child_pid> attribute.
108              
109             =head2 start
110              
111             Method overrided from parent class.
112              
113             This method will use C<fork> to create a child process to create the pulse bar.
114              
115             The child process will have a signal handler for the USR1 signal to stop the pulse bar.
116              
117             =cut
118              
119             around start => sub {
120              
121             my ( $orig, $self ) = ( shift, shift );
122              
123             my $child_pid = fork();
124              
125             if ($child_pid) {
126              
127             #parent
128             $self->_set_child_pid($child_pid);
129             $self->_set_running(1);
130             $self->$orig;
131              
132             }
133             else {
134             #child
135             $SIG{USR1} = sub { $self->_set_enough(1) };
136             $self->_keep_pulsing();
137             exit 0;
138              
139             }
140              
141             };
142              
143             sub _define_signal {
144              
145 6     6   41100 my %sig_num;
146 6 50 33     732 unless ( $Config{sig_name} && $Config{sig_num} ) {
147 0         0 confess "No sigs?";
148             }
149             else {
150 6         150 my @names = split ' ', $Config{sig_name};
151 6         570 @sig_num{@names} = split ' ', $Config{sig_num};
152              
153             confess("this platform does not include USR1 signal")
154 6 50       66 unless ( exists( $sig_num{USR1} ) );
155              
156 6         204 return $sig_num{USR1};
157              
158             }
159              
160             }
161              
162             =head2 stop
163              
164             Method overrided from parent class.
165              
166             This method will send a USR1 signal with C<kill> to the child process executing the
167             pulse bar.
168              
169             The child process termination will be handled with C<waitpid>.
170              
171             =cut
172              
173             around stop => sub {
174              
175             my ( $orig, $self ) = ( shift, shift );
176              
177             kill $self->get_usr1(), $self->get_child_pid();
178             $self->_sleep();
179             waitpid( $self->get_child_pid(), 0 );
180              
181             $self->$orig();
182              
183             };
184              
185             around _is_enough => sub {
186              
187             my ( $orig, $self ) = ( shift, shift );
188             return $self->is_enough();
189              
190             };
191              
192             =head1 SEE ALSO
193              
194             =over
195              
196             =item *
197              
198             L<Term::Pulse>
199              
200             =item *
201              
202             L<Term::YAP::iThread>
203              
204             =item *
205              
206             L<Moo>
207              
208             =back
209              
210             =head1 AUTHOR
211              
212             Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
213              
214             L<Term::Pulse> was originally created by Yen-Liang Chen, E<lt>alec at cpan.comE<gt>
215              
216             =head1 COPYRIGHT AND LICENSE
217              
218             This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.orgE<gt>
219              
220             This file is part of Siebel Monitoring Tools.
221              
222             Siebel Monitoring Tools is free software: you can redistribute it and/or modify
223             it under the terms of the GNU General Public License as published by
224             the Free Software Foundation, either version 3 of the License, or
225             (at your option) any later version.
226              
227             Siebel Monitoring Tools is distributed in the hope that it will be useful,
228             but WITHOUT ANY WARRANTY; without even the implied warranty of
229             MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
230             GNU General Public License for more details.
231              
232             You should have received a copy of the GNU General Public License
233             along with Siebel Monitoring Tools. If not, see <http://www.gnu.org/licenses/>.
234              
235             =cut
236              
237             1;