Skip to content

A platform for community discussion. Free, open, simple.

DEV: refresh user directory more often for sites with few users (#39852)

Penar Musaraj1f8af748eb98
diff --git a/app/jobs/scheduled/directory_refresh.rb b/app/jobs/scheduled/directory_refresh.rb
new file mode 100644
index 00000000000..ef2696437e8
--- /dev/null
+++ b/app/jobs/scheduled/directory_refresh.rb
@@ -0,0 +1,33 @@
+# frozen_string_literal: true
+
+module Jobs
+ class DirectoryRefresh < ::Jobs::Scheduled
+ every 1.hour
+
+ OLDER_PERIODS_REFRESH_KEY = "directory_older_periods_last_refresh"
+
+ def execute(args)
+ DirectoryItem.refresh_period!(:daily)
+
+ older_periods = DirectoryItem.period_types.keys - [:daily]
+
+ # on smaller site, update hourly, otherwise update daily
+ if small_site? || older_periods_due?
+ older_periods.each { |p| DirectoryItem.refresh_period!(p) }
+ Discourse.redis.set(OLDER_PERIODS_REFRESH_KEY, Time.zone.now.to_i)
+ end
+ end
+
+ private
+
+ def small_site?
+ limit = SiteSetting.directory_hourly_refresh_max_users
+ limit > 0 && User.human_users.count <= limit
+ end
+
+ def older_periods_due?
+ last = Discourse.redis.get(OLDER_PERIODS_REFRESH_KEY)
+ last.nil? || Time.zone.at(last.to_i) < 23.hours.ago
+ end
+ end
+end
diff --git a/app/jobs/scheduled/directory_refresh_daily.rb b/app/jobs/scheduled/directory_refresh_daily.rb
deleted file mode 100644
index 4840f23f224..00000000000
--- a/app/jobs/scheduled/directory_refresh_daily.rb
+++ /dev/null
@@ -1,11 +0,0 @@
-# frozen_string_literal: true
-
-module Jobs
- class DirectoryRefreshDaily < ::Jobs::Scheduled
- every 1.hour
-
- def execute(args)
- DirectoryItem.refresh_period!(:daily)
- end
- end
-end
diff --git a/app/jobs/scheduled/directory_refresh_older.rb b/app/jobs/scheduled/directory_refresh_older.rb
deleted file mode 100644
index a1f792cc44e..00000000000
--- a/app/jobs/scheduled/directory_refresh_older.rb
+++ /dev/null
@@ -1,12 +0,0 @@
-# frozen_string_literal: true
-
-module Jobs
- class DirectoryRefreshOlder < ::Jobs::Scheduled
- every 1.day
-
- def execute(args)
- periods = DirectoryItem.period_types.keys - [:daily]
- periods.each { |p| DirectoryItem.refresh_period!(p) }
- end
- end
-end
diff --git a/config/site_settings.yml b/config/site_settings.yml
index 76df9750a5c..33896145d96 100644
--- a/config/site_settings.yml
+++ b/config/site_settings.yml
@@ -1071,6 +1071,10 @@ users:
client: true
default: true
area: "users"
+ directory_hourly_refresh_max_users:
+ default: 100
+ hidden: true
+ area: "users"
allow_anonymous_mode:
default: false
client: true
diff --git a/spec/jobs/scheduled/directory_refresh_spec.rb b/spec/jobs/scheduled/directory_refresh_spec.rb
new file mode 100644
index 00000000000..dfc90c7fa08
--- /dev/null
+++ b/spec/jobs/scheduled/directory_refresh_spec.rb
@@ -0,0 +1,69 @@
+# frozen_string_literal: true
+
+RSpec.describe Jobs::DirectoryRefresh do
+ before { Discourse.redis.del(described_class::OLDER_PERIODS_REFRESH_KEY) }
+
+ it "always refreshes the daily period" do
+ DirectoryItem.stubs(:refresh_period!)
+ DirectoryItem.expects(:refresh_period!).with(:daily).once
+ described_class.new.execute({})
+ end
+
+ context "when older periods are due for refresh" do
+ it "refreshes all non-daily periods" do
+ older_periods = DirectoryItem.period_types.keys - [:daily]
+ older_periods.each { |p| DirectoryItem.expects(:refresh_period!).with(p).once }
+ DirectoryItem.stubs(:refresh_period!).with(:daily)
+ described_class.new.execute({})
+ end
+
+ it "records the refresh time in Redis" do
+ DirectoryItem.stubs(:refresh_period!)
+ freeze_time do
+ described_class.new.execute({})
+ expect(Discourse.redis.get(described_class::OLDER_PERIODS_REFRESH_KEY).to_i).to eq(
+ Time.zone.now.to_i,
+ )
+ end
+ end
+ end
+
+ context "when older periods were recently refreshed" do
+ before { Discourse.redis.set(described_class::OLDER_PERIODS_REFRESH_KEY, 1.hour.ago.to_i) }
+
+ it "does not refresh older periods for a large site" do
+ SiteSetting.directory_hourly_refresh_max_users = 0
+ DirectoryItem.stubs(:refresh_period!).with(:daily)
+ older_periods = DirectoryItem.period_types.keys - [:daily]
+ older_periods.each { |p| DirectoryItem.expects(:refresh_period!).with(p).never }
+ described_class.new.execute({})
+ end
+
+ context "when user count is within the limit" do
+ fab!(:user)
+
+ before { SiteSetting.directory_hourly_refresh_max_users = 5 }
+
+ it "refreshes older periods for a small site" do
+ older_periods = DirectoryItem.period_types.keys - [:daily]
+ older_periods.each { |p| DirectoryItem.expects(:refresh_period!).with(p).once }
+ DirectoryItem.stubs(:refresh_period!).with(:daily)
+ described_class.new.execute({})
+ end
+ end
+
+ context "when user count exceeds the limit" do
+ fab!(:user_1, :user)
+ fab!(:user_2, :user)
+
+ before { SiteSetting.directory_hourly_refresh_max_users = 1 }
+
+ it "does not refresh older periods" do
+ DirectoryItem.stubs(:refresh_period!).with(:daily)
+ older_periods = DirectoryItem.period_types.keys - [:daily]
+ older_periods.each { |p| DirectoryItem.expects(:refresh_period!).with(p).never }
+ described_class.new.execute({})
+ end
+ end
+ end
+end