v0.52 Improve handling and look at how we can improve ActiveRecord config for PostgreSQL and avoid insufficient pool size

This commit is contained in:
David Sainty 2024-08-19 22:01:47 +10:00
parent af3c46e1cf
commit e312a4ed12

View File

@ -1075,13 +1075,17 @@ class GossamerForumsImporter < ImportScripts::Base
# Get list of TOPICS / OP posts, i.e. post ids that have no parent / root id - SELECT post_id FROM gforum_Post WHERE post_root_id = 0;
def threaded_topic_import
# Define the custom connection pool settings
custom_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(
ActiveRecord::Base.connection_pool.spec.to_h.merge(pool: 40, timeout: 5000)
# Update connection pool settings
ActiveRecord::Base.establish_connection(
ActiveRecord::Base.connection_db_config.configuration_hash.merge(pool: 40, timeout: 5000)
)
# Register the custom connection pool under a unique identifier
ActiveRecord::Base.connection_handler.connection_pools['CustomPool'] = custom_pool
### # Define the custom connection pool settings
### custom_pool = ActiveRecord::ConnectionAdapters::ConnectionPool.new(
### ActiveRecord::Base.connection_pool.spec.to_h.merge(pool: 40, timeout: 5000)
### )
### # Register the custom connection pool under a unique identifier
### ActiveRecord::Base.connection_handler.connection_pools['CustomPool'] = custom_pool
# Use CachedThreadPool for dynamic thread management
#### pool = Concurrent::CachedThreadPool.new
@ -1181,7 +1185,8 @@ class GossamerForumsImporter < ImportScripts::Base
# Use connection pooling for PostgreSQL and synchronize access to shared resources
# ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
# ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.connected_to do
post_status = fetch_post_status(post_id)
if post_status.nil? || post_status == 0
puts "Starting import for post_id #{post_id}"
@ -1322,14 +1327,11 @@ class GossamerForumsImporter < ImportScripts::Base
unless TopicCustomField.exists?(name: 'original_gossamer_id', value: row['post_id'])
puts "TIJ EE post_id #{post_id}"
# ActiveRecord::Base.transaction do
## ActiveRecord::Base.connected_to(pool: 'CustomPool') do
## ActiveRecord::Base.transaction do
# Create the new topic in Discourse
begin
suffix = 1
topic_created = false
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do
while !topic_created
begin
@ -1359,7 +1361,6 @@ class GossamerForumsImporter < ImportScripts::Base
# puts e.backtrace.join("\n") # Print the full stack trace
end
end
end
end
# Workaround... take a copy of topic.id
@ -1388,7 +1389,6 @@ class GossamerForumsImporter < ImportScripts::Base
puts "TIJ GG post_id #{post_id}"
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do
# Create the initial post in the new topic
post = Post.create!(
@ -1402,7 +1402,6 @@ class GossamerForumsImporter < ImportScripts::Base
)
post.custom_fields['original_gossamer_id'] = row['post_id']
post.save!
end
end
sqlite_mutex.synchronize do
@ -1413,11 +1412,9 @@ class GossamerForumsImporter < ImportScripts::Base
puts "TIJ HH post_id #{post_id}"
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do
# Handle any attachments associated with the post
handle_post_attachments(row['post_id'], post, discourse_user_id, mysql_client)
end
end
# Create URL mappings for the new topic
@ -1467,7 +1464,6 @@ class GossamerForumsImporter < ImportScripts::Base
end
puts "TIJ JJ post_id #{post_id} reply post_id #{reply_row['post_id']} reply_post_views #{reply_post_views || 0} post_number #{post_number} current_topic_id #{current_topic_id} reply_post_views #{reply_post_views || 0}"
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do
# Create the reply post in the existing topic
post = Post.create!(
@ -1481,7 +1477,6 @@ class GossamerForumsImporter < ImportScripts::Base
)
post.custom_fields['original_gossamer_id'] = reply_row['post_id']
post.save!
end
end
puts "TIJ KK post_id #{post_id}"
@ -1499,12 +1494,10 @@ class GossamerForumsImporter < ImportScripts::Base
end
end
ActiveRecord::Base.connected_to(pool: 'CustomPool') do
ActiveRecord::Base.transaction do
# Handle any attachments associated with the reply
handle_post_attachments(reply_row['post_id'], post, reply_user_id, mysql_client)
end
end
# # Update the highest processed post_id in the database (thread-safe)
# update_highest_processed_post_id_thread_safe(reply_row['post_id'])
@ -1523,7 +1516,6 @@ class GossamerForumsImporter < ImportScripts::Base
puts "Error importing topic with post_id #{row['post_id']}: #{e.message}"
raise ActiveRecord::Rollback
end
# end
# end
else
puts "Topic for post_id #{row['post_id']} already exists, skipping creation."