[arvados] created: 2.6.0-8-gdb2a95206

git repository hosting git at public.arvados.org
Fri Apr 14 14:49:42 UTC 2023


        at  db2a9520689cdf5a83929db2a6f4249333460e01 (commit)


commit db2a9520689cdf5a83929db2a6f4249333460e01
Author: Tom Clegg <tom at curii.com>
Date:   Fri Apr 14 10:49:24 2023 -0400

    20203: Don't rely on rails cache for resetting globals.
    
    Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom at curii.com>

diff --git a/services/api/app/controllers/database_controller.rb b/services/api/app/controllers/database_controller.rb
index 69453959d..1be1f0cd6 100644
--- a/services/api/app/controllers/database_controller.rb
+++ b/services/api/app/controllers/database_controller.rb
@@ -61,8 +61,9 @@ class DatabaseController < ApplicationController
         ActiveRecord::FixtureSet.
           create_fixtures(Rails.root.join('test', 'fixtures'), fixturesets)
 
-        # Dump cache of permissions etc.
+        # Reset cache and global state
         Rails.cache.clear
+        CurrentApiClient.reset_system_globals
         ActiveRecord::Base.connection.clear_query_cache
 
         # Reload database seeds
diff --git a/services/api/lib/current_api_client.rb b/services/api/lib/current_api_client.rb
index ee666b77a..eb604f0e1 100644
--- a/services/api/lib/current_api_client.rb
+++ b/services/api/lib/current_api_client.rb
@@ -2,15 +2,6 @@
 #
 # SPDX-License-Identifier: AGPL-3.0
 
-$system_user = nil
-$system_group = nil
-$all_users_group = nil
-$anonymous_user = nil
-$anonymous_group = nil
-$anonymous_group_read_permission = nil
-$empty_collection = nil
-$public_project_group = nil
-$public_project_group_read_permission = nil
 
 module CurrentApiClient
   def current_user
@@ -74,26 +65,25 @@ module CurrentApiClient
   end
 
   def system_user
-    $system_user = check_cache $system_user do
-      real_current_user = Thread.current[:user]
-      begin
-        Thread.current[:user] = User.new(is_admin: true,
-                                         is_active: true,
-                                         uuid: system_user_uuid)
+    real_current_user = Thread.current[:user]
+    begin
+      Thread.current[:user] = User.new(is_admin: true,
+                                       is_active: true,
+                                       uuid: system_user_uuid)
+      $system_user ||=
         User.where(uuid: system_user_uuid).
           first_or_create!(is_active: true,
                            is_admin: true,
                            email: 'root',
                            first_name: 'root',
                            last_name: '')
-      ensure
-        Thread.current[:user] = real_current_user
-      end
+    ensure
+      Thread.current[:user] = real_current_user
     end
   end
 
   def system_group
-    $system_group = check_cache $system_group do
+    $system_group ||=
       act_as_system_user do
         ActiveRecord::Base.transaction do
           Group.where(uuid: system_group_uuid).
@@ -110,7 +100,6 @@ module CurrentApiClient
           end
         end
       end
-    end
   end
 
   def all_users_group_uuid
@@ -120,7 +109,7 @@ module CurrentApiClient
   end
 
   def all_users_group
-    $all_users_group = check_cache $all_users_group do
+    $all_users_group ||=
       act_as_system_user do
         ActiveRecord::Base.transaction do
           Group.where(uuid: all_users_group_uuid).
@@ -129,7 +118,6 @@ module CurrentApiClient
                              group_class: "role")
         end
       end
-    end
   end
 
   def act_as_system_user
@@ -156,7 +144,7 @@ module CurrentApiClient
   end
 
   def anonymous_group
-    $anonymous_group = check_cache $anonymous_group do
+    $anonymous_group ||=
       act_as_system_user do
         ActiveRecord::Base.transaction do
           Group.where(uuid: anonymous_group_uuid).
@@ -165,23 +153,20 @@ module CurrentApiClient
                              description: "Anonymous users")
         end
       end
-    end
   end
 
   def anonymous_group_read_permission
-    $anonymous_group_read_permission =
-        check_cache $anonymous_group_read_permission do
+    $anonymous_group_read_permission ||=
       act_as_system_user do
         Link.where(tail_uuid: all_users_group.uuid,
                    head_uuid: anonymous_group.uuid,
                    link_class: "permission",
                    name: "can_read").first_or_create!
       end
-    end
   end
 
   def anonymous_user
-    $anonymous_user = check_cache $anonymous_user do
+    $anonymous_user ||=
       act_as_system_user do
         User.where(uuid: anonymous_user_uuid).
           first_or_create!(is_active: false,
@@ -197,11 +182,10 @@ module CurrentApiClient
             first_or_create!
         end
       end
-    end
   end
 
   def public_project_group
-    $public_project_group = check_cache $public_project_group do
+    $public_project_group ||=
       act_as_system_user do
         ActiveRecord::Base.transaction do
           Group.where(uuid: public_project_uuid).
@@ -210,39 +194,34 @@ module CurrentApiClient
                              description: "Public favorites")
         end
       end
-    end
   end
 
   def public_project_read_permission
-    $public_project_group_read_permission =
-        check_cache $public_project_group_read_permission do
+    $public_project_group_read_permission ||=
       act_as_system_user do
         Link.where(tail_uuid: anonymous_group.uuid,
                    head_uuid: public_project_group.uuid,
                    link_class: "permission",
                    name: "can_read").first_or_create!
       end
-    end
   end
 
   def anonymous_user_token_api_client
-    $anonymous_user_token_api_client = check_cache $anonymous_user_token_api_client do
+    $anonymous_user_token_api_client ||=
       act_as_system_user do
         ActiveRecord::Base.transaction do
           ApiClient.find_or_create_by!(is_trusted: false, url_prefix: "", name: "AnonymousUserToken")
         end
       end
-    end
   end
 
   def system_root_token_api_client
-    $system_root_token_api_client = check_cache $system_root_token_api_client do
+    $system_root_token_api_client ||=
       act_as_system_user do
         ActiveRecord::Base.transaction do
           ApiClient.find_or_create_by!(is_trusted: true, url_prefix: "", name: "SystemRootToken")
         end
       end
-    end
   end
 
   def empty_collection_pdh
@@ -250,7 +229,7 @@ module CurrentApiClient
   end
 
   def empty_collection
-    $empty_collection = check_cache $empty_collection do
+    $empty_collection ||=
       act_as_system_user do
         ActiveRecord::Base.transaction do
           Collection.
@@ -266,34 +245,22 @@ module CurrentApiClient
           end
         end
       end
-    end
   end
 
-  private
-
-  # If the given value is nil, or the cache has been cleared since it
-  # was set, yield. Otherwise, return the given value.
-  def check_cache value
-    if not Rails.env.test? and
-        ActionController::Base.cache_store.is_a? ActiveSupport::Cache::FileStore and
-        not File.owned? ActionController::Base.cache_store.cache_path
-      # If we don't own the cache dir, we're probably
-      # crunch-dispatch. Whoever we are, using this cache is likely to
-      # either fail or screw up the cache for someone else. So we'll
-      # just assume the $globals are OK to live forever.
-      #
-      # The reason for making the globals expire with the cache in the
-      # first place is to avoid leaking state between test cases: in
-      # production, we don't expect the database seeds to ever go away
-      # even when the cache is cleared, so there's no particular
-      # reason to expire our global variables.
-    else
-      Rails.cache.fetch "CurrentApiClient.$globals" do
-        value = nil
-        true
-      end
-    end
-    return value unless value.nil?
-    yield
+  def reset_system_globals
+    $system_user = nil
+    $system_group = nil
+    $all_users_group = nil
+    $anonymous_group = nil
+    $anonymous_group_read_permission = nil
+    $anonymous_user = nil
+    $public_project_group = nil
+    $public_project_group_read_permission = nil
+    $anonymous_user_token_api_client = nil
+    $system_root_token_api_client = nil
+    $empty_collection = nil
   end
+  module_function :reset_system_globals
 end
+
+CurrentApiClient.reset_system_globals

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


hooks/post-receive
-- 




More information about the arvados-commits mailing list