Skip to content

Commit 37be00b

Browse files
committed
Enkel feilhåndtering og litt penere output
1 parent d960dae commit 37be00b

File tree

2 files changed

+33
-17
lines changed

2 files changed

+33
-17
lines changed

mailrouter/in/test.msg

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
placeholder

mailrouter/move_emails.pl

+32-17
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,19 @@
77
use MIME::Parser;
88
use autodie;
99

10-
my ($inputDirectory, $newEmottakDirectory, $oldEmottakDirectory) = @ARGV;
10+
my ($inputDirectory, $newEmottakDirectory, $oldEmottakDirectory, $errorDirectory) = @ARGV;
1111

1212
if (
1313
not defined $inputDirectory or
1414
not defined $newEmottakDirectory or
15-
not defined $oldEmottakDirectory
15+
not defined $oldEmottakDirectory or
16+
not defined $errorDirectory
1617
) {
17-
die "Need directory parameters (call with command line arguments 'inputDirectory' 'newEmottakDirectory' 'oldEmottakDirectory'\n";
18+
die "Need directory parameters (call with command line arguments 'inputDirectory' 'newEmottakDirectory' 'oldEmottakDirectory' 'errorDirectory\n";
1819
}
1920

2021
my $dryRunMode = 1;
22+
2123
if ($dryRunMode ne 0) {
2224
print "OBS! dryRunMode active, will not actually move any files!\n";
2325
}
@@ -28,14 +30,15 @@
2830
'testType' => ["testService", "testAction"]
2931
);
3032

31-
printf "Input directory : %s\n", $inputDirectory;
32-
printf "New eMottak directory : %s\n", $newEmottakDirectory;
33-
printf "Old eMottak directory : %s\n", $oldEmottakDirectory;
33+
printf "Input directory : %s\n", $inputDirectory;
34+
printf "New eMottak directory : %s\n", $newEmottakDirectory;
35+
printf "Old eMottak directory : %s\n\n", $oldEmottakDirectory;
3436

3537
opendir(DIR, $inputDirectory) or die "Can't open $inputDirectory: $!";
3638

3739
my $newCounter = 0;
3840
my $oldCounter = 0;
41+
my $errorCounter = 0;
3942
my $fileCounter = 0;
4043

4144
foreach my $filename (readdir(DIR)) {
@@ -44,15 +47,25 @@
4447
my $parser = MIME::Parser->new;
4548
$parser->output_to_core(1); #ikke skriv fil til disk
4649

47-
# Leser epost
48-
my $entity = $parser->parse_open("$inputDirectory/$filename");
49-
# $entity->dump_skeleton();
50-
my $first_part = $entity->parts(0);
51-
my $body = (defined $first_part) ? $first_part->bodyhandle->as_string : $entity->bodyhandle->as_string;
52-
53-
# Leser ebxml dokument
54-
my $xml_parser = XML::LibXML->new;
55-
my $dom = $xml_parser->parse_string($body);
50+
my $dom = eval {
51+
# Leser epost
52+
my $entity = $parser->parse_open("$inputDirectory/$filename");
53+
# $entity->dump_skeleton();
54+
my $first_part = $entity->parts(0);
55+
my $body = (defined $first_part) ? $first_part->bodyhandle->as_string : $entity->bodyhandle->as_string;
56+
57+
# Leser ebxml dokument
58+
my $xml_parser = XML::LibXML->new;
59+
$xml_parser->parse_string($body);
60+
};
61+
if ($@ ne '') {
62+
if ($dryRunMode eq 0) {
63+
move("$inputDirectory/$filename", "$errorDirectory/$filename");
64+
}
65+
printf "Message '%s' failed with error %s", $filename, $@;
66+
$errorCounter++;
67+
next;
68+
};
5669

5770
my $xpc = XML::LibXML::XPathContext->new($dom);
5871
$xpc->registerNs('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
@@ -87,7 +100,9 @@
87100
}
88101
}
89102

90-
printf "%s of %s files moved to new system\n", $newCounter, $fileCounter;
91-
printf "%s of %s files moved to old system\n", $oldCounter, $fileCounter;
103+
printf "\nMessages processed : %s/%s\n", $newCounter+$oldCounter+$errorCounter, $fileCounter;
104+
printf "Messages sent to new system : %s\n", $newCounter;
105+
printf "Messages sent to old system : %s\n", $oldCounter;
106+
printf "Messages sent to error : %s\n", $errorCounter;
92107

93108
closedir(DIR);

0 commit comments

Comments
 (0)