Skip to content
Snippets Groups Projects
Commit 7ecee7a4 authored by Lin Jen-Shin's avatar Lin Jen-Shin
Browse files

Introduce ThreadedConnectionPool for customized pool

parent 0c2f4a3c
No related branches found
No related tags found
No related merge requests found
Loading
@@ -3,38 +3,27 @@ class RemoveInactiveDefaultEmailServices < ActiveRecord::Migration
Loading
@@ -3,38 +3,27 @@ class RemoveInactiveDefaultEmailServices < ActiveRecord::Migration
   
DOWNTIME = false DOWNTIME = false
   
def up disable_ddl_transaction!
builds_service = spawn <<-SQL.strip_heredoc
DELETE FROM services
WHERE type = 'BuildsEmailService'
AND active IS FALSE
AND properties = '{"notify_only_broken_builds":true}';
SQL
pipelines_service = spawn <<-SQL.strip_heredoc
DELETE FROM services
WHERE type = 'PipelinesEmailService'
AND active IS FALSE
AND properties = '{"notify_only_broken_pipelines":true}';
SQL
   
[builds_service, pipelines_service].each(&:join) def up
end Gitlab::Database::ThreadedConnectionPool.with_pool(2) do |pool|
pool.execute_async <<-SQL.strip_heredoc
private DELETE FROM services
WHERE type = 'BuildsEmailService'
AND active IS FALSE
AND properties = '{"notify_only_broken_builds":true}';
SQL
   
def spawn(query) pool.execute_async <<-SQL.strip_heredoc
Thread.new do DELETE FROM services
with_connection do |connection| WHERE type = 'PipelinesEmailService'
connection.execute(query) AND active IS FALSE
end AND properties = '{"notify_only_broken_pipelines":true}';
SQL
end end
end end
   
def with_connection(&block) def down
pool = ActiveRecord::Base.establish_connection # Nothing can be done to restore the records
pool.with_connection(&block)
ensure
pool.disconnect!
end end
end end
module Gitlab
module Database
class ThreadedConnectionPool
def self.with_pool(pool_size)
pool = new(pool_size)
yield(pool)
ensure
pool.join
pool.close
end
def initialize(pool_size)
config = ActiveRecord::Base.configurations[Rails.env]
@ar_pool = ActiveRecord::Base.establish_connection(
config.merge(pool: pool_size))
@workers = []
@mutex = Mutex.new
end
def execute_async(sql)
@mutex.synchronize do
@workers << Thread.new do
@ar_pool.with_connection do |connection|
connection.execute(sql)
end
end
end
end
def join
threads = nil
@mutex.synchronize do
threads = @workers.dup
@workers.clear
end
threads.each(&:join)
end
def close
@ar_pool.disconnect!
end
end
end
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment