Class: StalkClimber::Climber
- Inherits:
-
Object
- Object
- StalkClimber::Climber
- Defined in:
- lib/stalk_climber/climber.rb
Instance Attribute Summary collapse
-
#beanstalk_addresses ⇒ Object
Returns the value of attribute beanstalk_addresses.
-
#cache ⇒ Object
readonly
Returns the value of attribute cache.
-
#test_tube ⇒ Object
Returns the value of attribute test_tube.
Instance Method Summary collapse
-
#climb ⇒ Object
(also: #each)
Climb over all jobs on all connections in the connection pool.
-
#climb_threaded(&block) ⇒ Object
(also: #each_threaded)
Perform a threaded climb across all connections in the connection pool.
-
#connection_pool ⇒ Object
Returns or creates a ConnectionPool from beanstalk_addresses.
-
#initialize(beanstalk_addresses = nil, test_tube = nil) {|_self| ... } ⇒ Climber
constructor
Creates a new Climber instance, optionally yielding the instance if a block is given.
-
#max_job_ids ⇒ Object
Returns a hash with connections as keys and max_job_ids as values.
-
#to_enum ⇒ Object
Returns an Enumerator for enumerating jobs on all connections.
Constructor Details
#initialize(beanstalk_addresses = nil, test_tube = nil) {|_self| ... } ⇒ Climber
Creates a new Climber instance, optionally yielding the instance if a block is given
54 55 56 57 58 |
# File 'lib/stalk_climber/climber.rb', line 54 def initialize(beanstalk_addresses = nil, test_tube = nil) self.beanstalk_addresses = beanstalk_addresses self.test_tube = test_tube yield(self) if block_given? end |
Instance Attribute Details
#beanstalk_addresses ⇒ Object
Returns the value of attribute beanstalk_addresses.
5 6 7 |
# File 'lib/stalk_climber/climber.rb', line 5 def beanstalk_addresses @beanstalk_addresses end |
#cache ⇒ Object (readonly)
Returns the value of attribute cache.
6 7 8 |
# File 'lib/stalk_climber/climber.rb', line 6 def cache @cache end |
#test_tube ⇒ Object
Returns the value of attribute test_tube.
5 6 7 |
# File 'lib/stalk_climber/climber.rb', line 5 def test_tube @test_tube end |
Instance Method Details
#climb ⇒ Object Also known as: each
Climb over all jobs on all connections in the connection pool. An instance of Job is yielded. For more information see Connection#climb
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/stalk_climber/climber.rb', line 20 def climb enum = to_enum return enum unless block_given? loop do begin yield enum.next rescue StopIteration => e return (e.nil? || !e.respond_to?(:result) || e.result.nil?) ? nil : e.result end end end |
#climb_threaded(&block) ⇒ Object Also known as: each_threaded
Perform a threaded climb across all connections in the connection pool. This method cannot be used for enumerable enumeration because a break called from one of the threads will cause a LocalJumpError. This could be fixed, but expected behavior on break varies as to whether or not to wait for all threads before returning a result. However, still useful for operations that always visit all jobs. An instance of Job is yielded to block
41 42 43 44 45 46 47 48 |
# File 'lib/stalk_climber/climber.rb', line 41 def climb_threaded(&block) threads = [] self.connection_pool.connections.each do |connection| threads << Thread.new { connection.each(&block) } end threads.each(&:join) return end |
#connection_pool ⇒ Object
Returns or creates a ConnectionPool from beanstalk_addresses
9 10 11 12 13 14 15 |
# File 'lib/stalk_climber/climber.rb', line 9 def connection_pool return @connection_pool unless @connection_pool.nil? if self.beanstalk_addresses.nil? raise RuntimeError, 'beanstalk_addresses must be set in order to establish a connection' end @connection_pool = ConnectionPool.new(self.beanstalk_addresses, self.test_tube) end |
#max_job_ids ⇒ Object
Returns a hash with connections as keys and max_job_ids as values
62 63 64 65 66 67 |
# File 'lib/stalk_climber/climber.rb', line 62 def max_job_ids connection_pairs = connection_pool.connections.map do |connection| [connection, connection.max_job_id] end return Hash[connection_pairs] end |
#to_enum ⇒ Object
Returns an Enumerator for enumerating jobs on all connections. Connections are enumerated in the order defined. See Connection#to_enum for more information A job is yielded with each iteration.
74 75 76 77 78 79 80 81 82 |
# File 'lib/stalk_climber/climber.rb', line 74 def to_enum return Enumerator.new do |yielder| self.connection_pool.connections.each do |connection| connection.each do |job| yielder << job end end end end |