FIX: Don't update User#last_seen_at when PG is in readonly.

This commit is contained in:
Guo Xiang Tan 2019-01-21 13:29:29 +08:00
parent cc83bc8fcf
commit c732ae9ca9
4 changed files with 43 additions and 12 deletions

View File

@ -241,6 +241,8 @@ class Auth::DefaultCurrentUserProvider
end
def should_update_last_seen?
return false if Discourse.pg_readonly_mode?
if @request.xhr?
@env["HTTP_DISCOURSE_VISIBLE".freeze] == "true".freeze
elsif !!(@env[API_KEY_ENV]) || !!(@env[USER_API_KEY_ENV])

View File

@ -355,6 +355,10 @@ module Discourse
recently_readonly? || $redis.mget(*keys).compact.present?
end
def self.pg_readonly_mode?
$redis.get(PG_READONLY_MODE_KEY).present?
end
def self.last_read_only
@last_read_only ||= DistributedCache.new('last_read_only', namespace: false)
end

View File

@ -72,7 +72,7 @@ class Middleware::RequestTracker
def self.log_request_on_site(data, host)
RailsMultisite::ConnectionManagement.with_hostname(host) do
unless Discourse.readonly_mode?([Discourse::PG_READONLY_MODE_KEY])
unless Discourse.pg_readonly_mode?
log_request(data)
end
end

View File

@ -160,18 +160,27 @@ describe Auth::DefaultCurrentUserProvider do
).should_update_last_seen?).to eq(false)
end
it "should not update last seen for suspended users" do
user = Fabricate(:user)
provider = provider('/')
cookies = {}
provider.log_on_user(user, {}, cookies)
unhashed_token = cookies["_t"][:value]
describe "#current_user" do
let(:unhashed_token) do
provider = provider('/')
cookies = {}
provider.log_on_user(Fabricate(:user), {}, cookies)
cookies["_t"][:value]
end
after do
$redis.flushall
end
it "should not update last seen for suspended users" do
user = Fabricate(:user)
provider = provider('/')
cookies = {}
provider.log_on_user(user, {}, cookies)
unhashed_token = cookies["_t"][:value]
freeze_time
freeze_time
Sidekiq::Testing.inline! do
# Need to clear this key from redis, otherwise
# this test could fail if run twice in 1 minute
$redis.del("user:#{user.id}:#{Time.now.to_date}")
provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
u = provider2.current_user
u.reload
@ -191,6 +200,22 @@ describe Auth::DefaultCurrentUserProvider do
expect(u.last_seen_at).to eq(nil)
end
describe "when readonly mode is enabled due to postgres" do
before do
Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
end
after do
Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
end
it "should not update last seen at" do
provider2 = provider("/", "HTTP_COOKIE" => "_t=#{unhashed_token}")
u = provider2.current_user
u.reload
expect(u.last_seen_at).to eq(nil)
end
end
end
it "should update ajax reqs with discourse visible" do