ETLp/Execute/Iteration.pm untested

filter: Statementstmt 0.0% 0 / 84 Branchbran 0.0% 0 / 18 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 0.0% 0 / 8 Podpod n/a totaltotal 0.0% 0 / 132 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ETLp::Execute::Iteration;
2  
3 use MooseX::Declare;
4  
5 =head1 NAME
6  
7 ETLp::Execute::Iteration - Execute an Iterative Pipeline
8  
9 =head1 DESCRIPTION
10  
11 Takes the array ref of Pipeline items and executes each item in turn
12  
13 =head1 METHODS
14  
15 =head2 new
16  
17 =head3 parameters
18  
19 All of the following parameters are mandatory
20  
21 * pipeline. an arrayref pipeline items
22 * config. The application configuration
23
24 =head3 retuns
25  
26 * void
27  
28 =cut
29  
30 class ETLp::Execute::Iteration with ETLp::Role::Config {
31 use Try::Tiny;
32 use Data::Dumper;
33 use File::LocalizeNewlines;
34 use File::Basename;
35 use File::Copy;
36 use Carp;
37 use ETLp::Exception;
38
39 has 'pipeline' => (is => 'ro', isa => 'ArrayRef', required => 1);
40 has 'config' => (is => 'ro', isa => 'HashRef', required => 1);
41
42 $Data::Dumper::Deparse = 1;
43
44 method run {
45 my $warning = 0;
46
47 try {
48 FILE_LOOP: foreach my $filename ($self->_get_source_files) {
49 ITEM_LOOP:
50 foreach my $item (@{$self->pipeline}) {
51 $self->logger->debug("Item: " . Dumper($item));
52
53 # Create an audit entry for the item
54 my $audit_item = $self->audit->create_item(
55 name => $item->{name},
56 type => $item->{type},
57 phase => $item->{phase},
58 );
59
60 # The action to be taken when an error is encountered
61 my $on_error = $item->{on_error}
62 || $self->config->{config}->{on_error}
63 || 'die';
64
65 $self->logger->debug("On error: $on_error");
66
67 if ($item->{localize}) {
68 my $f = File::LocalizeNewlines->new();
69 my $count = $f->localize($filename);
70 }
71
72 my $next_file_loop = 0;
73 my $next_item_loop = 0;
74
75 # Execute the item
76 try {
77 $self->logger->debug("Processing file: $filename");
78 $filename = $item->{runner}($filename);
79 $self->logger->debug("New file name: $filename");
80 } catch {
81 my $error = "Error processing $filename: $_";
82 my $fail_dir = $self->config->{config}->{fail_dir};
83
84 move $filename, $fail_dir ||
85 ETLpException->throw(error =>
86 "Cannot move $filename to $fail_dir: $!");
87
88 $filename = $fail_dir .'/' . basename($filename);
89
90 $audit_item->update_message(''.$error);
91 $audit_item->update_status('failed');
92
93 if ($on_error eq 'die') {
94 ETLpException->throw(error => $error);
95 }
96 $warning = 1;
97
98 $self->logger->error($error);
99
100 # Can't go to next loop item from within the catch
101 # block so set the flags to do it later
102 $next_file_loop = 1 if ($on_error eq 'skip');
103 $next_item_loop = 1 if ($on_error eq 'ignore');
104 };
105
106 next FILE_LOOP if $next_file_loop;
107 next ITEM_LOOP if $next_item_loop;
108
109 # Yay, it worked!
110 $audit_item->update_status('succeeded');
111 }
112 }
113 } catch {
114 my $error = $_;
115 if (ref $error eq 'ETLpException') {
116 $self->logger->error($error->error);
117 $self->audit->update_message($error->error);
118 } else {
119 $self->logger->error($error);
120 $self->audit->update_message($error);
121 }
122 $self->audit->update_status('failed');
123 $error->rethrow;
124 };
125
126 if ($warning) {
127 $self->audit->update_status('warning');
128 } else {
129 $self->audit->update_status('succeeded');
130 }
131 }
132
133 # Get a list of files to process
134 method _get_source_files {
135 my $incoming_dir = $self->config->{config}->{incoming_dir} ||
136 ETLpException->throw(error =>"incoming_dir is not defined");
137 my $filename_format = $self->config->{config}->{filename_format} ||
138 ETLpException->throw(error => "filename_format is not defined");
139
140 $filename_format = '\/' . $filename_format
141 unless (substr($filename_format, 0, 2) eq '\/');
142
143 $self->logger->debug('Getting the source files');
144 $self->logger->debug("Incoming dir: $incoming_dir");
145 $self->logger->debug("filename format: $filename_format");
146
147 my @all_sources_files = glob("$incoming_dir/*");
148
149 $self->logger->debug("All files: " . Dumper(\@all_sources_files));
150 my @files =
151 #map { $incoming_dir . '/' . $_ }
152 grep(/$filename_format/, @all_sources_files);
153
154 $self->logger->debug("File to load: " . Dumper(\@files));
155
156 return @files;
157 }
158 }
159  
160 =head1 LICENSE AND COPYRIGHT
161  
162 Copyright 2010 Redbone Systems Ltd
163  
164 This program is free software; you can redistribute it and/or modify it
165 under the terms of either: the GNU General Public License as published
166 by the Free Software Foundation; or the Artistic License.
167  
168 The terms are in the LICENSE file that accompanies this application
169  
170