File Coverage

blib/lib/Siffra/Bootstrap.pm
Criterion Covered Total %
statement 71 138 51.4
branch 2 24 8.3
condition 2 12 16.6
subroutine 20 25 80.0
pod 5 5 100.0
total 100 204 49.0


line stmt bran cond sub pod time code
1             package Siffra::Bootstrap;
2              
3 1     1   67959 use 5.014;
  1         4  
4 1     1   5 use strict;
  1         2  
  1         19  
5 1     1   4 use warnings;
  1         2  
  1         21  
6 1     1   619 use utf8;
  1         14  
  1         5  
7 1     1   646 use Data::Dumper;
  1         6797  
  1         59  
8 1     1   442 use DDP;
  1         41602  
  1         9  
9 1     1   547 use Log::Any qw($log);
  1         10387  
  1         7  
10 1     1   2221 use Scalar::Util qw(blessed);
  1         2  
  1         53  
11             $Carp::Verbose = 1;
12              
13 1     1   507 use Config::Any;
  1         8557  
  1         32  
14 1     1   892 use IO::Prompter;
  1         34875  
  1         7  
15              
16             $| = 1; #autoflush
17              
18             use constant {
19             FALSE => 0,
20             TRUE => 1,
21 1   50     113 DEBUG => $ENV{ DEBUG } // 0,
22 1     1   105 };
  1         3  
23              
24             BEGIN
25             {
26 1     1   34 binmode( STDOUT, ":encoding(UTF-8)" );
  1     1   7  
  1         2  
  1         6  
27 1         17687 binmode( STDERR, ":encoding(UTF-8)" );
28              
29 1         603 require Siffra::Tools;
30 1     1   7 use Exporter ();
  1         2  
  1         23  
31 1     1   4 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  1         2  
  1         95  
32 1         75437 $VERSION = '0.09';
33 1         25 @ISA = qw(Siffra::Tools Exporter);
34              
35             #Give a hoot don't pollute, do not export more than needed by default
36 1         5 @EXPORT = qw();
37 1         2 @EXPORT_OK = qw();
38 1         41 %EXPORT_TAGS = ();
39             } ## end BEGIN
40              
41             UNITCHECK
42             {
43 1     1   7 eval { use Fcntl qw(:flock); };
  1         2  
  1         1705  
44             $log->info( "Tentando lockar o programa [ $0 ]..." );
45             unless ( flock( DATA, LOCK_EX | LOCK_NB ) )
46             {
47             $log->warn( "O programa [ $0 ] já está sendo executado. Saindo." );
48             exit( 1 );
49             }
50             $log->info( "Programa [ $0 ] lockado com sucesso..." );
51             } ## end UNITCHECK
52              
53             #################### subroutine header begin ####################
54              
55             =head2 sample_function
56              
57             Usage : How to use this function/method
58             Purpose : What it does
59             Returns : What it returns
60             Argument : What it wants to know
61             Throws : Exceptions and other anomolies
62             Comment : This is a sample subroutine header.
63             : It is polite to include more pod and fewer comments.
64              
65             See Also :
66              
67             =cut
68              
69             #################### subroutine header end ####################
70              
71             =head2 C<new()>
72              
73             Usage : $self->block_new_method() within text_pm_file()
74             Purpose : Build 'new()' method as part of a pm file
75             Returns : String holding sub new.
76             Argument : $module: pointer to the module being built
77             (as there can be more than one module built by EU::MM);
78             for the primary module it is a pointer to $self
79             Throws : n/a
80             Comment : This method is a likely candidate for alteration in a subclass,
81             e.g., pass a single hash-ref to new() instead of a list of
82             parameters.
83              
84             =cut
85              
86             sub new
87             {
88 1     1 1 90 $log->debug( "new", { progname => $0, pid => $$, perl_version => $], package => __PACKAGE__ } );
89 1         15 my ( $class, %parameters ) = @_;
90 1         11 my $self = $class->SUPER::new( %parameters );
91              
92             $self->{ beachmarck } = {
93 1         49 created => $^T,
94             started => undef,
95             finished => undef
96             };
97              
98 1         3 $self->{ configurations } = \%parameters;
99              
100 1         4 $self->_initialize( %parameters );
101 1         3719 return $self;
102             } ## end sub new
103              
104             sub _initialize()
105             {
106 1     1   6 $log->debug( "_initialize", { package => __PACKAGE__ } );
107 1         4 my ( $self, %parameters ) = @_;
108 1         6 $self->SUPER::_initialize( %parameters );
109             }
110              
111             sub _finalize()
112             {
113 0     0   0 $log->debug( "_finalize", { package => __PACKAGE__ } );
114 0         0 my ( $self, %parameters ) = @_;
115 0         0 $self->SUPER::_finalize( %parameters );
116             }
117              
118             =head2 C<loadApplication()>
119              
120             Usage : $self->block_new_method() within text_pm_file()
121             Purpose : Build 'new()' method as part of a pm file
122             Returns : String holding sub new.
123             Argument : $module: pointer to the module being built
124             (as there can be more than one module built by EU::MM);
125             for the primary module it is a pointer to $self
126             Throws : n/a
127             Comment : This method is a likely candidate for alteration in a subclass,
128             e.g., pass a single hash-ref to new() instead of a list of
129             parameters.
130              
131             =cut
132              
133             sub loadApplication()
134             {
135 0     0 1 0 my ( $self, %parameters ) = @_;
136 0         0 $log->debug( "loadApplication", { package => __PACKAGE__ } );
137 0         0 my $configurationFile = $self->{ configurations }->{ configurationFile };
138              
139 0 0       0 if ( !-e $configurationFile )
140             {
141 0         0 ( $configurationFile = $0 ) =~ s/\.pl/\-config\.json/;
142 0         0 $log->error( "Não existe o arquivo de configuração...", { package => __PACKAGE__ } );
143              
144 0 0       0 if ( prompt 'Não existe o arquivo de configuração, deseja criar agora ?', -yn1, -default => 'y' )
145             {
146 0         0 my $config;
147              
148 0         0 $config->{ $configurationFile }->{ applicationName } = prompt( 'Application Name :', -v, -echostyle => 'bold white' );
149 0         0 $config->{ $configurationFile }->{ applicationModule } = prompt( 'Application Module :', -v, -echostyle => 'bold white' );
150 0         0 $config->{ $configurationFile }->{ environment } = prompt( 'Environment [desenv] :', -v, -echostyle => 'bold white', -default => 'desenv' );
151              
152             # Mail Config
153             $config->{ $configurationFile }->{ mail } = {
154 0         0 server => prompt( 'E-mail server [mail] :', -v, -echostyle => 'bold white', -default => 'mail' ),
155             port => prompt( 'E-mail port [25] :', -v, -echostyle => 'bold white', -default => '25' ),
156             debug => prompt( 'E-mail debug [0] :', -v, -echostyle => 'bold white', -default => '0' ),
157             from => prompt( 'E-mail from :', -v, -echostyle => 'bold white' ),
158             };
159              
160 0         0 my $json_text = $self->{ json }->pretty( 1 )->canonical( 1 )->encode( $config->{ $configurationFile } );
161              
162 0         0 open FH, ">", $configurationFile;
163 0         0 print FH $json_text;
164 0         0 close FH;
165             } ## end if ( prompt 'Não existe o arquivo de configuração, deseja criar agora ?'...)
166             else
167             {
168 0         0 return FALSE;
169             }
170             } ## end if ( !-e $configurationFile...)
171              
172 0         0 my @configFiles = ( $configurationFile );
173 0         0 my $configAny = Config::Any->load_files( { files => \@configFiles, flatten_to_hash => 1, use_ext => 1 } );
174              
175 0         0 foreach my $configFile ( keys %$configAny )
176             {
177 0         0 $self->{ configurations }->{ $_ } = $configAny->{ $configFile }->{ $_ } foreach ( keys %{ $configAny->{ $configFile } } );
  0         0  
178             }
179              
180 0         0 $log->info( "Configurações lidas com sucesso na aplicação [ $self->{ configurations }->{ application }->{ name } ]..." );
181              
182 0 0       0 $log->error( 'Falta passar nome do package' ) if ( $self->{ configurations }->{ application }->{ package } !~ /^\D/ );
183              
184 0         0 eval "use $self->{ configurations }->{ application }->{ fileName };"; ## Usando o módulo.
185              
186 0 0       0 if ( $@ )
187             {
188 0         0 $log->error( "Problemas ao usar o arquivo [ $self->{ configurations }->{ application }->{ fileName } ]..." );
189 0         0 return FALSE;
190             }
191             else
192             {
193 0         0 $self->{ application }->{ instance } = eval { $self->{ configurations }->{ application }->{ package }->new( bootstrap => $self ); };
  0         0  
194 0 0       0 if ( !$self->{ application }->{ instance } )
195             {
196 0         0 $log->error( "Erro ao iniciar o módulo [ $self->{ configurations }->{ application }->{ package } ]..." );
197 0         0 return FALSE;
198             }
199             } ## end else [ if ( $@ ) ]
200              
201 0         0 my $emailDispatcher = $log->{ adapter }->{ dispatcher }->{ outputs }->{ Email };
202              
203 0 0       0 if ( $emailDispatcher )
204             {
205 0         0 my $mailConf = $self->{ configurations }->{ mail };
206              
207 0 0       0 if ( $mailConf )
208             {
209 0         0 eval { require DateTime; };
  0         0  
210 0         0 $emailDispatcher->{ to } = [ $mailConf->{ to } ];
211 0         0 $emailDispatcher->{ from } = $mailConf->{ from };
212 0         0 $emailDispatcher->{ host } = $mailConf->{ host };
213 0         0 $emailDispatcher->{ port } = $mailConf->{ port };
214 0         0 $emailDispatcher->{ subject } = "[ " . DateTime->now()->datetime( ' ' ) . " ] " . $self->{ configurations }->{ application }->{ name };
215             } ## end if ( $mailConf )
216              
217             } ## end if ( $emailDispatcher ...)
218              
219 0         0 return TRUE;
220             } ## end sub loadApplication
221              
222             =head2 C<run()>
223             =cut
224              
225             sub run
226             {
227 0     0 1 0 my ( $self, %parameters ) = @_;
228 0         0 my $retorno = {};
229              
230 0 0       0 if ( ref $self->{ application }->{ instance } eq $self->{ configurations }->{ application }->{ package } )
231             {
232 0         0 $self->{ beachmarck }->{ started } = time();
233 0         0 $retorno = eval { $self->{ application }->{ instance }->start(); };
  0         0  
234 0         0 $self->{ beachmarck }->{ finished } = time();
235 0 0       0 $log->error( "Problemas durante execucao de start: $@" ) if ( $@ );
236             } ## end if ( ref $self->{ application...})
237             else
238             {
239 0         0 $log->error( "Impossivel de executar $self->{ configurations }->{ application }->{ package }::start\nSub nao existe" );
240             }
241              
242 0         0 return $retorno;
243             } ## end sub run
244              
245             =head2 C<getExecutionTime()>
246             =cut
247              
248             sub getExecutionTime()
249             {
250 0     0 1 0 my ( $self, %parameters ) = @_;
251 0   0     0 return ( ( $self->{ beachmarck }->{ finished } // 0 ) - ( $self->{ beachmarck }->{ started } // 0 ) );
      0        
252             }
253              
254             =head2 C<getExecutionInfo()>
255             =cut
256              
257             sub getExecutionInfo()
258             {
259 0     0 1 0 my ( $self, %parameters ) = @_;
260              
261 0         0 $log->info( "Quantidade de erro(s): 0" );
262 0         0 $log->info( "Tempo de execucao: " . $self->getExecutionTime() . " segundo(s)" );
263 0         0 $log->info( "Fim da aplicacao." );
264             } ## end sub getExecutionInfo
265              
266             sub END
267             {
268 1     1   184 $log->debug( "END", { package => __PACKAGE__, line => __LINE__ } );
269 1         6 my ( $self, %parameters ) = @_;
270 1         2 eval { $log->{ adapter }->{ dispatcher }->{ outputs }->{ Email }->flush; };
  1         58  
271             }
272              
273             sub DESTROY
274             {
275 1     1   563 my ( $self, %parameters ) = @_;
276 1 50       6 if ( ${^GLOBAL_PHASE} eq 'DESTRUCT' )
277             {
278 0 0 0     0 $self->getExecutionInfo() if ( blessed( $self ) && $self->isa( __PACKAGE__ ) );
279 0         0 return;
280             }
281              
282 1 50 33     12 if ( blessed( $self ) && $self->isa( __PACKAGE__ ) )
283             {
284 1         6 $log->debug( "DESTROY", { package => __PACKAGE__, GLOBAL_PHASE => ${^GLOBAL_PHASE}, blessed => TRUE } );
285             }
286             else
287             {
288 0         0 $log->debug( 'DESTROY', { package => __PACKAGE__, GLOBAL_PHASE => ${^GLOBAL_PHASE}, blessed => FALSE } );
289             }
290              
291 1         13 Siffra::Bootstrap->SUPER::DESTROY;
292             } ## end sub DESTROY
293              
294             #################### main pod documentation begin ###################
295             ## Below is the stub of documentation for your module.
296             ## You better edit it!
297              
298             =encoding UTF-8
299              
300              
301             =head1 NAME
302              
303             Siffra::Bootstrap - Module abstract (<= 44 characters) goes here
304              
305             =head1 SYNOPSIS
306              
307             use Siffra::Bootstrap;
308             blah blah blah
309              
310              
311             =head1 DESCRIPTION
312              
313             Stub documentation for this module was created by ExtUtils::ModuleMaker.
314             It looks like the author of the extension was negligent enough
315             to leave the stub unedited.
316              
317             Blah blah blah.
318              
319              
320             =head1 USAGE
321              
322              
323              
324             =head1 BUGS
325              
326              
327              
328             =head1 SUPPORT
329              
330              
331              
332             =head1 AUTHOR
333              
334             Luiz Benevenuto
335             CPAN ID: LUIZBENE
336             Siffra TI
337             luiz@siffra.com.br
338             https://siffra.com.br
339              
340             =head1 COPYRIGHT
341              
342             This program is free software; you can redistribute
343             it and/or modify it under the same terms as Perl itself.
344              
345             The full text of the license can be found in the
346             LICENSE file included with this module.
347              
348              
349             =head1 SEE ALSO
350              
351             perl(1).
352              
353             =cut
354              
355             #################### main pod documentation end ###################
356              
357             1;
358              
359             # The preceding line will help the module return a true value
360              
361             __DATA__