Last active
December 16, 2015 19:49
-
-
Save we4tech/5487442 to your computer and use it in GitHub Desktop.
Thread pool, and separate mongo client with each thread.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ThreadPool | |
attr_accessor :size, :pool | |
def logger | |
BeatDeckMachine::logger | |
end | |
def initialize(size) | |
@size = size | |
@jobs = Queue.new | |
@pool = create_pool | |
end | |
def create_pool | |
@size.times.map do |i| | |
Thread.start do | |
Thread.current[:id] = i | |
catch(:thread_quit) do | |
session = create_mongo_client | |
loop do | |
job, args = @jobs.pop | |
args << session | |
logger.debug "Thread(#{Thread.current[:id]}): Handling job" | |
job.call *args | |
end | |
end | |
end | |
end | |
end | |
def join | |
#@pool.map(&:join) | |
# This cause Deadlock ? exceptino from rb_check_deadlock | |
# To avoid this situation i've used another thread to check whether all threads from pool are dead already? | |
# Example - | |
# Thread.new do | |
# break until pool.pool.select(&:alive?).size.zero? | |
# end.join | |
end | |
def shutdown | |
logger.info 'Shutting down threads.' | |
@size.times do | |
add { throw :thread_quit } | |
end | |
@pool.map &:join | |
end | |
def add(*args, &job) | |
logger.debug 'Added new job in queue' | |
@jobs << [job, args] | |
end | |
def queue_size; | |
@jobs.size | |
end | |
def create_mongo_client | |
# Some stuffs here with Moped::Session.new | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment