0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/spec/tasks/nested_replies_spec.rb
Mark VanLandingham c85593eda8
PERF: Add fast nested reply stats preparation (#41870)
## Problem

Nested reply statistics currently start backfilling only after nested
replies are enabled. At the default 100 topics every five minutes, that
is 28,800 topics per day—about 174 days for a site with five million
topics. Large sites therefore have to expose the feature before its
historical stats are ready.

## Solution

Add a backend-only `nested_replies:prepare_stats` rake task. For each
site, it:

- enables the existing realtime stats callbacks while the nested-replies
UI remains disabled;
- captures the current regular-topic high-water mark;
- enqueues low-priority, keyset-paginated batches which immediately
enqueue the next batch instead of waiting five minutes;
- reuses the existing per-topic backfill SQL; and
- isolates an individual failed topic into its own job, where normal
Sidekiq retries apply, so later topics can continue.

The task leaves the hidden maintenance flag enabled, so replies created,
deleted, or reparented after preparation begins keep the same stats
current before nested replies are enabled. On multisite it prepares
every database by default; `RAILS_DB` scopes it to one database.

The existing scheduled backfill keeps its original feature gate, topic
selection, batch schedule, and calculations. This adds no UI,
migrations, stats version, readiness marker, durable cursor setting,
lock, or watchdog.

Run it with:

```sh
RAILS_ENV=production bin/rake nested_replies:prepare_stats
```

Completion is logged with the captured high-water topic ID. Re-running
the task is safe because it uses the existing idempotent upsert
behavior.

Tests cover disabled-mode preparation, bounded continuation and
high-water behavior, isolated retries, rake-task enqueueing,
preservation of the old scheduler gate, and realtime maintenance while
the UI is disabled.
2026-07-21 10:45:53 -05:00

17 lines
771 B
Ruby
Vendored

# frozen_string_literal: true
RSpec.describe "nested_replies tasks" do
it "enables maintenance and enqueues site-wide stats preparation" do
Fabricate(:topic)
max_topic_id = Topic.where(archetype: Archetype.default, deleted_at: nil).maximum(:id)
SiteSetting.nested_replies_stats_maintenance_enabled = false
output = capture_stdout { invoke_rake_task("nested_replies:prepare_stats") }
expect(SiteSetting.nested_replies_stats_maintenance_enabled).to eq(true)
expect(Jobs::PrepareNestedReplyStats.jobs.size).to eq(1)
args = Jobs::PrepareNestedReplyStats.jobs.first["args"].first.with_indifferent_access
expect(args).to include(max_topic_id: max_topic_id)
expect(output).to include("Enqueued nested reply stats preparation")
end
end