LP1893463: Prevent duplicated report outputs
authorJason Boyer <JBoyer@equinoxinitiative.org>
Fri, 28 Aug 2020 21:13:14 +0000 (17:13 -0400)
committerChris Sharp <csharp@georgialibraries.org>
Wed, 7 Oct 2020 18:51:18 +0000 (14:51 -0400)
When re-running recurring reports that are interrupted,
it's possible to end up with a duplicate copy of the next
recurrance of that report. This branch adds a unique index
to reporter.schedule and tells clark-kent.pl not to worry
about it.

For the purposes of this change a duplicate report is
one where report.schedule has identical values for report,
(output) folder, runner, run_time, and email.

Signed-off-by: Jason Boyer <JBoyer@equinoxinitiative.org>
Signed-off-by: Chris Sharp <csharp@georgialibraries.org>
Open-ILS/src/reporter/clark-kent.pl
Open-ILS/src/sql/Pg/reporter-schema.sql
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.reporter_schedule_uniqueness.sql [new file with mode: 0644]

index 320ee8c..9db6778 100755 (executable)
@@ -250,7 +250,7 @@ for my $r ( @reports ) {
                          WHERE id = ?;
                SQL
 
-           $logger->debug('Report SQL: ' . $r->{resultset}->toSQL);
+               $logger->debug('Report SQL: ' . $r->{resultset}->toSQL);
                $sth = $data_dbh->prepare($r->{resultset}->toSQL);
 
                $sth->execute;
@@ -314,7 +314,9 @@ for my $r ( @reports ) {
                                        VALUES ( ?, ?, ?, ?::TIMESTAMPTZ + ?, ?, ?, ?, ?, ?, ?, ? );
                        SQL
 
-                       $state_dbh->do(
+                       my $prevP = $state_dbh->{PrintError};
+                       $state_dbh->{PrintError} = 0;
+                       if (!$state_dbh->do(
                                $sql,
                                {},
                                $r->{report}->{id},
@@ -329,7 +331,11 @@ for my $r ( @reports ) {
                                $r->{chart_pie},
                                $r->{chart_bar},
                                $r->{chart_line},
-                       );
+                       )) {
+                               # Ignore duplicate key errors on reporter.schedule (err 7 is a fatal query error). Just look for the constraint name in the message to avoid l10n issues.
+                               warn($state_dbh->errstr()) unless $state_dbh->err() == 7 && $state_dbh->errstr() =~ m/rpt_sched_recurrence_once_idx/;
+                       }
+                       $state_dbh->{PrintError} = $prevP;
                }
 
                $state_dbh->do(<<'              SQL',{}, $r->{id});
index 4ed0047..eb07999 100644 (file)
@@ -110,6 +110,7 @@ CREATE TABLE reporter.schedule (
 );
 CREATE INDEX rpt_sched_runner_idx ON reporter.schedule (runner);
 CREATE INDEX rpt_sched_folder_idx ON reporter.schedule (folder);
+CREATE UNIQUE INDEX rpt_sched_recurrence_once_idx ON reporter.schedule (report,folder,runner,run_time,email);
 
 CREATE OR REPLACE VIEW reporter.simple_record AS
 SELECT r.id,
diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.reporter_schedule_uniqueness.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.reporter_schedule_uniqueness.sql
new file mode 100644 (file)
index 0000000..1a2b22f
--- /dev/null
@@ -0,0 +1,9 @@
+BEGIN;
+
+SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version);
+
+-- Explicitly supply the name because it is referenced in clark-kent.pl
+CREATE UNIQUE INDEX rpt_sched_recurrence_once_idx ON reporter.schedule (report,folder,runner,run_time,email);
+
+COMMIT;
+