mirror of
https://github.com/discourse/discourse.git
synced 2024-12-15 11:33:40 +08:00
6bf0ac730f
Firstly, we need to understand that ActiveRecord can be connected to a role which prevent writes and this happens in Discourse when a replica database has been setup for failover purposes. When a role prevent writes from happening, ActiveRecord will raise the `ActiveRecord::ReadOnlyError` if a write query is attempted. Secondly, theme fields are baked at runtime within GET requests. The baking process involves writing the baked value to the `ThemeField#baked_value` column in the database. If we combine the two points above, we can see how the writing of the baked value to the database will trigger a `ActiveRecord::ReadOnlyError` in a GET requests when the database is connected to a role preventing writes. However, failing to bake a theme is not the end of the world and should not cause GET requests to fail. Therefore, this commit adds a rescue for `ActiveRecord::ReadOnlyError` in the `ThemeField#ensure_baked!` method.
149 lines
3.5 KiB
Ruby
149 lines
3.5 KiB
Ruby
# frozen_string_literal: true
|
|
require "weakref"
|
|
|
|
module Scheduler
|
|
module Deferrable
|
|
DEFAULT_TIMEOUT = 90
|
|
STATS_CACHE_SIZE = 100
|
|
|
|
attr_reader :async
|
|
|
|
def initialize
|
|
@async = !Rails.env.test?
|
|
@queue =
|
|
WorkQueue::ThreadSafeWrapper.new(
|
|
WorkQueue::FairQueue.new(:site, 500) do
|
|
WorkQueue::FairQueue.new(:user, 100) { WorkQueue::BoundedQueue.new(50) }
|
|
end,
|
|
)
|
|
|
|
@mutex = Mutex.new
|
|
@stats_mutex = Mutex.new
|
|
@paused = false
|
|
@thread = nil
|
|
@reactor = nil
|
|
@timeout = DEFAULT_TIMEOUT
|
|
@stats = LruRedux::ThreadSafeCache.new(STATS_CACHE_SIZE)
|
|
end
|
|
|
|
def timeout=(t)
|
|
@mutex.synchronize { @timeout = t }
|
|
end
|
|
|
|
def length
|
|
@queue.size
|
|
end
|
|
|
|
def stats
|
|
@stats_mutex.synchronize { @stats.to_a }
|
|
end
|
|
|
|
def pause
|
|
stop!
|
|
@paused = true
|
|
end
|
|
|
|
def resume
|
|
@paused = false
|
|
end
|
|
|
|
# for test and sidekiq
|
|
def async=(val)
|
|
@async = val
|
|
end
|
|
|
|
def later(
|
|
desc = nil,
|
|
db = RailsMultisite::ConnectionManagement.current_db,
|
|
force: true,
|
|
current_user: nil,
|
|
&blk
|
|
)
|
|
@stats_mutex.synchronize do
|
|
stats = (@stats[desc] ||= { queued: 0, finished: 0, duration: 0, errors: 0 })
|
|
stats[:queued] += 1
|
|
end
|
|
|
|
if @async
|
|
start_thread if !@thread&.alive? && !@paused
|
|
@queue.push({ site: db, user: current_user, db: db, job: blk, desc: desc }, force: force)
|
|
else
|
|
blk.call
|
|
end
|
|
end
|
|
|
|
def stop!
|
|
@thread.kill if @thread&.alive?
|
|
@thread = nil
|
|
@reactor&.stop
|
|
@reactor = nil
|
|
end
|
|
|
|
# test only
|
|
def stopped?
|
|
!@thread&.alive?
|
|
end
|
|
|
|
def do_all_work
|
|
do_work(non_block = true) while !@queue.empty?
|
|
end
|
|
|
|
private
|
|
|
|
def start_thread
|
|
@mutex.synchronize do
|
|
@reactor = MessageBus::TimerThread.new if !@reactor
|
|
@thread =
|
|
Thread.new do
|
|
@thread.abort_on_exception = true if Rails.env.test?
|
|
do_work while true
|
|
end if !@thread&.alive?
|
|
end
|
|
end
|
|
|
|
# using non_block to match Ruby #deq
|
|
def do_work(non_block = false)
|
|
db, job, desc = @queue.shift(block: !non_block).values_at(:db, :job, :desc)
|
|
|
|
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
db ||= RailsMultisite::ConnectionManagement::DEFAULT
|
|
|
|
RailsMultisite::ConnectionManagement.with_connection(db) do
|
|
begin
|
|
warning_job =
|
|
@reactor.queue(@timeout) do
|
|
Rails.logger.error "'#{desc}' is still running after #{@timeout} seconds on db #{db}, this process may need to be restarted!"
|
|
end if !non_block
|
|
job.call
|
|
rescue => ex
|
|
@stats_mutex.synchronize do
|
|
stats = @stats[desc]
|
|
stats[:errors] += 1 if stats
|
|
end
|
|
Discourse.handle_job_exception(ex, message: "Running deferred code '#{desc}'")
|
|
ensure
|
|
warning_job&.cancel
|
|
end
|
|
end
|
|
rescue => ex
|
|
Discourse.handle_job_exception(ex, message: "Processing deferred code queue")
|
|
ensure
|
|
ActiveRecord::Base.connection_handler.clear_active_connections!
|
|
if start
|
|
@stats_mutex.synchronize do
|
|
stats = @stats[desc]
|
|
if stats
|
|
stats[:finished] += 1
|
|
stats[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
|
|
end
|
|
end
|
|
end
|
|
end
|
|
end
|
|
|
|
class Defer
|
|
extend Deferrable
|
|
initialize
|
|
end
|
|
end
|