v0.43 Move to connection pooling for MySQL problem

This commit is contained in:
David Sainty 2024-08-18 02:41:56 +10:00
parent 416dc9c1e0
commit 855a0a9e46

View File

@ -1,7 +1,7 @@
# Federated Computer, Inc. # Federated Computer, Inc.
# David Sainty <saint@federated.computer> 2024 A.D. # David Sainty <saint@federated.computer> 2024 A.D.
# Gossamer Threads to Discourse -- Migration-Import Script # Gossamer Threads to Discourse -- Migration-Import Script
# v0.42.1 Rewrite and simplication of concurrent-ruby support # v0.43 Move to connection pooling for MySQL problem
require 'mysql2' require 'mysql2'
require 'open-uri' require 'open-uri'
@ -1064,8 +1064,17 @@ class GossamerForumsImporter < ImportScripts::Base
pool_size pool_size
end end
def threaded_topic_import
# 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; # 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 connection pool inside the method
mariadb_pool = ConnectionPool.new(size: 10, timeout: 5) do
Mysql2::Client.new(
host: "slowtwitch.northend.network",
username: "admin",
password: "yxnh93Ybbz2Nm8#mp28zCVv",
database: "slowtwitch"
)
end
# The query selects post_ids from gforum_Post where post_root_id is 0, meaning these posts are the topic starters (OPs). # The query selects post_ids from gforum_Post where post_root_id is 0, meaning these posts are the topic starters (OPs).
# Execute the query and fetch the result # Execute the query and fetch the result
@ -1124,22 +1133,26 @@ class GossamerForumsImporter < ImportScripts::Base
# Submit the import job for the current post_id to the thread pool # Submit the import job for the current post_id to the thread pool
pool.post do pool.post do
puts "PP 11 -- #{post_id}"
# Initialise a new MariaDB / Mysql2 client inside of each thread # Initialise a new MariaDB / Mysql2 client inside of each thread
mysql_client = Mysql2::Client.new( #### mysql_client = Mysql2::Client.new(
host: "slowtwitch.northend.network", #### host: "slowtwitch.northend.network",
username: "admin", #### username: "admin",
password: "yxnh93Ybbz2Nm8#mp28zCVv", #### password: "yxnh93Ybbz2Nm8#mp28zCVv",
database: "slowtwitch" #### database: "slowtwitch"
) #### )
puts "PP 22 -- #{post_id}" #### puts "PP 22 -- #{post_id}"
puts " FIRST Checking MySQL connection status..."
if mysql_client.query('SELECT 1').nil?
puts " MySQL connection is not valid"
else
puts " MySQL connection is valid"
end
begin begin
mariadb_pool.with do |mysql_client|
puts "PP 11 -- #{post_id} -- Checking MySQL connections status.."
#### puts " FIRST Checking MySQL connection status..."
#### if mysql_client.query('SELECT 1').nil?
#### puts " MySQL connection is not valid"
#### else
#### puts " MySQL connection is valid"
#### end
# Use connection pooling for PostgreSQL and synchronize access to shared resources # Use connection pooling for PostgreSQL and synchronize access to shared resources
ActiveRecord::Base.connection_pool.with_connection do ActiveRecord::Base.connection_pool.with_connection do
post_status = post_status(post_id) post_status = post_status(post_id)
@ -1153,22 +1166,23 @@ class GossamerForumsImporter < ImportScripts::Base
puts "Skipping post_id #{post_id}, already processed." puts "Skipping post_id #{post_id}, already processed."
end end
end end
end
rescue => e rescue => e
puts "Error processing post ID #{post_id}: #{e.message}" puts "Error processing post ID #{post_id}: #{e.message}"
sqlite_mutex.synchronize do sqlite_mutex.synchronize do
mark_post_as_failed(post_id) mark_post_as_failed(post_id)
end end
ensure #### ensure
# Ensure the MariaDB connection is closed after processing #### # Ensure the MariaDB connection is closed after processing
mysql_client.close if mysql_client #### mysql_client.close if mysql_client
puts "** CLOSED MariaDB client" #### puts "** CLOSED MariaDB client"
puts "PP 22 -- #{post_id}" #### puts "PP 22 -- #{post_id}"
puts " FINAL Checking MySQL connection status..." #### puts " FINAL Checking MySQL connection status..."
if mysql_client.query('SELECT 1').nil? #### if mysql_client.query('SELECT 1').nil?
puts " MySQL connection is not valid" #### puts " MySQL connection is not valid"
else #### else
puts " MySQL connection is valid" #### puts " MySQL connection is valid"
end #### end
end end
end end
end end