Class: MedularisDaemonsCommon::ThreadPool::ThreadPoolJobRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/threadpool.rb

Overview

Encapsulate each thread pool thread in object

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(thread_pool) ⇒ ThreadPoolJobRunner

Returns a new instance of ThreadPoolJobRunner.



25
26
27
28
29
# File 'lib/threadpool.rb', line 25

def initialize(thread_pool)
	@thread_pool = thread_pool
	@timeout_lock = Mutex.new
	@thread_lock  = Mutex.new
end

Instance Attribute Details

#time_startedObject

Returns the value of attribute time_started.



23
24
25
# File 'lib/threadpool.rb', line 23

def time_started
  @time_started
end

Instance Method Details

#check_timeout(timeout) ⇒ Object



44
45
46
47
48
49
50
51
# File 'lib/threadpool.rb', line 44

def check_timeout(timeout)
	@timeout_lock.synchronize {
		if !@time_started.nil? && Time.now - @time_started > timeout
			stop
			run
		end
	}
end

#runObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/threadpool.rb', line 31

def run
	@thread_lock.synchronize {
		@thread = Thread.new {
			until @thread_pool.terminate
				@timeout_lock.synchronize { @time_started = nil }
				work = @thread_pool.next_job
				@timeout_lock.synchronize { @time_started = Time.now }
				work.handler.call *work.params unless work.nil?
			end
		}
	}
end

#stopObject



53
54
55
56
57
58
59
60
# File 'lib/threadpool.rb', line 53

def stop
	@thread_lock.synchronize {
		if @thread.alive?
			@thread.kill
			@thread.join
		end
	}
end