lib/Project/Easy.pm

filter: Statementstmt 76.8% 275 / 358 Branchbran 49.1% 58 / 118 Conditioncond 34.2% 26 / 76 MC/DCmcdc 2.0% 1 / 50 Subroutinesub 84.6% 33 / 39 Podpod 40.0% 8 / 20 totaltotal 60.6% 401 / 661 CC 91 SCAR 20.6
CC
91
Cov
65%
db
14.7
db
14.7
instantiate
13.5
CRAP 7.8
« lib/Project/Easy/Helper/Status.pm ↑ Summary lib/Project/Easy/Helper/Console.pm »
1 package Project::Easy;
2  
3 6 ▶use Class::Easy::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 10 ▶use IO::Easy;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 6 ▶unless ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^O eq "MSWin32") 6 0 6
7 6 try_to_use 'Sys::SigAction';
8 }
9  
10 # these constants must be available prior to the helper use
11 6 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 10 our $VERSION = '0.30';
13  
14 6 has etc => 'etc'; # conf, config
15 6 has bin => 'bin'; # scripts, tools
16 6 has t => 't'; # test, tests
17 };
18  
19 6 ▶use Project::Easy::Helper;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 10 ▶use Project::Easy::Config::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # because singleton
23 6 our $singleton = {};
24  
25 6 ▶sub singleton {
Subroutine singleton: called CC=1 SCAR=0.0
Pod documented
26 4 ▶ return $singleton;
Subroutine singleton: called CC=1 SCAR=0.0
Pod documented
27 }
28  
29 6 has daemons => {};
30  
31 6 has daemon_package => 'Project::Easy::Daemon';
32 6 has db_package => 'Project::Easy::DB';
33 6 has conf_package => 'Project::Easy::Config';
34  
35 6 ▶sub import {
Subroutine import: called CC=4 SCAR=14.4
36 4 ▶ my $pack = shift;
Subroutine import: called CC=4 SCAR=14.4
37 4 my @params = @_;
38
39 4 my $caller = caller;
40
41 4 ▶ if (scalar grep {$_ eq 'script'} @params or $caller eq 'main') {
Branch: 4 branches
BranchTrueFalseTotal
if (scalar grep({$_ eq 'script';} @params) or $caller eq 'main') { } 1 2 3
elsif (scalar grep({$_ eq 'project';} @params)) { } 0 2 2
if (scalar grep({$_ eq 'script';} @params) or $caller eq 'main') { } 4 6 10
elsif (scalar grep({$_ eq 'project';} @params)) { } 0 6 6
Condition50%3/6
scalar grep({$_ eq "script";} @params) or $caller eq "main"
l!l&&r!l&&!r
102
scalar grep({$_ eq "script";} @params) or $caller eq "main"
l!l&&r!l&&!r
006
MC/DC50%1/2
scalar grep({$_ eq "script";} @params) or $caller eq "main"
scalar grep({$_ eq "script";} @params) $caller eq "main"
MC/DC0%0/2
scalar grep({$_ eq "script";} @params) or $caller eq "main"
scalar grep({$_ eq "script";} @params) $caller eq "main"
Truth table66%2/3
A or B
A= scalar grep({$_ eq "script";} @params)B= $caller eq "main"
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= scalar grep({$_ eq "script";} @params)B= $caller eq "main"
ABresult
1X1
011
000
42 4 Project::Easy::Helper::_script_wrapper;
43  
44 4 Class::Easy->import;
45 0 IO::Easy->import (qw(project));
46
47 0 } elsif (scalar grep {$_ eq 'project'} @params) {
48 0 Project::Easy::Helper::_script_wrapper (undef, 1);
49
50 }
51 }
52  
53 6 ▶sub init {
Subroutine init: called CC=3 SCAR=11.1
Pod undocumented
54 4 ▶ my $class = shift;
Subroutine init: called CC=3 SCAR=11.1
Pod undocumented
55
56 4 my $conf_package = $class->conf_package;
57 4 ▶ try_to_use ($conf_package)
Branch: 2 branches
BranchTrueFalseTotal
try_to_use($conf_package) or die "configuration package must exists" 2 0 2
try_to_use($conf_package) or die "configuration package must exists" 6 0 6
58 or die ('configuration package must exists');
59
60 4 $class->attach_paths;
61
62 4 my $root = dir ($class->lib_path)->parent;
63  
64 44 make_accessor ($class, root => $root);
65
66 3 my $conf_path = $root->append ($class->etc, $class->id . '.' . $class->conf_format)->as_file;
67
68 3 ▶ die "can't locate generic config file at '$conf_path'"
Branch: 2 branches
BranchTrueFalseTotal
unless -f $conf_path 0 2 2
unless -f $conf_path 0 6 6
69 unless -f $conf_path;
70  
71 # blessing for functionality extension: serializer
72 3 $conf_path = bless ($conf_path, 'Project::Easy::Config::File');
73  
74 3 make_accessor ($class, conf_path => $conf_path);
75
76 }
77  
78 6 ▶sub instantiate {
Subroutine instantiate: called CC=9 SCAR=26.0
Pod undocumented
79 1 ▶ my $class = shift;
Subroutine instantiate: called CC=9 SCAR=26.0
Pod undocumented
80
81 die "you cannot use Project::Easy in one project more than one time ($singleton->{_instantiated})"
82 1 ▶ if $singleton->{_instantiated};
Branch: 2 branches
BranchTrueFalseTotal
if $Project::Easy::singleton->{"_instantiated"} 0 2 2
if $Project::Easy::singleton->{"_instantiated"} 0 6 6
83
84 1 debug "here we try to detect package location, "
85 . "because this package isn't for public distribution "
86 . "and must live in <project directory>/lib";
87
88 1 $class->detect_environment;
89  
90 0 my $db_package = $class->db_package;
91 0 try_to_use ($db_package);
92
93 2 bless $singleton, $class;
94
95 2 $singleton->{_instantiated} = $class;
96  
97 2 my $config = $singleton->config;
98  
99 2 foreach my $datasource (keys %{$config->{db}}) {
100 2 my $datasource_package = $config->{db}->{$datasource}->{package};
101
102 2 ▶ if (defined $datasource_package) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $datasource_package) 0 4 4
if (defined $datasource_package) 0 9 9
103 0 try_to_use ($datasource_package);
104 }
105
106 # now we must sure to entities packages available
107 # and create new ones, if unavailable
108  
109 2 ▶ if (defined $datasource_package and $datasource_package->can ('create_entity')) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $datasource_package and $datasource_package->can('create_entity')) { } 0 4 4
if (defined $datasource_package and $datasource_package->can('create_entity')) { } 0 9 9
Condition33%2/6
defined $datasource_package and $datasource_package->can("create_entity")
!ll&&!rl&&r
400
defined $datasource_package and $datasource_package->can("create_entity")
!ll&&!rl&&r
900
MC/DC0%0/2
defined $datasource_package and $datasource_package->can("create_entity")
defined $datasource_package $datasource_package->can("create_entity")
MC/DC0%0/2
defined $datasource_package and $datasource_package->can("create_entity")
defined $datasource_package $datasource_package->can("create_entity")
Truth table33%1/3
A and B
A= defined $datasource_packageB= $datasource_package->can("create_entity")
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $datasource_packageB= $datasource_package->can("create_entity")
ABresult
0X0
100
111
110 0 $datasource_package->can ('create_entity')->($class, $class->root, $datasource);
111 } else {
112 2 Project::Easy::Helper->can ('create_entity')->($class, $class->root, $datasource);
113 }
114
115  
116 0 ▶ make_accessor ($class, "db_$datasource" => sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
117 0 ▶ return $class->db ($datasource);
Subroutine __ANON__: called CC=1 SCAR=1.2
118 2 });
119 }
120
121 2 ▶ if (exists $config->{daemons}) {
Branch: 2 branches
BranchTrueFalseTotal
if (exists $config->{"daemons"}) 0 2 2
if (exists $config->{"daemons"}) 0 6 6
122
123 0 my $d_pack = $class->daemon_package;
124 0 try_to_use ($d_pack);
125
126 0 foreach my $d_name (keys %{$config->{daemons}}) {
127 0 my $d_conf = $config->{daemons}->{$d_name};
128
129 0 my $d;
130
131 0 ▶ if ($d_conf->{package}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($d_conf->{'package'}) { } 0 0 0
if ($d_conf->{'package'}) { } 0 0 0
132 0 try_to_use ($d_conf->{package});
133 0 $d = $d_conf->{package}->new ($singleton, $d_name, $d_conf);
134 } else {
135 0 $d = $d_pack->new ($singleton, $d_name, $d_conf);
136 }
137
138 0 $d->create_script_file ($class->root);
139
140 0 $singleton->daemons->{$d_name} = $d;
141 }
142 }
143
144 0 return $singleton;
145 }
146  
147 6 ▶sub detect_environment {
Subroutine detect_environment: called CC=6 SCAR=20.5
Pod undocumented
148 4 ▶ my $class = shift;
Subroutine detect_environment: called CC=6 SCAR=20.5
Pod undocumented
149
150 2 my $root = $class->root;
151
152 0 my $distro_path = $root->append ('var', 'distribution');
153 0 my $instance_path = $root->append ('var', 'instance');
154
155 0 ▶ if (-f $distro_path and ! -f $instance_path) {
Branch: 2 branches
BranchTrueFalseTotal
if (-f $distro_path and not -f $instance_path) 0 2 2
if (-f $distro_path and not -f $instance_path) 0 6 6
Condition33%2/6
-f $distro_path and not -f $instance_path
!ll&&!rl&&r
200
-f $distro_path and not -f $instance_path
!ll&&!rl&&r
600
MC/DC0%0/2
-f $distro_path and not -f $instance_path
-f $distro_path not -f $instance_path
MC/DC0%0/2
-f $distro_path and not -f $instance_path
-f $distro_path not -f $instance_path
Truth table33%1/3
A and not(B)
A= -f $distro_pathB= not -f $instance_path
ABresult
0X0
100
111
Truth table33%1/3
A and not(B)
A= -f $distro_pathB= not -f $instance_path
ABresult
0X0
100
111
156 0 rename $distro_path, $instance_path
157 }
158
159 0 my @fixups = ();
160 24 ▶ $root->dir_io ($class->etc)->scan_tree (sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
161 2 ▶ my $f = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
162 2 ▶ push @fixups, $f->name
Branch: 2 branches
BranchTrueFalseTotal
if -d $f 2 6 8
if -d $f 6 18 24
163 if -d $f;
164 0 });
165
166 0 my $ending = ".\nprobably you want to set '"
167 . $instance_path->rel_path (dir->current) . '\' contents to fixup config dir name (available fixups: '
168 . join (', ', @fixups).")\n";
169
170 0 ▶ die "instance file not found" . $ending
Branch: 2 branches
BranchTrueFalseTotal
unless -f $instance_path 0 2 2
unless -f $instance_path 0 6 6
171 unless -f $instance_path;
172
173 0 my $instance_string = $instance_path->as_file->contents;
174
175 0 $instance_string =~ s/[\s\n\t\r]+$//;
176  
177 0 ▶ die "can't recognise instance name '$instance_string'" . $ending
Branch: 2 branches
BranchTrueFalseTotal
unless $instance_string 0 2 2
unless $instance_string 0 6 6
178 unless $instance_string;
179
180 0 my ($instance, $fixup_core) = split (/:/, $instance_string, 2); # windows workaround
181
182 2 make_accessor ($class, 'instance', default => $instance);
183 38 make_accessor ($class, 'fixup_core', default => $fixup_core);
184
185 2 my $fixup_path = $class->fixup_path_instance;
186  
187 2 ▶ die "can't locate fixup config file at '$fixup_path'" . $ending
Branch: 2 branches
BranchTrueFalseTotal
unless -f $fixup_path 0 2 2
unless -f $fixup_path 0 6 6
188 unless -f $fixup_path;
189
190 2 make_accessor ($class, 'fixup_path', default => $fixup_path);
191
192 }
193  
194 15 ▶sub fixup_path_instance {
Subroutine fixup_path_instance: called CC=4 SCAR=15.1
Pod undocumented
195 0 ▶ my $self = shift;
Subroutine fixup_path_instance: called CC=4 SCAR=15.1
Pod undocumented
196 2 ▶ my $instance = shift || $self->instance;
Condition50%3/6
shift() || $self->instance
l!l&&r!l&&!r
040
shift() || $self->instance
l!l&&r!l&&!r
3120
MC/DC0%0/2
shift() || $self->instance
shift() $self->instance
MC/DC0%0/2
shift() || $self->instance
shift() $self->instance
Truth table33%1/3
A || B
A= shift()B= $self->instance
ABresult
1X1
011
000
Truth table66%2/3
A || B
A= shift()B= $self->instance
ABresult
1X1
011
000
197
198 2 my $fixup_core = $self->fixup_core;
199
200 2 my $fixup_path;
201
202 2 ▶ if (defined $fixup_core and $fixup_core) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $fixup_core and $fixup_core) { } 0 4 4
if (defined $fixup_core and $fixup_core) { } 0 15 15
Condition33%2/6
defined $fixup_core and $fixup_core
!ll&&!rl&&r
400
defined $fixup_core and $fixup_core
!ll&&!rl&&r
1500
MC/DC0%0/2
defined $fixup_core and $fixup_core
defined $fixup_core $fixup_core
MC/DC0%0/2
defined $fixup_core and $fixup_core
defined $fixup_core $fixup_core
Truth table33%1/3
A and B
A= defined $fixup_coreB= $fixup_core
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $fixup_coreB= $fixup_core
ABresult
0X0
100
111
203 0 $fixup_path = IO::Easy->new ($fixup_core)->append ($instance);
204 } else {
205 2 $fixup_path = $self->root->append ($self->etc, $instance);
206 }
207
208 2 $fixup_path = $fixup_path->append ($self->id . '.' . $self->conf_format)->as_file;
209
210 2 bless ($fixup_path, 'Project::Easy::Config::File');
211 }
212  
213 25 ▶sub config {
Subroutine config: called CC=4 SCAR=15.2
Pod documented
214 2 ▶ my $class = shift;
Subroutine config: called CC=4 SCAR=15.2
Pod documented
215
216 2 ▶ if (@_ > 0) { # get config for another instance, do not cache
Branch: 2 branches
BranchTrueFalseTotal
if (@_ > 0) 0 101 101
if (@_ > 0) 0 25 25
217 0 my $config = $class->conf_package->parse (
218 $singleton, @_
219 );
220
221 # reparse config
222 0 ▶ if ($_[0] eq $class->instance) {
Branch: 2 branches
BranchTrueFalseTotal
if ($_[0] eq $class->instance) 0 0 0
if ($_[0] eq $class->instance) 0 0 0
223 0 $singleton->{config} = $config
224 }
225
226 0 return $config
227 }
228
229 8 ▶ unless ($singleton->{config}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($Project::Easy::singleton->{"config"}) 2 99 101
unless ($Project::Easy::singleton->{"config"}) 6 19 25
230 4 $singleton->{config} = $class->conf_package->parse (
231 $singleton
232 );
233 }
234
235 4 return $singleton->{config};
236 }
237  
238 3 ▶sub _detect_entity {
Subroutine _detect_entity: called CC=5 SCAR=16.6
239 4 ▶ my $self = shift;
Subroutine _detect_entity: called CC=5 SCAR=16.6
240 4 my $name = shift;
241
242 # here we need to convert supplied name to outstanding format.
243 # name must be in datasource representation (oracle_article) instead of entity (OracleArticle)
244
245 4 my $ds = Project::Easy::Helper::table_from_package ($name);
246
247 # for example: $name = 'oracle_article'
248 # $ds_config_name - datasource name in configuration (oracle)
249 # $ds_path - for table name in RDBMS (article)
250 # $entity_name - entity name in perl package name (OracleArticle)
251 # $ds_entity_name - datasource name in perl package name (Oracle)
252 0 my ($ds_config_name, $ds_path, $entity_name, $ds_entity_name);
253
254 4 $ds_config_name = 'default';
255 4 $ds_entity_name = '';
256 4 $ds_path = $ds;
257 101 $entity_name = Project::Easy::Helper::package_from_table ($ds);
258  
259 # next, we want to check table name against entity datasource prefixes
260 0 foreach my $k (grep {!/^default$/} keys %{$self->config->{db}}) {
261 # my $qk = Project::Easy::Helper::package_from_table ($k);
262
263 0 ▶ if (index ($ds, $k) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (index($ds, $k) == 0) 4 3 7
if (index($ds, $k) == 0) 3 0 3
264 0 my $separator = substr ($ds, length($k), 1);
265 101 ▶ if ($separator eq '_' or $separator eq ':') {
Branch: 2 branches
BranchTrueFalseTotal
if ($separator eq "_" or $separator eq ":") 4 0 4
if ($separator eq "_" or $separator eq ":") 3 0 3
Condition33%2/6
$separator eq "_" or $separator eq ":"
l!l&&r!l&&!r
400
$separator eq "_" or $separator eq ":"
l!l&&r!l&&!r
300
MC/DC0%0/2
$separator eq "_" or $separator eq ":"
$separator eq "_" $separator eq ":"
MC/DC0%0/2
$separator eq "_" or $separator eq ":"
$separator eq "_" $separator eq ":"
Truth table33%1/3
A or B
A= $separator eq "_"B= $separator eq ":"
ABresult
1X1
011
000
Truth table33%1/3
A or B
A= $separator eq "_"B= $separator eq ":"
ABresult
1X1
011
000
266 2 $ds_config_name = $k;
267 101 $ds_entity_name = Project::Easy::Helper::package_from_table ($k);
268 7 $ds_path = substr ($ds, length ($k) + 1);
269 7 $entity_name = Project::Easy::Helper::package_from_table ($ds);
270 7 last;
271 }
272 }
273 }
274
275 7 debug "datasource: $ds, entity name: $entity_name, datasource path: $ds_path, datasource entity name: $ds_entity_name, datasource config key: $ds_config_name";
276
277 7 return ($entity_name, $ds_path, $ds_entity_name, $ds_config_name);
278 }
279  
280 0 ▶sub entity {
Subroutine entity: not called CC=2 SCAR=10.5
Pod documented
281 0 ▶ my $self = shift;
Subroutine entity: called CC=2 SCAR=10.5
Pod documented
282 0 my $name = shift;
283
284 # TODO: make cache for entities
285
286 # try to detect entity by prefix
287 0 my ($entity_name, $ds_path, $ds_entity_name, $ds_config_name) = $self->_detect_entity ($name);
288
289 0 my $ds_config = $self->config->{db}->{$ds_config_name};
290
291 0 ▶ my $ds_package = $ds_config->{package} || $self->db_package;
Condition16%1/6
$ds_config->{'package'} || $self->db_package
l!l&&r!l&&!r
020
$ds_config->{'package'} || $self->db_package
l!l&&r!l&&!r
000
MC/DC0%0/2
$ds_config->{'package'} || $self->db_package
$ds_config->{'package'} $self->db_package
MC/DC0%0/2
$ds_config->{'package'} || $self->db_package
$ds_config->{'package'} $self->db_package
Truth table33%1/3
A || B
A= $ds_config->{'package'}B= $self->db_package
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $ds_config->{'package'}B= $self->db_package
ABresult
1X1
011
000
292
293 0 debug "datasource package: $ds_package";
294
295 0 $ds_package->entity ($name, $entity_name, $ds_path, $ds_entity_name, $ds_config_name);
296 }
297  
298 0 ▶sub collection {
Subroutine collection: not called CC=2 SCAR=10.5
Pod undocumented
299 0 ▶ my $self = shift;
Subroutine collection: called CC=2 SCAR=10.5
Pod undocumented
300 0 my $name = shift;
301
302 # TODO: make cache for entities
303
304 # try to detect entity by prefix
305 0 my ($entity_name, $ds_path, $ds_entity_name, $ds_config_name) = $self->_detect_entity ($name);
306
307 0 my $ds_config = $self->config->{db}->{$ds_config_name};
308
309 0 ▶ my $ds_package = $ds_config->{package} || $self->db_package;
Condition16%1/6
$ds_config->{'package'} || $self->db_package
l!l&&r!l&&!r
030
$ds_config->{'package'} || $self->db_package
l!l&&r!l&&!r
000
MC/DC0%0/2
$ds_config->{'package'} || $self->db_package
$ds_config->{'package'} $self->db_package
MC/DC0%0/2
$ds_config->{'package'} || $self->db_package
$ds_config->{'package'} $self->db_package
Truth table33%1/3
A || B
A= $ds_config->{'package'}B= $self->db_package
ABresult
1X1
011
000
Truth table0%0/3
A || B
A= $ds_config->{'package'}B= $self->db_package
ABresult
1X1
011
000
310
311 0 debug "datasource package: $ds_package";
312
313 0 $ds_package->collection ($name, $entity_name, $ds_path, $ds_entity_name, $ds_config_name);
314 }
315  
316  
317 0 ▶sub daemon {
Subroutine daemon: not called CC=1 SCAR=1.2
Pod undocumented
318 0 ▶ my $core = shift;
Subroutine daemon: called CC=1 SCAR=1.2
Pod undocumented
319 0 my $code = shift;
320
321 0 return $core->daemons->{$code};
322 }
323  
324 3 ▶sub db { # TODO: rewrite using alert
Subroutine db: called CC=10 SCAR=26.9
Pod documented
325 2 ▶ my $class = shift;
Subroutine db: called CC=10 SCAR=26.9
Pod documented
326 2 ▶ my $type = shift || 'default';
Condition50%2/4
shift() || 'default'
l!l
400
shift() || 'default'
l!l
30
MC/DC0%0/1
shift() || 'default'
shift()
MC/DC0%0/1
shift() || 'default'
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
Truth table50%1/2
A
A= shift()
Aresult
11
00
327
328 2 my $core = $class->singleton; # fetch current process singleton
329
330 $core->{db}->{$type} = {ts => {}}
331 2 ▶ unless $core->{db}->{$type};
Branch: 2 branches
BranchTrueFalseTotal
unless $core->{"db"}{$type} 4 36 40
unless $core->{"db"}{$type} 3 0 3
332
333 2 my $current_db = $core->{db}->{$type};
334  
335 2 ▶ my $db_package = $core->config->{db}->{$type}->{package} || $core->db_package;
Condition33%2/6
$core->config->{'db'}{$type}{'package'} || $core->db_package
l!l&&r!l&&!r
0400
$core->config->{'db'}{$type}{'package'} || $core->db_package
l!l&&r!l&&!r
030
MC/DC0%0/2
$core->config->{'db'}{$type}{'package'} || $core->db_package
$core->config->{'db'}{$type}{'package'} $core->db_package
MC/DC0%0/2
$core->config->{'db'}{$type}{'package'} || $core->db_package
$core->config->{'db'}{$type}{'package'} $core->db_package
Truth table33%1/3
A || B
A= $core->config->{'db'}{$type}{'package'}B= $core->db_package
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $core->config->{'db'}{$type}{'package'}B= $core->db_package
ABresult
1X1
011
000
336  
337 3 ▶ unless ($current_db->{$$}) {
Branch: 2 branches
BranchTrueFalseTotal
unless ($current_db->{$$}) 4 36 40
unless ($current_db->{$$}) 3 0 3
338
339 0 ▶ $DBI::Easy::ERRHANDLER = sub {
Subroutine __ANON__: not called CC=1 SCAR=1.2
340 0 ▶ debug '%%%%%%%%%%%%%%%%%%%%%% DBI ERROR: we relaunch connection %%%%%%%%%%%%%%%%%%%%%%%%';
Subroutine __ANON__: called CC=1 SCAR=1.2
341 0 debug 'ERROR: ', $@;
342
343 0 $class->_connect_db ($current_db, $db_package, $type, 1);
344
345 0 return $class->db ($type);
346 3 };
347
348 3 my $t = timer ("database handle start");
349  
350 3 $class->_connect_db ($current_db, $db_package, $type);
351  
352 3 $t->end;
353
354 }
355
356 # we reconnect every hour for morning bug by default
357 3 my $force_reconnect = $core->config->{db}->{$type}->{force_reconnect};
358 3 ▶ $force_reconnect = 0
Branch: 2 branches
BranchTrueFalseTotal
unless defined $force_reconnect 40 0 40
unless defined $force_reconnect 3 0 3
359 unless defined $force_reconnect;
360 0 ▶ $force_reconnect = 3600
Branch: 2 branches
BranchTrueFalseTotal
if $force_reconnect != 0 and $force_reconnect < 3600 0 40 40
if $force_reconnect != 0 and $force_reconnect < 3600 0 3 3
Condition33%2/6
$force_reconnect != 0 and $force_reconnect < 3600
!ll&&!rl&&r
4000
$force_reconnect != 0 and $force_reconnect < 3600
!ll&&!rl&&r
300
MC/DC0%0/2
$force_reconnect != 0 and $force_reconnect < 3600
$force_reconnect != 0 $force_reconnect < 3600
MC/DC0%0/2
$force_reconnect != 0 and $force_reconnect < 3600
$force_reconnect != 0 $force_reconnect < 3600
Truth table33%1/3
A and B
A= $force_reconnect != 0B= $force_reconnect < 3600
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $force_reconnect != 0B= $force_reconnect < 3600
ABresult
0X0
100
111
361 if $force_reconnect != 0 and $force_reconnect < 3600;
362 0 ▶ if ($force_reconnect != 0 and (time - $current_db->{ts}->{$$}) > $force_reconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if ($force_reconnect != 0 and time - $current_db->{"ts"}{$$} > $force_reconnect) 0 40 40
if ($force_reconnect != 0 and time - $current_db->{"ts"}{$$} > $force_reconnect) 0 3 3
Condition33%2/6
$force_reconnect != 0 and time - $current_db->{"ts"}{$$} > $force_reconnect
!ll&&!rl&&r
4000
$force_reconnect != 0 and time - $current_db->{"ts"}{$$} > $force_reconnect
!ll&&!rl&&r
300
MC/DC0%0/2
$force_reconnect != 0 and time - $current_db->{"ts"}{$$} > $force_reconnect
$force_reconnect != 0 time - $current_db->{"ts"}{$$} > $force_reconnect
MC/DC0%0/2
$force_reconnect != 0 and time - $current_db->{"ts"}{$$} > $force_reconnect
$force_reconnect != 0 time - $current_db->{"ts"}{$$} > $force_reconnect
Truth table33%1/3
A and B
A= $force_reconnect != 0B= time - $current_db->{"ts"}{$$} > $force_reconnect
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= $force_reconnect != 0B= time - $current_db->{"ts"}{$$} > $force_reconnect
ABresult
0X0
100
111
363 0 debug "forced reconnect";
364 0 $class->_connect_db ($current_db, $db_package, $type, 1);
365 }
366
367 40 return $current_db->{$$};
368
369 }
370  
371 3 ▶sub _connect_db {
Subroutine _connect_db: called CC=6 SCAR=25.7
372 40 ▶ my $class = shift;
Subroutine _connect_db: called CC=6 SCAR=25.7
373 4 my ($current_db, $db_package, $type, $disconnect) = @_;
374
375 4 my $core = $class->singleton;
376
377 4 my $old_dbh = delete $current_db->{$$};
378  
379 4 ▶ if (defined $disconnect and $disconnect) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $disconnect and $disconnect) 0 4 4
if (defined $disconnect and $disconnect) 0 3 3
Condition33%2/6
defined $disconnect and $disconnect
!ll&&!rl&&r
400
defined $disconnect and $disconnect
!ll&&!rl&&r
300
MC/DC0%0/2
defined $disconnect and $disconnect
defined $disconnect $disconnect
MC/DC0%0/2
defined $disconnect and $disconnect
defined $disconnect $disconnect
Truth table33%1/3
A and B
A= defined $disconnectB= $disconnect
ABresult
0X0
100
111
Truth table33%1/3
A and B
A= defined $disconnectB= $disconnect
ABresult
0X0
100
111
380 # basic windows support
381 0 eval {
382 0 ▶ if ($^O ne 'MSWin32') {
Branch: 2 branches
BranchTrueFalseTotal
if ($^O ne "MSWin32") 0 0 0
if ($^O ne "MSWin32") 0 0 0
383 0 ▶ my $h = Sys::SigAction::set_sig_handler('ALRM', sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
384 # failed disconnect is safe solution
385 0 ▶ die;
Subroutine __ANON__: called CC=1 SCAR=6.9
386 0 });
387 0 alarm (2);
388 }
389
390 $old_dbh->disconnect
391 0 ▶ if $old_dbh;
Branch: 2 branches
BranchTrueFalseTotal
if $old_dbh 0 0 0
if $old_dbh 0 0 0
392
393 0 ▶ alarm (0)
Branch: 2 branches
BranchTrueFalseTotal
if $^O ne "MSWin32" 0 0 0
if $^O ne "MSWin32" 0 0 0
394 if $^O ne 'MSWin32';
395 };
396 }
397  
398 40 $current_db->{$$} = $db_package->new ($core, $type);
399 0 $current_db->{ts}->{$$} = time;
400  
401 }
402  
403  
404  
405 6 1;
406  
407 =head1 NAME
408  
409 Project::Easy - project deployment made easy.
410  
411 =head1 SYNOPSIS
412  
413 package Caramba;
414  
415 use Class::Easy;
416  
417 use Project::Easy;
418 use base qw(Project::Easy);
419  
420 has 'id', default => 'caramba';
421 has 'conf_format', default => 'json';
422  
423 my $class = __PACKAGE__;
424  
425 has 'entity_prefix', default => join '::', $class, 'Entity', '';
426  
427 $class->init;
428  
429 =head1 ACCESSORS
430  
431 =head2 singleton
432  
433 =over 4
434  
435 =item singleton
436  
437 return class instance
438  
439 =cut
440  
441 =head2 configurable options
442  
443 =over 4
444  
445 =item id
446  
447 project id
448  
449 =item conf_format
450  
451 default config file format
452  
453 =item daemon_package
454  
455 interface for daemon creation
456  
457 default => 'Project::Easy::Daemon'
458  
459 =item db_package
460  
461 interface for db connections creation
462  
463 default => 'Project::Easy::DB'
464  
465 =item conf_package
466  
467 configuration interface
468  
469 default => 'Project::Easy::Config';
470  
471 =item default configuration directory
472  
473 has 'etc', default => 'etc';
474  
475 =item default binary directory
476  
477 has 'bin', default => 'bin';
478  
479 =cut
480  
481 =head2 autodetect options
482  
483 =over 4
484  
485 =item root
486  
487 IO::Easy object for project root directory
488  
489 =item instance
490  
491 string contains current project instance name
492  
493 =item fixup_core
494  
495 path (string) to configuration fixup root
496  
497 =item conf_path
498  
499 path object to the global configuration file
500  
501 =item fixup_path
502  
503 path object to the local configuration file
504  
505 =cut
506  
507 =head1 METHODS
508  
509 =head2 config
510  
511 return configuration object
512  
513 =head2 db
514  
515 database pool
516  
517 =cut
518  
519 =head1 ENTITIES
520  
521 =over 4
522  
523 =item intro
524  
525 Project::Easy create default entity classes on initialization.
526 this entity based on default database connection. you can use
527 this connection (not recommended) within modules by mantra:
528  
529 my $core = <project_namespace>->singleton;
530 my $dbh = $core->db;
531  
532 method db return default $dbh. you can use non-default dbh named 'cache' by calling:
533  
534 my $dbh_cache = $core->db ('cache');
535  
536 or
537 my $dbh_cache = $core->db_cache;
538  
539 if DBI::Easy default API satisfy you, then you can use database entities
540 by calling
541  
542 my $account_record = $core->entity ('Account');
543 my $account_collection = $core->collection ('Account');
544
545 my $all_accounts = $account_collection->new->list;
546  
547 in this case, virtual packages created for entity 'account'.
548  
549 or you can create these packages by hand:
550  
551 package <project_namespace>::Entity::Account;
552
553 use Class::Easy;
554
555 use base qw(<project_namespace>::Entity::Record);
556
557 1;
558  
559 and for collection:
560  
561 package <project_namespace>::Entity::Account::Collection;
562  
563 use Class::Easy;
564  
565 use base qw(<project_namespace>::Entity::Collection);
566  
567 1;
568  
569 in this case
570  
571 my $account_record = $core->entity ('Account');
572 my $account_collection = $core->collection ('Account');
573
574 also works for you
575  
576 =cut
577  
578 =item creation another database entity class
579  
580 TODO: creation by script
581  
582 =cut
583  
584 =item using entities from multiple databases
585  
586 TODO: read database tables and create entity mappings,
587 each entity subclass must contain converted database identifier:
588  
589 default entity, table account_settings => entity AccountSettings
590 'cache' entity, table account_settings => entity CacheAccountSettings
591
592  
593 =cut
594  
595  
596 =head1 AUTHOR
597  
598 Ivan Baktsheev, C<< <apla at the-singlers.us> >>
599  
600 =head1 BUGS
601  
602 Please report any bugs or feature requests to my email address,
603 or through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Project-Easy>.
604 I will be notified, and then you'll automatically be notified
605 of progress on your bug as I make changes.
606  
607 =head1 SUPPORT
608  
609  
610  
611 =head1 ACKNOWLEDGEMENTS
612  
613  
614  
615 =head1 COPYRIGHT & LICENSE
616  
617 Copyright 2007-2009 Ivan Baktsheev
618  
619 This program is free software; you can redistribute it and/or modify it
620 under the same terms as Perl itself.
621  
622  
623 =cut
« lib/Project/Easy/Helper/Status.pm ↑ Summary lib/Project/Easy/Helper/Console.pm »