diff options
author | TechSneeze <dave@techsneeze.com> | 2021-03-08 21:14:30 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-08 21:14:30 +0300 |
commit | 01c50cdb07c5c385178474ccd36d360c9a7757e6 (patch) | |
tree | 5e25bbf0c5ea5f06c3ce582f63be13e4e471c625 | |
parent | 1b80ad21e743813256d7e20854b109bde652d799 (diff) | |
parent | 68cea0822bea41cf894d0b64a602ff8cf7ac811a (diff) |
Merge pull request #88 from wolfgangkarall/db_tx_support
DB transaction support
-rwxr-xr-x | dmarcts-report-parser.pl | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/dmarcts-report-parser.pl b/dmarcts-report-parser.pl index 2de3b04..0f90ace 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. @@ -295,7 +296,13 @@ if ($reports_source == TS_IMAP) { $processedReport++; if ($processResult & 4) { # processXML returned a value with database error bit enabled, do nothing at all! - next; + if ($imapmovefoldererr) { + # if we can, move to error folder + moveToImapFolder($imap, $msg, $imapmovefoldererr); + } else { + # do nothing at all + next; + } } elsif ($processResult & 2) { # processXML return a value with delete bit enabled. $imap->delete_message($msg) @@ -744,6 +751,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 +950,24 @@ 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"; + } + } else { + if ($db_tx_support) { + $dbh->do(qq{COMMIT}); + if ($dbh->errstr) { + print "Cannot commit transaction (" . $dbh->errstr .").\n"; + } + } + } return $res; } |