Teach Storage to use $ENV{TZ} for the db timezone inside transactions, and by default...
authorMike Rylander <mrylander@gmail.com>
Mon, 3 Aug 2015 17:27:56 +0000 (13:27 -0400)
committerMike Rylander <mrylander@gmail.com>
Tue, 4 Aug 2015 16:07:02 +0000 (12:07 -0400)
Signed-off-by: Mike Rylander <mrylander@gmail.com>
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Driver/Pg/storage.pm
Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/Publisher/metabib.pm

index 7401846..ada5da6 100644 (file)
@@ -51,6 +51,64 @@ sub initialize {
     $log->debug("We seem to be OK...",DEBUG);
 }
 
+sub register_method {
+    my $class = shift;
+    my %args = @_;
+
+    $args{package} ||= ref($class) || $class;
+
+    unless ($args{no_tz_force}) {
+        my %dup_args = %args;
+        $dup_args{api_name} = 'no_tz.' . $args{api_name};
+
+        $args{method} = 'force_db_tz';
+        delete $args{package};
+
+        __PACKAGE__->SUPER::register_method( %dup_args );
+
+    }
+
+    __PACKAGE__->SUPER::register_method( %args );
+
+}
+
+sub force_db_tz {
+    my $self = shift;
+    my $client = shift;
+    my @args = @_;
+
+    my ($current_xact) = $self->method_lookup('open-ils.storage.transaction.current')->run;
+
+    if (!$current_xact && $ENV{TZ}) {
+        try {
+            OpenILS::Application::Storage::CDBI->db_Main->do(
+                'SET timezone TO ?;',
+                {},
+                $ENV{TZ}
+            );
+        } catch Error with {
+            $log->error( "Could not set timezone: $ENV{TZ}");
+        };
+    }
+
+    my $method = $self->method_lookup('no_tz.' . $self->{api_name});
+    die unless $method;
+
+    $client->respond( $_ ) for ( $method->run(@args) );
+
+    if (!$current_xact && $ENV{TZ}) {
+        try {
+            OpenILS::Application::Storage::CDBI->db_Main->do(
+                'SET timezone TO DEFAULT;',
+            );
+        } catch Error with {
+            $log->error( "Could not reset default timezone");
+        };
+    }
+
+    return undef;
+}
+
 sub child_init {
 
     $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG);
index c9fd303..157aa7c 100644 (file)
             throw $e;
         };
 
+        if ($ENV{TZ}) {
+            try {
+                $dbh->do('SET LOCAL timezone TO ?;',{},$ENV{TZ});
+
+            } catch Error with {
+                my $e = shift;
+                $log->debug("Failed to set timezone: $ENV{TZ}", WARN);
+            };
+        }
 
-        my $death_cb = $client->session->register_callback(
-            death => sub {
-                __PACKAGE__->pg_rollback_xaction;
-            }
-        );
-
-        $log->debug("Registered 'death' callback [$death_cb] for new transaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
-
-        $client->session->session_data( death_cb => $death_cb );
 
-        if ($self->api_name =~ /autocommit$/o) {
-            $pg->current_xact_is_auto(1);
-            my $dc_cb = $client->session->register_callback(
-                disconnect => sub {
-                    my $ses = shift;
-                    $ses->unregister_callback(death => $death_cb);
-                    __PACKAGE__->pg_commit_xaction;
+        if ($client->session) { # not a subrequest
+            my $death_cb = $client->session->register_callback(
+                death => sub {
+                    __PACKAGE__->pg_rollback_xaction;
                 }
             );
-            $log->debug("Registered 'disconnect' callback [$dc_cb] for new transaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
-            if ($client and $client->session) {
-                $client->session->session_data( disconnect_cb => $dc_cb );
+    
+            $log->debug("Registered 'death' callback [$death_cb] for new transaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
+    
+            $client->session->session_data( death_cb => $death_cb );
+    
+            if ($self->api_name =~ /autocommit$/o) {
+                $pg->current_xact_is_auto(1);
+                my $dc_cb = $client->session->register_callback(
+                    disconnect => sub {
+                        my $ses = shift;
+                        $ses->unregister_callback(death => $death_cb);
+                        __PACKAGE__->pg_commit_xaction;
+                    }
+                );
+                $log->debug("Registered 'disconnect' callback [$dc_cb] for new transaction with Open-ILS XACT-ID [$xact_id]", DEBUG);
+                if ($client and $client->session) {
+                    $client->session->session_data( disconnect_cb => $dc_cb );
+                }
             }
         }
 
             $success = 0;
         };
         
-        $pg->current_xact_session->unregister_callback( death => 
-            $pg->current_xact_session->session_data( 'death_cb' )
-        ) if ($pg->current_xact_session);
-
-        if ($pg->current_xact_is_auto) {
-            $pg->current_xact_session->unregister_callback( disconnect => 
-                $pg->current_xact_session->session_data( 'disconnect_cb' )
-            );
+        if ($pg->current_xact_session) { # not a subrequest
+            $pg->current_xact_session->unregister_callback( death => 
+                $pg->current_xact_session->session_data( 'death_cb' )
+            ) if ($pg->current_xact_session);
+    
+            if ($pg->current_xact_is_auto) {
+                $pg->current_xact_session->unregister_callback( disconnect => 
+                    $pg->current_xact_session->session_data( 'disconnect_cb' )
+                );
+            }
         }
 
         $pg->unset_xact_session;
             $success = 0;
         };
     
-        $pg->current_xact_session->unregister_callback( death =>
-            $pg->current_xact_session->session_data( 'death_cb' )
-        ) if ($pg->current_xact_session);
-
-        if ($pg->current_xact_is_auto) {
-            $pg->current_xact_session->unregister_callback( disconnect =>
-                $pg->current_xact_session->session_data( 'disconnect_cb' )
-            );
+        if ($pg->current_xact_session) { # not a subrequest
+            $pg->current_xact_session->unregister_callback( death =>
+                $pg->current_xact_session->session_data( 'death_cb' )
+            ) if ($pg->current_xact_session);
+    
+            if ($pg->current_xact_is_auto) {
+                $pg->current_xact_session->unregister_callback( disconnect =>
+                    $pg->current_xact_session->session_data( 'disconnect_cb' )
+                );
+            }
         }
 
         $pg->unset_xact_session;
 
         $pg->set_audit_session( $client->session );
 
-        my $death_cb = $client->session->register_callback(
-            death => sub {
-                __PACKAGE__->pg_clear_audit_info;
-            }
-        );
-
-        $log->debug("Registered 'death' callback [$death_cb] for clearing audit information", DEBUG);
-
-        $client->session->session_data( death_cb_ai => $death_cb );
+        if ($client->session) { # not a subrequest
+            my $death_cb = $client->session->register_callback(
+                death => sub {
+                    __PACKAGE__->pg_clear_audit_info;
+                }
+            );
+    
+            $log->debug("Registered 'death' callback [$death_cb] for clearing audit information", DEBUG);
+    
+            $client->session->session_data( death_cb_ai => $death_cb );
+        }
 
         return 1;
 
             $log->debug("Failed to clear audit information: ".$e, INFO);
         };
 
-        $pg->current_audit_session->unregister_callback( death => 
-            $pg->current_audit_session->session_data( 'death_cb_ai' )
-        ) if ($pg->current_audit_session);
+        if ($pg->current_audit_session) { # not a subrequest
+            $pg->current_audit_session->unregister_callback( death => 
+                $pg->current_audit_session->session_data( 'death_cb_ai' )
+            ) if ($pg->current_audit_session);
+        }
 
         $pg->unset_audit_session;
     }
index fad6ef3..d26999e 100644 (file)
@@ -137,12 +137,14 @@ sub ordered_records_from_metarecord { # XXX Replace with QP-based search-within-
 }
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.ordered.metabib.metarecord.records',
+    no_tz_force => 1,
     method      => 'ordered_records_from_metarecord',
     api_level   => 1,
     cachable    => 1,
 );
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.ordered.metabib.metarecord.records.staff',
+    no_tz_force => 1,
     method      => 'ordered_records_from_metarecord',
     api_level   => 1,
     cachable    => 1,
@@ -150,12 +152,14 @@ __PACKAGE__->register_method(
 
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.ordered.metabib.metarecord.records.atomic',
+    no_tz_force => 1,
     method      => 'ordered_records_from_metarecord',
     api_level   => 1,
     cachable    => 1,
 );
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.ordered.metabib.metarecord.records.staff.atomic',
+    no_tz_force => 1,
     method      => 'ordered_records_from_metarecord',
     api_level   => 1,
     cachable    => 1,
@@ -193,12 +197,14 @@ sub isxn_search {
 }
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.id_list.biblio.record_entry.search.isbn',
+    no_tz_force => 1,
     method      => 'isxn_search',
     api_level   => 1,
     stream      => 1,
 );
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.id_list.biblio.record_entry.search.issn',
+    no_tz_force => 1,
     method      => 'isxn_search',
     api_level   => 1,
     stream      => 1,
@@ -350,6 +356,7 @@ sub metarecord_copy_count {
 }
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.metabib.metarecord.copy_count',
+    no_tz_force => 1,
     method      => 'metarecord_copy_count',
     api_level   => 1,
     stream      => 1,
@@ -357,6 +364,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.metabib.metarecord.copy_count.staff',
+    no_tz_force => 1,
     method      => 'metarecord_copy_count',
     api_level   => 1,
     stream      => 1,
@@ -639,6 +647,7 @@ sub biblio_multi_search_full_rec {
 }
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.biblio.full_rec.multi_search',
+    no_tz_force => 1,
     method      => 'biblio_multi_search_full_rec',
     api_level   => 1,
     stream      => 1,
@@ -646,6 +655,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.biblio.full_rec.multi_search.staff',
+    no_tz_force => 1,
     method      => 'biblio_multi_search_full_rec',
     api_level   => 1,
     stream      => 1,
@@ -700,6 +710,7 @@ sub search_full_rec {
 }
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.direct.metabib.full_rec.search_fts.value',
+    no_tz_force => 1,
     method      => 'search_full_rec',
     api_level   => 1,
     stream      => 1,
@@ -707,6 +718,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => 'open-ils.storage.direct.metabib.full_rec.search_fts.index_vector',
+    no_tz_force => 1,
     method      => 'search_full_rec',
     api_level   => 1,
     stream      => 1,
@@ -866,6 +878,7 @@ sub search_class_fts {
 for my $class ( qw/title author subject keyword series identifier/ ) {
     __PACKAGE__->register_method(
         api_name    => "open-ils.storage.metabib.$class.search_fts.metarecord",
+        no_tz_force => 1,
         method      => 'search_class_fts',
         api_level   => 1,
         stream      => 1,
@@ -874,6 +887,7 @@ for my $class ( qw/title author subject keyword series identifier/ ) {
     );
     __PACKAGE__->register_method(
         api_name    => "open-ils.storage.metabib.$class.search_fts.metarecord.unordered",
+        no_tz_force => 1,
         method      => 'search_class_fts',
         api_level   => 1,
         stream      => 1,
@@ -882,6 +896,7 @@ for my $class ( qw/title author subject keyword series identifier/ ) {
     );
     __PACKAGE__->register_method(
         api_name    => "open-ils.storage.metabib.$class.search_fts.metarecord.staff",
+        no_tz_force => 1,
         method      => 'search_class_fts',
         api_level   => 1,
         stream      => 1,
@@ -890,6 +905,7 @@ for my $class ( qw/title author subject keyword series identifier/ ) {
     );
     __PACKAGE__->register_method(
         api_name    => "open-ils.storage.metabib.$class.search_fts.metarecord.staff.unordered",
+        no_tz_force => 1,
         method      => 'search_class_fts',
         api_level   => 1,
         stream      => 1,
@@ -1014,6 +1030,7 @@ sub search_class_fts_count {
 for my $class ( qw/title author subject keyword series identifier/ ) {
     __PACKAGE__->register_method(
         api_name    => "open-ils.storage.metabib.$class.search_fts.metarecord_count",
+        no_tz_force => 1,
         method      => 'search_class_fts_count',
         api_level   => 1,
         stream      => 1,
@@ -1022,6 +1039,7 @@ for my $class ( qw/title author subject keyword series identifier/ ) {
     );
     __PACKAGE__->register_method(
         api_name    => "open-ils.storage.metabib.$class.search_fts.metarecord_count.staff",
+        no_tz_force => 1,
         method      => 'search_class_fts_count',
         api_level   => 1,
         stream      => 1,
@@ -1387,6 +1405,7 @@ sub postfilter_search_class_fts {
 for my $class ( qw/title author subject keyword series identifier/ ) {
     __PACKAGE__->register_method(
         api_name    => "open-ils.storage.metabib.$class.post_filter.search_fts.metarecord",
+        no_tz_force => 1,
         method      => 'postfilter_search_class_fts',
         api_level   => 1,
         stream      => 1,
@@ -1395,6 +1414,7 @@ for my $class ( qw/title author subject keyword series identifier/ ) {
     );
     __PACKAGE__->register_method(
         api_name    => "open-ils.storage.metabib.$class.post_filter.search_fts.metarecord.staff",
+        no_tz_force => 1,
         method      => 'postfilter_search_class_fts',
         api_level   => 1,
         stream      => 1,
@@ -1878,6 +1898,7 @@ sub postfilter_search_multi_class_fts {
 
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord",
+    no_tz_force => 1,
     method      => 'postfilter_search_multi_class_fts',
     api_level   => 1,
     stream      => 1,
@@ -1885,6 +1906,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.metabib.post_filter.multiclass.search_fts.metarecord.staff",
+    no_tz_force => 1,
     method      => 'postfilter_search_multi_class_fts',
     api_level   => 1,
     stream      => 1,
@@ -1893,6 +1915,7 @@ __PACKAGE__->register_method(
 
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.metabib.multiclass.search_fts",
+    no_tz_force => 1,
     method      => 'postfilter_search_multi_class_fts',
     api_level   => 1,
     stream      => 1,
@@ -1900,6 +1923,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.metabib.multiclass.search_fts.staff",
+    no_tz_force => 1,
     method      => 'postfilter_search_multi_class_fts',
     api_level   => 1,
     stream      => 1,
@@ -2285,6 +2309,7 @@ sub biblio_search_multi_class_fts {
 
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.biblio.multiclass.search_fts.record",
+    no_tz_force => 1,
     method      => 'biblio_search_multi_class_fts',
     api_level   => 1,
     stream      => 1,
@@ -2292,6 +2317,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.biblio.multiclass.search_fts.record.staff",
+    no_tz_force => 1,
     method      => 'biblio_search_multi_class_fts',
     api_level   => 1,
     stream      => 1,
@@ -2299,6 +2325,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.biblio.multiclass.search_fts",
+    no_tz_force => 1,
     method      => 'biblio_search_multi_class_fts',
     api_level   => 1,
     stream      => 1,
@@ -2306,6 +2333,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.biblio.multiclass.search_fts.staff",
+    no_tz_force => 1,
     method      => 'biblio_search_multi_class_fts',
     api_level   => 1,
     stream      => 1,
@@ -2582,6 +2610,7 @@ sub staged_fts {
 }
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.biblio.multiclass.staged.search_fts",
+    no_tz_force => 1,
     method      => 'staged_fts',
     api_level   => 0,
     stream      => 1,
@@ -2589,6 +2618,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.biblio.multiclass.staged.search_fts.staff",
+    no_tz_force => 1,
     method      => 'staged_fts',
     api_level   => 0,
     stream      => 1,
@@ -2596,6 +2626,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.metabib.multiclass.staged.search_fts",
+    no_tz_force => 1,
     method      => 'staged_fts',
     api_level   => 0,
     stream      => 1,
@@ -2603,6 +2634,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.metabib.multiclass.staged.search_fts.staff",
+    no_tz_force => 1,
     method      => 'staged_fts',
     api_level   => 0,
     stream      => 1,
@@ -2637,6 +2669,7 @@ sub FTS_paging_estimate {
 }
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.fts_paging_estimate",
+    no_tz_force => 1,
     method      => 'FTS_paging_estimate',
     argc        => 5,
     strict      => 1,
@@ -2736,6 +2769,7 @@ sub xref_count {
 }
 __PACKAGE__->register_method(
     api_name  => "open-ils.storage.search.xref",
+    no_tz_force => 1,
     method    => 'xref_count',
     api_level => 1,
 );
@@ -2750,6 +2784,7 @@ sub abstract_query2str {
 
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.query_parser.abstract_query.canonicalize",
+    no_tz_force => 1,
     method      => "abstract_query2str",
     api_level   => 1,
     signature   => {
@@ -2791,6 +2826,7 @@ sub str2abstract_query {
 
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.query_parser.abstract_query.from_string",
+    no_tz_force => 1,
     method      => "str2abstract_query",
     api_level   => 1,
     signature   => {
@@ -3092,6 +3128,7 @@ sub query_parser_fts {
 }
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.query_parser_search",
+    no_tz_force => 1,
     method      => 'query_parser_fts',
     api_level   => 1,
     stream      => 1,
@@ -3193,6 +3230,7 @@ sub query_parser_fts_wrapper {
 }
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.biblio.multiclass.staged.search_fts",
+    no_tz_force => 1,
     method      => 'query_parser_fts_wrapper',
     api_level   => 1,
     stream      => 1,
@@ -3200,6 +3238,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.biblio.multiclass.staged.search_fts.staff",
+    no_tz_force => 1,
     method      => 'query_parser_fts_wrapper',
     api_level   => 1,
     stream      => 1,
@@ -3207,6 +3246,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.metabib.multiclass.staged.search_fts",
+    no_tz_force => 1,
     method      => 'query_parser_fts_wrapper',
     api_level   => 1,
     stream      => 1,
@@ -3214,6 +3254,7 @@ __PACKAGE__->register_method(
 );
 __PACKAGE__->register_method(
     api_name    => "open-ils.storage.metabib.multiclass.staged.search_fts.staff",
+    no_tz_force => 1,
     method      => 'query_parser_fts_wrapper',
     api_level   => 1,
     stream      => 1,