Sha256: 1248dc281fb7feb2ae9ff73f96a933520959657894cc2cb6e3415209ffc7a02a

Contents?: true

Size: 1.13 KB

Versions: 4

Compression:

Stored size: 1.13 KB

Contents

module AutomateEm
	
	class ResolverPool
		
		def initialize(size = 30)
			
			@size = size
			@jobs = Queue.new
			
			@pool = Array.new(@size) do |i|
				
				Thread.new do
					#Thread.current[:id] = i
					Thread.current.priority = Thread.current.priority - 1
					loop do
						begin
							job = @jobs.pop
							job.resolve
						rescue => e
							#
							# Print error here
							#
						end
					end
				end
				
			end
			
		end
		
		def schedule(job)
			@jobs << job
		end
	
	end
	
	
	class ResolverJob
		
		include EM::Deferrable
		
		def initialize(hostname)
			if IPAddress.valid? hostname
				self.succeed(hostname)
			else
				@hostname = hostname
				
				#
				# Enter self into resolver queue
				#
				if EM.reactor_thread?
					EM.defer do
						AutomateEm.resolver.schedule(self)
					end
				else
					AutomateEm.resolver.schedule(self)
				end
			end
		end
		
		def resolve 
			begin
				ip = Resolv.getaddress(@hostname)
				EM.schedule do
					self.succeed(ip)
				end
			rescue => e
				EM.schedule do
					self.fail(e)
				end
			end
		end
		
	end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
automate-em-0.0.4 lib/automate-em/core/resolver_pool.rb
automate-em-0.0.3 lib/automate-em/core/resolver_pool.rb
automate-em-0.0.2 lib/automate-em/core/resolver_pool.rb
automate-em-0.0.1 lib/automate-em/core/resolver_pool.rb