Class: BeanCounter::Strategy::StalkClimberStrategy

Inherits:
BeanCounter::Strategy show all
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

Instance Method Summary collapse

Methods inherited from BeanCounter::Strategy

inherited, #jobs, known_strategy?, materialize_strategy, strategies, #tubes

Instance Attribute Details

#test_tubeObject (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

#climberStalkClimber::Climber (private)

StalkClimber instance used to climb/crawl beanstalkd pool

Returns:

  • (StalkClimber::Climber)


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.

Yields:

  • Nothing is yielded to the provided block

Returns:

  • (Array<StalkClimber::Job>)

    all jobs enqueued during the execution of the provided block

Raises:

  • (ArgumentError)

    if a block is not provided.

See Also:



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.

Parameters:

  • job (StalkClimber::Job)

    the job to be deleted

Returns:

  • (Boolean)

    If the given job was successfully deleted or does not exist, returns true. Otherwise returns false.

See Also:



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.

Parameters:

  • job (StalkClimber::Job)

    the job to evaluate if matches.

  • options (defaults to: {})

    [HashSymbol => Numeric, Proc, Range, Regexp, String, Symbol] Options to be used to evaluate a match.

Returns:

  • (Boolean)

    If job matches the provided options, returns true. Otherwise, returns false.

See Also:



88
89
90
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 88

def job_matches?(job, options = {})
  return matcher(MATCHABLE_JOB_ATTRIBUTES, job, options)
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.

Returns:

  • (Boolean)


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.

Parameters:

  • job (StalkClimber::Job)

    the job to print in a more readable format.

Returns:

  • (String)

    A more human-readable representation of job.

See Also:



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.

Parameters:

  • tube (StalkClimber::Tube)

    the tube to print in a more readable format.

Returns:

  • (String)

    A more human-readable representation of tube.

See Also:



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

Returns:

  • (Symbol)


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.

Parameters:

  • tube (StalkClimber::Tube)

    the tube to evaluate a match against.

  • options (defaults to: {})

    [HashSymbol => Numeric, Proc, Range, Regexp, String, Symbol] a Hash of options to use when evaluating a match.

Returns:

  • (Boolean)

    If tube matches against the provided options, returns true. Otherwise returns false.

See Also:



133
134
135
# File 'lib/bean_counter/strategies/stalk_climber_strategy.rb', line 133

def tube_matches?(tube, options = {})
  return matcher(MATCHABLE_TUBE_ATTRIBUTES, tube, options)
end