From 2f906ed55ab0b9887a3aee66dc8c29cf7c1d7545 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Wed, 23 Oct 2024 09:29:01 -0400 Subject: [PATCH] Constants for account refresh (#32420) --- app/models/account.rb | 6 ++++-- spec/models/account_spec.rb | 8 ++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/models/account.rb b/app/models/account.rb index 708415b6ee..957ccc73c4 100644 --- a/app/models/account.rb +++ b/app/models/account.rb @@ -65,6 +65,8 @@ class Account < ApplicationRecord ) BACKGROUND_REFRESH_INTERVAL = 1.week.freeze + REFRESH_DEADLINE = 6.hours + STALE_THRESHOLD = 1.day DEFAULT_FIELDS_SIZE = 4 INSTANCE_ACTOR_ID = -99 @@ -229,13 +231,13 @@ class Account < ApplicationRecord end def possibly_stale? - last_webfingered_at.nil? || last_webfingered_at <= 1.day.ago + last_webfingered_at.nil? || last_webfingered_at <= STALE_THRESHOLD.ago end def schedule_refresh_if_stale! return unless last_webfingered_at.present? && last_webfingered_at <= BACKGROUND_REFRESH_INTERVAL.ago - AccountRefreshWorker.perform_in(rand(6.hours.to_i), id) + AccountRefreshWorker.perform_in(rand(REFRESH_DEADLINE), id) end def refresh! diff --git a/spec/models/account_spec.rb b/spec/models/account_spec.rb index a8f6ed7493..d129ef280d 100644 --- a/spec/models/account_spec.rb +++ b/spec/models/account_spec.rb @@ -208,16 +208,16 @@ RSpec.describe Account do end end - context 'when last_webfingered_at is more than 24 hours before' do - let(:last_webfingered_at) { 25.hours.ago } + context 'when last_webfingered_at is before the threshold' do + let(:last_webfingered_at) { (described_class::STALE_THRESHOLD + 1.hour).ago } it 'returns true' do expect(account.possibly_stale?).to be true end end - context 'when last_webfingered_at is less than 24 hours before' do - let(:last_webfingered_at) { 23.hours.ago } + context 'when last_webfingered_at is after the threshold' do + let(:last_webfingered_at) { (described_class::STALE_THRESHOLD - 1.hour).ago } it 'returns false' do expect(account.possibly_stale?).to be false