Change notifications API to use a replica (#25874)
This commit is contained in:
parent
6ee7c03b28
commit
315002c1be
8 changed files with 31 additions and 11 deletions
|
@ -9,8 +9,12 @@ class Api::V1::NotificationsController < Api::BaseController
|
||||||
DEFAULT_NOTIFICATIONS_LIMIT = 40
|
DEFAULT_NOTIFICATIONS_LIMIT = 40
|
||||||
|
|
||||||
def index
|
def index
|
||||||
|
with_read_replica do
|
||||||
@notifications = load_notifications
|
@notifications = load_notifications
|
||||||
render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id)
|
@relationships = StatusRelationshipsPresenter.new(target_statuses_from_notifications, current_user&.account_id)
|
||||||
|
end
|
||||||
|
|
||||||
|
render json: @notifications, each_serializer: REST::NotificationSerializer, relationships: @relationships
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -6,7 +6,7 @@ class Api::V1::Timelines::HomeController < Api::BaseController
|
||||||
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
|
after_action :insert_pagination_headers, unless: -> { @statuses.empty? }
|
||||||
|
|
||||||
def show
|
def show
|
||||||
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
|
with_read_replica do
|
||||||
@statuses = load_statuses
|
@statuses = load_statuses
|
||||||
@relationships = StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
@relationships = StatusRelationshipsPresenter.new(@statuses, current_user&.account_id)
|
||||||
end
|
end
|
||||||
|
|
|
@ -10,6 +10,7 @@ class ApplicationController < ActionController::Base
|
||||||
include SessionTrackingConcern
|
include SessionTrackingConcern
|
||||||
include CacheConcern
|
include CacheConcern
|
||||||
include DomainControlHelper
|
include DomainControlHelper
|
||||||
|
include DatabaseHelper
|
||||||
|
|
||||||
helper_method :current_account
|
helper_method :current_account
|
||||||
helper_method :current_session
|
helper_method :current_session
|
||||||
|
|
11
app/helpers/database_helper.rb
Normal file
11
app/helpers/database_helper.rb
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module DatabaseHelper
|
||||||
|
def with_read_replica(&block)
|
||||||
|
ApplicationRecord.connected_to(role: :read, prevent_writes: true, &block)
|
||||||
|
end
|
||||||
|
|
||||||
|
def with_primary(&block)
|
||||||
|
ApplicationRecord.connected_to(role: :primary, &block)
|
||||||
|
end
|
||||||
|
end
|
|
@ -2,9 +2,10 @@
|
||||||
|
|
||||||
class FeedInsertWorker
|
class FeedInsertWorker
|
||||||
include Sidekiq::Worker
|
include Sidekiq::Worker
|
||||||
|
include DatabaseHelper
|
||||||
|
|
||||||
def perform(status_id, id, type = 'home', options = {})
|
def perform(status_id, id, type = 'home', options = {})
|
||||||
ApplicationRecord.connected_to(role: :primary) do
|
with_primary do
|
||||||
@type = type.to_sym
|
@type = type.to_sym
|
||||||
@status = Status.find(status_id)
|
@status = Status.find(status_id)
|
||||||
@options = options.symbolize_keys
|
@options = options.symbolize_keys
|
||||||
|
@ -18,7 +19,7 @@ class FeedInsertWorker
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
|
with_read_replica do
|
||||||
check_and_insert
|
check_and_insert
|
||||||
end
|
end
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
|
|
@ -3,14 +3,15 @@
|
||||||
class MergeWorker
|
class MergeWorker
|
||||||
include Sidekiq::Worker
|
include Sidekiq::Worker
|
||||||
include Redisable
|
include Redisable
|
||||||
|
include DatabaseHelper
|
||||||
|
|
||||||
def perform(from_account_id, into_account_id)
|
def perform(from_account_id, into_account_id)
|
||||||
ApplicationRecord.connected_to(role: :primary) do
|
with_primary do
|
||||||
@from_account = Account.find(from_account_id)
|
@from_account = Account.find(from_account_id)
|
||||||
@into_account = Account.find(into_account_id)
|
@into_account = Account.find(into_account_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
|
with_read_replica do
|
||||||
FeedManager.instance.merge_into_home(@from_account, @into_account)
|
FeedManager.instance.merge_into_home(@from_account, @into_account)
|
||||||
end
|
end
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
|
|
@ -2,15 +2,16 @@
|
||||||
|
|
||||||
class RegenerationWorker
|
class RegenerationWorker
|
||||||
include Sidekiq::Worker
|
include Sidekiq::Worker
|
||||||
|
include DatabaseHelper
|
||||||
|
|
||||||
sidekiq_options lock: :until_executed
|
sidekiq_options lock: :until_executed
|
||||||
|
|
||||||
def perform(account_id, _ = :home)
|
def perform(account_id, _ = :home)
|
||||||
ApplicationRecord.connected_to(role: :primary) do
|
with_primary do
|
||||||
@account = Account.find(account_id)
|
@account = Account.find(account_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
|
with_read_replica do
|
||||||
PrecomputeFeedService.new.call(@account)
|
PrecomputeFeedService.new.call(@account)
|
||||||
end
|
end
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
|
|
@ -2,16 +2,17 @@
|
||||||
|
|
||||||
class UnmergeWorker
|
class UnmergeWorker
|
||||||
include Sidekiq::Worker
|
include Sidekiq::Worker
|
||||||
|
include DatabaseHelper
|
||||||
|
|
||||||
sidekiq_options queue: 'pull'
|
sidekiq_options queue: 'pull'
|
||||||
|
|
||||||
def perform(from_account_id, into_account_id)
|
def perform(from_account_id, into_account_id)
|
||||||
ApplicationRecord.connected_to(role: :primary) do
|
with_primary do
|
||||||
@from_account = Account.find(from_account_id)
|
@from_account = Account.find(from_account_id)
|
||||||
@into_account = Account.find(into_account_id)
|
@into_account = Account.find(into_account_id)
|
||||||
end
|
end
|
||||||
|
|
||||||
ApplicationRecord.connected_to(role: :read, prevent_writes: true) do
|
with_read_replica do
|
||||||
FeedManager.instance.unmerge_from_home(@from_account, @into_account)
|
FeedManager.instance.unmerge_from_home(@from_account, @into_account)
|
||||||
end
|
end
|
||||||
rescue ActiveRecord::RecordNotFound
|
rescue ActiveRecord::RecordNotFound
|
||||||
|
|
Loading…
Reference in a new issue