diff options
author | Wolfgang Karall-Ahlborn <github@karall-edv.at> | 2021-03-07 19:56:57 +0300 |
---|---|---|
committer | Wolfgang Karall-Ahlborn <github@karall-edv.at> | 2021-03-07 19:56:57 +0300 |
commit | 2edcc8de4238fe7cd7950a4128f075bade5f3a92 (patch) | |
tree | 092e0124ee911fa1a19785bd810710de3c69d4b9 | |
parent | 1b80ad21e743813256d7e20854b109bde652d799 (diff) |
Use DB transaction to avoid incomplete records being added.
If one record in the report can't be processed, the entry in table
'report' will stay in the DB, as well as any other entries in
'rptrecord' from records that were processed successfully.
This leads to an misleading error
'Skipping IMAP message with UID #5012 due to database errors.'
because the next time the IMAP client processes the message, it'll find
the entry in table 'report' from the previous incomplete processing and
issue
'Already have <report-org> <report-id>, skipped'
and handle the report accordingly, for example move it to the IMAP folder
configured in $imapmovefolder.
-rwxr-xr-x | dmarcts-report-parser.pl | 22 |
1 files changed, 21 insertions, 1 deletions
diff --git a/dmarcts-report-parser.pl b/dmarcts-report-parser.pl index 2de3b04..ddc8367 100755 --- a/dmarcts-report-parser.pl +++ b/dmarcts-report-parser.pl @@ -117,12 +117,13 @@ sub show_usage { # Define all possible configuration options. our ($debug, $delete_reports, $delete_failed, $reports_replace, $maxsize_xml, $compress_xml, - $dbname, $dbuser, $dbpass, $dbhost, $dbport, + $dbname, $dbuser, $dbpass, $dbhost, $dbport, $db_tx_support, $imapserver, $imapport, $imapuser, $imappass, $imapignoreerror, $imapssl, $imaptls, $imapmovefolder, $imapmovefoldererr, $imapreadfolder, $imapopt, $tlsverify, $processInfo); # defaults $maxsize_xml = 50000; +$db_tx_support = 1; # Load script configuration options from local config file. The file is expected # to be in the current working directory. @@ -744,6 +745,14 @@ sub storeXMLInDatabase { $policy_pct = $xml->{'policy_published'}[0]->{'pct'}; } + # begin transaction + if ($db_tx_support) { + $dbh->do(qq{START TRANSACTION}); + if ($dbh->errstr) { + print "Cannot start transaction (" . $dbh->errstr ."). Continuing without transaction support.\n"; + $db_tx_support = 0; + } + } # see if already stored my $sth = $dbh->prepare(qq{SELECT org, serial FROM report WHERE reportid=?}); $sth->execute($id); @@ -935,6 +944,17 @@ sub storeXMLInDatabase { print "Result $res XML: $xml->{raw_xml}\n"; } + if ($res <= 0) { + if ($db_tx_support) { + print "Cannot add records to rptrecord. Rolling back DB transaction.\n"; + $dbh->do(qq{ROLLBACK}); + if ($dbh->errstr) { + print "Cannot rollback transaction (" . $dbh->errstr .").\n"; + } + } else { + print "Warning: errors while adding to rptrecord, serial $serial records likely obsolete.\n"; + } + } return $res; } |