mirror of
https://github.com/discourse/discourse.git
synced 2025-09-06 10:50:21 +08:00
FIX: Ensure that we revert back to default connection after running jobs.
This commit is contained in:
parent
9b4fd0b26b
commit
09721090a3
4 changed files with 35 additions and 26 deletions
|
@ -126,23 +126,23 @@ module Jobs
|
||||||
begin
|
begin
|
||||||
exception = {}
|
exception = {}
|
||||||
|
|
||||||
begin
|
RailsMultisite::ConnectionManagement.with_connection(db: db) do
|
||||||
RailsMultisite::ConnectionManagement.establish_connection(db: db)
|
|
||||||
I18n.locale = SiteSetting.default_locale || "en"
|
|
||||||
I18n.ensure_all_loaded!
|
|
||||||
begin
|
begin
|
||||||
execute(opts)
|
I18n.locale = SiteSetting.default_locale || "en"
|
||||||
|
I18n.ensure_all_loaded!
|
||||||
|
begin
|
||||||
|
execute(opts)
|
||||||
|
rescue => e
|
||||||
|
exception[:ex] = e
|
||||||
|
exception[:other] = { problem_db: db }
|
||||||
|
end
|
||||||
rescue => e
|
rescue => e
|
||||||
exception[:ex] = e
|
exception[:ex] = e
|
||||||
|
exception[:message] = "While establishing database connection to #{db}"
|
||||||
exception[:other] = { problem_db: db }
|
exception[:other] = { problem_db: db }
|
||||||
|
ensure
|
||||||
|
total_db_time += Instrumenter.stats.duration_ms
|
||||||
end
|
end
|
||||||
rescue => e
|
|
||||||
exception[:ex] = e
|
|
||||||
exception[:message] = "While establishing database connection to #{db}"
|
|
||||||
exception[:other] = { problem_db: db }
|
|
||||||
ensure
|
|
||||||
ActiveRecord::Base.connection_handler.clear_active_connections!
|
|
||||||
total_db_time += Instrumenter.stats.duration_ms
|
|
||||||
end
|
end
|
||||||
|
|
||||||
exceptions << exception unless exception.empty?
|
exceptions << exception unless exception.empty?
|
||||||
|
|
|
@ -63,18 +63,21 @@ module Scheduler
|
||||||
# using non_block to match Ruby #deq
|
# using non_block to match Ruby #deq
|
||||||
def do_work(non_block = false)
|
def do_work(non_block = false)
|
||||||
db, job, desc = @queue.deq(non_block)
|
db, job, desc = @queue.deq(non_block)
|
||||||
|
|
||||||
begin
|
begin
|
||||||
RailsMultisite::ConnectionManagement.establish_connection(db: db) if db
|
if db
|
||||||
job.call
|
RailsMultisite::ConnectionManagement.with_connection(db: db) do
|
||||||
|
job.call
|
||||||
|
end
|
||||||
|
else
|
||||||
|
job.call
|
||||||
|
end
|
||||||
rescue => ex
|
rescue => ex
|
||||||
Discourse.handle_job_exception(ex, message: "Running deferred code '#{desc}'")
|
Discourse.handle_job_exception(ex, message: "Running deferred code '#{desc}'")
|
||||||
end
|
end
|
||||||
rescue => ex
|
rescue => ex
|
||||||
Discourse.handle_job_exception(ex, message: "Processing deferred code queue")
|
Discourse.handle_job_exception(ex, message: "Processing deferred code queue")
|
||||||
ensure
|
|
||||||
ActiveRecord::Base.connection_handler.clear_active_connections!
|
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
class Defer
|
class Defer
|
||||||
|
|
|
@ -77,9 +77,7 @@ module Scheduler
|
||||||
@mutex.synchronize { info.write! }
|
@mutex.synchronize { info.write! }
|
||||||
|
|
||||||
if @manager.enable_stats
|
if @manager.enable_stats
|
||||||
begin
|
RailsMultisite::ConnectionManagement.with_connection(db: "default") do
|
||||||
RailsMultisite::ConnectionManagement.establish_connection(db: "default")
|
|
||||||
|
|
||||||
stat = SchedulerStat.create!(
|
stat = SchedulerStat.create!(
|
||||||
name: klass.to_s,
|
name: klass.to_s,
|
||||||
hostname: hostname,
|
hostname: hostname,
|
||||||
|
@ -87,8 +85,6 @@ module Scheduler
|
||||||
started_at: Time.zone.now,
|
started_at: Time.zone.now,
|
||||||
live_slots_start: GC.stat[:heap_live_slots]
|
live_slots_start: GC.stat[:heap_live_slots]
|
||||||
)
|
)
|
||||||
ensure
|
|
||||||
ActiveRecord::Base.connection_handler.clear_active_connections!
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -106,16 +102,13 @@ module Scheduler
|
||||||
info.prev_result = failed ? "FAILED" : "OK"
|
info.prev_result = failed ? "FAILED" : "OK"
|
||||||
info.current_owner = nil
|
info.current_owner = nil
|
||||||
if stat
|
if stat
|
||||||
begin
|
RailsMultisite::ConnectionManagement.with_connection(db: "default") do
|
||||||
RailsMultisite::ConnectionManagement.establish_connection(db: "default")
|
|
||||||
stat.update!(
|
stat.update!(
|
||||||
duration_ms: duration,
|
duration_ms: duration,
|
||||||
live_slots_finish: GC.stat[:heap_live_slots],
|
live_slots_finish: GC.stat[:heap_live_slots],
|
||||||
success: !failed,
|
success: !failed,
|
||||||
error: error
|
error: error
|
||||||
)
|
)
|
||||||
ensure
|
|
||||||
ActiveRecord::Base.connection_handler.clear_active_connections!
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
attempts(3) do
|
attempts(3) do
|
||||||
|
|
13
spec/multisite/jobs.rb
Normal file
13
spec/multisite/jobs.rb
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
require 'rails_helper'
|
||||||
|
|
||||||
|
RSpec.describe "Running Sidekiq Jobs in Multisite" do
|
||||||
|
it 'should revert back to the default connection' do
|
||||||
|
expect(RailsMultisite::ConnectionManagement.current_db)
|
||||||
|
.to eq('default')
|
||||||
|
|
||||||
|
Jobs::DestroyOldDeletionStubs.new.perform({})
|
||||||
|
|
||||||
|
expect(RailsMultisite::ConnectionManagement.current_db)
|
||||||
|
.to eq('default')
|
||||||
|
end
|
||||||
|
end
|
Loading…
Add table
Add a link
Reference in a new issue