[Bps-public-commit] rt-extension-rest2 branch, add-cleanup-callback, created. 059ab1c61b19980694f8fd2191b28447e288f15a

Jim Brandt jbrandt at bestpractical.com
Fri Oct 27 17:00:26 EDT 2017


The branch, add-cleanup-callback has been created
        at  059ab1c61b19980694f8fd2191b28447e288f15a (commit)

- Log -----------------------------------------------------------------
commit 059ab1c61b19980694f8fd2191b28447e288f15a
Author: Jim Brandt <jbrandt at bestpractical.com>
Date:   Fri Oct 27 16:55:41 2017 -0400

    Add a CleanupRequest callback to clean up DBI state
    
    Add a CleanupRequest method similar to RTs to clean up the
    DBIx::SearchBuilder state at the end of a request. REST2
    provides its own Plack app, so RT's Cleanup isn't called for
    REST2 requests.
    
    Previously, calling the REST2 API with a user (like root) and then
    loading a page in the RT web UI logged in with the same user would result in
    various types of DB related errors like:
    
    DBD::Pg::st execute failed: ERROR: prepared statement “dbdpg_p93558_1” already exists

diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index fdecd03..43e39ad 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -561,7 +561,15 @@ handle them appropriately.
 
 # XXX TODO: API doc
 
-sub to_psgi_app { shift->to_app(@_) }
+sub to_psgi_app {
+    my $self = shift;
+    my $res = $self->to_app(@_);
+
+    return Plack::Util::response_cb($res, sub {
+        my $res = shift;
+        $self->CleanupRequest;
+    });
+}
 
 sub to_app {
     my $class = shift;
@@ -593,6 +601,24 @@ sub PSGIWrap {
     };
 }
 
+sub CleanupRequest {
+
+    if ( $RT::Handle && $RT::Handle->TransactionDepth ) {
+        $RT::Handle->ForceRollback;
+        $RT::Logger->crit(
+            "Transaction not committed. Usually indicates a software fault."
+            . "Data loss may have occurred" );
+    }
+
+    # Clean out the ACL cache. the performance impact should be marginal.
+    # Consistency is imprived, too.
+    RT::Principal->InvalidateACLCache();
+    DBIx::SearchBuilder::Record::Cachable->FlushCache
+      if ( RT->Config->Get('WebFlushDbCacheEveryRequest')
+        and UNIVERSAL::can(
+            'DBIx::SearchBuilder::Record::Cachable' => 'FlushCache' ) );
+}
+
 =head1 AUTHOR
 
 Best Practical Solutions, LLC <modules at bestpractical.com>

-----------------------------------------------------------------------


More information about the Bps-public-commit mailing list