[Rt-commit] rt branch, 4.4/clear-mason-cache-button, created. rt-4.4.4-225-g7d895ac15f

? sunnavy sunnavy at bestpractical.com
Fri Jan 22 16:53:18 EST 2021


The branch, 4.4/clear-mason-cache-button has been created
        at  7d895ac15f2577c63034f4d51f77244f8c362929 (commit)

- Log -----------------------------------------------------------------
commit 7d895ac15f2577c63034f4d51f77244f8c362929
Author: sunnavy <sunnavy at bestpractical.com>
Date:   Thu Apr 5 02:18:07 2018 +0800

    Add support to clear mason cache via web interface

diff --git a/lib/RT/Interface/Web.pm b/lib/RT/Interface/Web.pm
index fa81b0fe3c..c2aa48398b 100644
--- a/lib/RT/Interface/Web.pm
+++ b/lib/RT/Interface/Web.pm
@@ -266,6 +266,14 @@ sub HandleRequest {
         require Module::Refresh;
         Module::Refresh->refresh;
     }
+    else {
+        require File::Spec;
+        my $mason_cache_created = ( stat File::Spec->catdir( $RT::MasonDataDir, 'obj' ) )[ 9 ] // '';
+        if ( ( $HTML::Mason::Commands::m->{rt_mason_cache_created} // '' ) ne $mason_cache_created ) {
+            $HTML::Mason::Commands::m->interp->flush_code_cache;
+            $HTML::Mason::Commands::m->{rt_mason_cache_created} = $mason_cache_created;
+        }
+    }
 
     $HTML::Mason::Commands::r->content_type("text/html; charset=utf-8");
 
@@ -1888,6 +1896,41 @@ sub RequestENV {
     return $name ? $env->{$name} : $env;
 }
 
+=head2 ClearMasonCache
+
+Delete current mason cache.
+
+=cut
+
+sub ClearMasonCache {
+    require File::Path;
+    require File::Spec;
+    my $mason_obj_dir = File::Spec->catdir( $RT::MasonDataDir, 'obj' );
+
+    my $error;
+
+    # There is a race condition that other processes add new cache items while
+    # remove_tree is running, which could prevent it from deleting the whole "obj"
+    # directory with errors like "Directory not empty". Let's try for a few times
+    # here to get around it.
+
+    for ( 1 .. 10 ) {
+        last unless -e $mason_obj_dir;
+        File::Path::remove_tree( $mason_obj_dir, { safe => 1, error => \$error } );
+    }
+
+    if ( $error && @$error ) {
+
+        # Only one dir is specified, so there will be only one error if any
+        my ( $file, $message ) = %{ $error->[0] };
+        RT->Logger->error("Failed to clear mason cache: $file => $message");
+        return ( 0, HTML::Mason::Commands::loc( "Failed to clear mason cache: [_1] => [_2]", $file, $message ) );
+    }
+    else {
+        return ( 1, HTML::Mason::Commands::loc('Cache cleared') );
+    }
+}
+
 package HTML::Mason::Commands;
 
 use vars qw/$r $m %session/;
diff --git a/share/html/Admin/Helpers/ClearMasonCache b/share/html/Admin/Helpers/ClearMasonCache
new file mode 100644
index 0000000000..18b10cc580
--- /dev/null
+++ b/share/html/Admin/Helpers/ClearMasonCache
@@ -0,0 +1,54 @@
+%# BEGIN BPS TAGGED BLOCK {{{
+%#
+%# COPYRIGHT:
+%#
+%# This software is Copyright (c) 1996-2018 Best Practical Solutions, LLC
+%#                                          <sales at bestpractical.com>
+%#
+%# (Except where explicitly superseded by other copyright notices)
+%#
+%#
+%# LICENSE:
+%#
+%# This work is made available to you under the terms of Version 2 of
+%# the GNU General Public License. A copy of that license should have
+%# been provided with this software, but in any event can be snarfed
+%# from www.gnu.org.
+%#
+%# This work is distributed in the hope that it will be useful, but
+%# WITHOUT ANY WARRANTY; without even the implied warranty of
+%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+%# General Public License for more details.
+%#
+%# You should have received a copy of the GNU General Public License
+%# along with this program; if not, write to the Free Software
+%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+%# 02110-1301 or visit their web page on the internet at
+%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
+%#
+%#
+%# CONTRIBUTION SUBMISSION POLICY:
+%#
+%# (The following paragraph is not intended to limit the rights granted
+%# to you to modify and distribute this software under the terms of
+%# the GNU General Public License and is only of importance to you if
+%# you choose to contribute your changes and enhancements to the
+%# community by submitting them to Best Practical Solutions, LLC.)
+%#
+%# By intentionally submitting any modifications, corrections or
+%# derivatives to this work, or any other work intended for use with
+%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
+%# you are the copyright holder for those contributions and you grant
+%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
+%# royalty-free, perpetual, license to use, copy, create derivative
+%# works based on those contributions, and sublicense and distribute
+%# those contributions and any derivatives thereof.
+%#
+%# END BPS TAGGED BLOCK }}}
+<%init>
+
+my ( $ret, $msg ) = RT::Interface::Web::ClearMasonCache();
+$r->content_type( 'application/json; charset=utf-8' );
+$m->out( JSON( { status => $ret, message => $msg } ) );
+$m->abort;
+</%init>
diff --git a/share/html/Admin/Tools/Configuration.html b/share/html/Admin/Tools/Configuration.html
index 1c63b50068..4a65ddd298 100644
--- a/share/html/Admin/Tools/Configuration.html
+++ b/share/html/Admin/Tools/Configuration.html
@@ -173,6 +173,26 @@ for my $type (qw/Tickets Queues Transactions Groups PrivilegedUsers Unprivileged
 <td valign="top" class="boxcontainer">
 
 <&|/Widgets/TitleBox, title => loc("Mason template search order") &>
+
+% if ( $m->{rt_mason_cache_created} ) {
+% my $mason_obj_date = RT::Date->new( $session{CurrentUser} );
+% $mason_obj_date->Set( Format => 'Unix', Value => $m->{rt_mason_cache_created} );
+
+<div class="mason-cache">
+    <div class="mason-cache-info"><&|/l&>Cache created</&>: <% $mason_obj_date->AsString %></div>
+    <a class="button clear-mason-cache" href="javascript:;"><&|/l&>Clear Mason Cache</&></a>
+</div>
+<script type="text/javascript">
+jQuery('a.clear-mason-cache').click( function() {
+    jQuery.post(RT.Config.WebHomePath + '/Admin/Helpers/ClearMasonCache', function(data) {
+        jQuery('div.mason-cache div.mason-cache-info').text(data.message);
+    }, 'json');
+    return false;
+});
+</script>
+
+% }
+
 <ol>
 % foreach my $path ( RT::Interface::Web->ComponentRoots ) {
 <li><% $path %></li>
diff --git a/share/static/css/base/admin.css b/share/static/css/base/admin.css
index fb8a17862d..7c0713bc03 100644
--- a/share/static/css/base/admin.css
+++ b/share/static/css/base/admin.css
@@ -108,3 +108,16 @@ div.inline-row i {
     text-align: left;
     width: 85%;
 }
+
+div.mason-cache {
+    min-height: 2em;
+}
+
+div.mason-cache div.mason-cache-info {
+    display: inline-block;
+    padding: 0.5em;
+}
+
+div.mason-cache a.clear-mason-cache {
+    float: right;
+}

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


More information about the rt-commit mailing list