ETLp/File/Watch.pm untested

filter: Statementstmt 0.0% 0 / 74 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 10 Podpod n/a totaltotal 0.0% 0 / 105 CC 1 SCAR 6.9
CC
1
Cov
0%
BEGIN
2.0
BEGIN
2.0
BEGIN
2.0
CRAP 2.0
1 package ETLp::File::Watch;
2  
3 use MooseX::Declare;
4  
5 =head1 NAME
6  
7 ETLp::File::Watch - Waits for the appearance of a specific file or
8 one or more files that match the supplied patterns
9  
10 =head1 SYNOPSIS
11  
12 A watcher will look for the existence of a file matching the supplied pattern
13 (glob not regex) and then exits. Generally it signals that processing is
14 required by the next item in the pipeline (which will generally want to use
15 the file that matches that pattern).
16  
17 Generally, detection of a file matching the pattern shouldn't be sufficient
18 grounds for a watcher to exit. It should poll the file size at intervals to
19 determine whether it is still growing. A watcher should only exit when the file
20 size stops changing.
21  
22 use ETLp::File::Watch;
23  
24 my $watcher = ETLp::File::Watch->new(
25 directory => "$Bin/../incoming",
26 file_pattern => 'data*.zip',
27 duration => '5h'
28 );
29
30 unless ($watcher->watch() {
31 die "file not detected";
32 }
33
34 =head1 METHODS
35  
36 =head2 new
37  
38 Creates a new watcher
39  
40 =head3 Parameters
41  
42 A hashref containing:
43  
44 * directory. Optional string. The location of the watch file
45 * file_pattern: Required string. The pattern (glob) that the watcher is
46 waiting for
47 * call: Required string. The config file and section to invoke when a file
48 is successfully found
49 * duration. Required. How long the watcher should wait before giving up. The
50 time can be specified by the duration (integer) followed by s, m, h or d -
51 (seconds, minutes, hours or days)
52 * wait_time. Optional integer. The time in seconds between each iteration of the
53 check. Defaults to 1
54 * raise_no_file_error. Optional boolean. If set to 1, an error will be raised if the
55 watcher expires without encountering a file. Deafults to 0.
56 * exit_on_detection. Optional boolean. If set to 1
57
58 =head3 Returns
59  
60 * A file watcher
61
62 =head2 watch
63  
64 Watches for the existence of a file pattern
65  
66 =head3 Parameters
67  
68 * None
69
70 =head3 Returns
71  
72 * Void
73  
74 =cut
75  
76 class ETLp::File::Watch with ETLp::Role::Config {
77 use Moose;
78 use DateTime;
79 use FindBin qw($Bin);
80 use Fcntl ':flock';
81 use ETLp::Utility::Command;
82 use ETLp::Types;
83 use ETLp::Exception;
84 use Try::Tiny;
85  
86 has 'directory' => (is => 'ro', isa => 'Str');
87 has 'file_pattern' => (is => 'rw', isa => 'Str', required => 1);
88 has 'call' => (is => 'ro', isa => 'Str', required => 1);
89 has 'wait_time' =>
90 (is => 'ro', isa => 'PositiveInt', required => 0, default => 1);
91 has 'duration' => (is => 'ro', isa => 'Str', required => 1);
92 has 'raise_no_file_error' =>
93 (is => 'ro', isa => 'Bool', required => 0, default => 0);
94 has 'exit_on_detection' =>
95 (is => 'ro', isa => 'Bool', required => 0, default => 0);
96
97 use constant UNIT_MAP => {
98 s => 'seconds',
99 m => 'minutes',
100 h => 'hours',
101 d => 'days'
102 };
103
104 method watch {
105 my $file_pattern = $self->{_file_pattern};
106 my $file_found = 0;
107
108 $self->logger->info("Waiting for file: $file_pattern");
109
110 my $dt = DateTime->now;
111
112 # Add the duration to the current time. This is when the watcher
113 # should stop
114 if ($self->duration =~ /^\s*(\d+)\s*(s|m|h|d)\s*$/) {
115 my $measure = $1;
116 my $unit = $2;
117
118 $dt->add(UNIT_MAP->{$unit} => $measure);
119 } else {
120 ETLpException->throw(
121 error => "Invalid duration: " . $self->duration
122 );
123 }
124
125 # The end time as an epoch (which is what the time function returns)
126 my $expire_time = $dt->epoch;
127
128 while (time < $expire_time) {
129
130 my @files = glob($file_pattern);
131
132 # unfortunately, globbing returns the original pattern
133 # if there is no expansion and no match, so make sure that
134 # if there is one "match" that it really is a file
135 if (scalar(@files) ==1) {
136 if (-f $files[0]) {
137 $file_found = 1;
138 $self->_run_command;
139 }
140 }
141
142 if (scalar(@files) > 1) {
143 $file_found = 1;
144 $self->_run_command;
145 }
146
147 last if ($self->exit_on_detection && $file_found);
148
149 sleep $self->wait_time;
150 }
151
152 # Raise an error if we've expired before encountering a file
153 if ($file_found == 0) {
154 if ($self->raise_no_file_error) {
155 ETLpException->throw(error => "No file found");
156 }
157 return 0;
158 }
159
160 $self->logger->info('Exiting file watcher');
161 return 1;
162 }
163
164 method _run_command {
165 my $command = "$0 " . $self->call;
166 my $os = ETLp::Utility::Command->new();
167
168 try {
169 $os->run($command)
170 } catch {
171 ETLpException->throw($_)
172 }
173 }
174
175 method BUILD {
176 $self->{_file_pattern} = $self->file_pattern;
177
178 if ($self->directory) {
179 $self->{_file_pattern} = $self->directory . '/' .
180 $self->{_file_pattern};
181 }
182 }
183 }