Class: BeanCounter::Strategy::StalkClimberStrategy
- Inherits:
-
BeanCounter::Strategy
- Object
- BeanCounter::Strategy
- BeanCounter::Strategy::StalkClimberStrategy
- Extended by:
- Forwardable
- Defined in:
- lib/bean_counter/strategies/stalk_climber_strategy.rb
Constant Summary collapse
- STATS_METHOD_NAMES =
Index of what method should be called to retrieve each stat
begin attrs = ( BeanCounter::Strategy::MATCHABLE_JOB_ATTRIBUTES + BeanCounter::Strategy::MATCHABLE_TUBE_ATTRIBUTES ).map!(&:to_sym).uniq.sort method_names = attrs.map {|method| method.to_s.gsub(/-/, '_').to_sym } attr_methods = Hash[attrs.zip(method_names)] attr_methods[:pause] = :pause_time attr_methods end
- TEST_TUBE =
Default tube used by StalkClimber when probing the Beanstalkd pool
'bean_counter_stalk_climber_test'
Constants inherited from BeanCounter::Strategy
MATCHABLE_JOB_ATTRIBUTES, MATCHABLE_TUBE_ATTRIBUTES
Instance Attribute Summary collapse
-
#test_tube ⇒ Object
private
Accessor for test_tube that defaults to TEST_TUBE.
Instance Method Summary collapse
-
#climber ⇒ StalkClimber::Climber
private
StalkClimber instance used to climb/crawl beanstalkd pool.
-
#collect_new_jobs { ... } ⇒ Array<StalkClimber::Job>
Collects all jobs enqueued during the execution of the provided
block
. -
#delete_job(job) ⇒ Boolean
Attempts to delete the given StalkClimber::Job
job
. -
#job_matches?(job, options = {}) ⇒ Boolean
Returns a Boolean indicating whether or not the provided StalkClimber::Job
job
matches the given Hash ofoptions
. -
#matcher(valid_attributes, matchable, opts = {}) ⇒ Boolean
private
Given the set of valid attributes,
valid_attributes
, determines if everyvalue
ofopts
evaluates to true when compared to the attribute ofmatchable
identified by the correspondingopts
key
. -
#pretty_print_job(job) ⇒ String
Returns a String representation of the StalkClimber::Job
job
in a pretty, human readable format. -
#pretty_print_tube(tube) ⇒ String
Returns a String representation of
tube
in a pretty, human readable format. -
#stats_method_name(stats_attr) ⇒ Symbol
private
Simplify lookup of what method to call to retrieve requested stat.
-
#tube_matches?(tube, options = {}) ⇒ Boolean
Returns a boolean indicating whether or not the provided StalkClimber
tube
matches the given Hash ofoptions
.
Methods inherited from BeanCounter::Strategy
inherited, #jobs, known_strategy?, materialize_strategy, strategies, #tubes
Instance Attribute Details
#test_tube ⇒ Object (private)
Accessor for test_tube that defaults to TEST_TUBE
167 168 169 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 167 def test_tube return @test_tube ||= TEST_TUBE end |
Instance Method Details
#climber ⇒ StalkClimber::Climber (private)
StalkClimber instance used to climb/crawl beanstalkd pool
141 142 143 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 141 def climber return @climber ||= StalkClimber::Climber.new(BeanCounter.beanstalkd_url, test_tube) end |
#collect_new_jobs { ... } ⇒ Array<StalkClimber::Job>
Collects all jobs enqueued during the execution of the provided block
.
Returns an Array of StalkClimber::Job.
Fulfills BeanCounter::Strategy#collect_new_jobs contract.
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 39 def collect_new_jobs raise ArgumentError, 'Block required' unless block_given? min_ids = climber.max_job_ids yield max_ids = climber.max_job_ids new_jobs = [] min_ids.each do |connection, min_id| testable_ids = (min_id..max_ids[connection]).to_a new_jobs.concat(connection.fetch_jobs(testable_ids).compact) end return new_jobs end |
#delete_job(job) ⇒ Boolean
Attempts to delete the given StalkClimber::Job job
. Returns true if
deletion succeeds or if job
does not exist. Returns false if job
could
not be deleted (typically due to it being reserved by another connection).
Fulfills BeanCounter::Strategy#delete_job contract.
64 65 66 67 68 69 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 64 def delete_job(job) job.delete return true rescue Beaneater::NotFoundError return job.exists? ? false : true end |
#job_matches?(job, options = {}) ⇒ Boolean
Returns a Boolean indicating whether or not the provided StalkClimber::Job
job
matches the given Hash of options
.
See MATCHABLE_JOB_ATTRIBUTES for a list of attributes that can be used when matching.
Fulfills BeanCounter::Strategy#job_matches? contract.
88 89 90 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 88 def job_matches?(job, = {}) return matcher(MATCHABLE_JOB_ATTRIBUTES, job, ) end |
#matcher(valid_attributes, matchable, opts = {}) ⇒ Boolean (private)
Given the set of valid attributes, valid_attributes
, determines if every
value
of opts
evaluates to true when compared to the attribute of
matchable
identified by the corresponding opts
key
.
150 151 152 153 154 155 156 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 150 def matcher(valid_attributes, matchable, opts = {}) # Refresh state/stats before checking match return false unless matchable.exists? return (opts.keys & valid_attributes).all? do |key| opts[key] === matchable.send(stats_method_name(key)) end end |
#pretty_print_job(job) ⇒ String
Returns a String representation of the StalkClimber::Job job
in a pretty,
human readable format.
Fulfills BeanCounter::Strategy#pretty_print_job contract.
101 102 103 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 101 def pretty_print_job(job) return job.to_h.to_s end |
#pretty_print_tube(tube) ⇒ String
Returns a String representation of tube
in a pretty, human readable format.
Fulfills BeanCounter::Strategy#pretty_print_tube contract.
113 114 115 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 113 def pretty_print_tube(tube) return tube.to_h.to_s end |
#stats_method_name(stats_attr) ⇒ Symbol (private)
Simplify lookup of what method to call to retrieve requested stat
161 162 163 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 161 def stats_method_name(stats_attr) return STATS_METHOD_NAMES[stats_attr.to_sym] end |
#tube_matches?(tube, options = {}) ⇒ Boolean
Returns a boolean indicating whether or not the provided StalkClimber tube
matches the given Hash of options
.
See MATCHABLE_TUBE_ATTRIBUTES for a list of attributes that can be used when evaluating a match.
Fulfills BeanCounter::Strategy#tube_matches? contract.
133 134 135 |
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 133 def tube_matches?(tube, = {}) return matcher(MATCHABLE_TUBE_ATTRIBUTES, tube, ) end |