Class: BeanCounter::Strategy
- Inherits:
-
Object
- Object
- BeanCounter::Strategy
- Defined in:
- lib/bean_counter/strategy.rb
Direct Known Subclasses
Defined Under Namespace
Classes: GemeraldBeanstalkStrategy, StalkClimberStrategy
Constant Summary collapse
- MATCHABLE_JOB_ATTRIBUTES =
Available job attributes. These attributes can be used by strategies to validate what attributes are used for matching jobs.
begin attrs = [ :age, :body, :buries, :connection, :delay, :id, :kicks, :pri, :releases, :reserves, :state, :'time-left', :timeouts, :ttr, :tube, ] attrs.concat(attrs.map(&:to_s)) end
- MATCHABLE_TUBE_ATTRIBUTES =
Available tube attributes. These attributes can be used by strategies to validate what attributes are used for matching tubes.
begin attrs = [ :'cmd-delete', :'cmd-pause-tube', :'current-jobs-buried', :'current-jobs-delayed', :'current-jobs-ready', :'current-jobs-reserved', :'current-jobs-urgent', :'current-using', :'current-waiting', :'current-watching', :name, :pause, :'pause-time-left', :'total-jobs', ] attrs.concat(attrs.map(&:to_s)) end
- @@strategies =
{}
Class Method Summary collapse
-
.inherited(subclass) ⇒ Object
Used to maintain an index of classes known to subclass Strategy.
-
.known_strategy?(strategy_identifier) ⇒ Boolean
Determines if the provided
strategy_identifer
corresponds to a known subclass of strategy. -
.materialize_strategy(strategy_identifier) ⇒ BeanCounter::Strategy
Materialize the provided
strategy_identifier
into a subclass of Strategy. -
.strategies ⇒ Array<BeanCounter::Strategy>
Returns a list of known classes that inherit from BeanCounter::Strategy.
Instance Method Summary collapse
-
#collect_new_jobs { ... } ⇒ Array<Strategy::Job>
abstract
Provide a means of collecting jobs enqueued during the execution of the provided
block
. -
#delete_job ⇒ Boolean
abstract
Provide a means for deleting a job specific to the job interface used by the strategy.
-
#job_matches? ⇒ Boolean
abstract
Returns a boolean indicating whether or not the provided
job
matches the given Hash ofoptions
. -
#jobs ⇒ Enumerator<Strategy::Job>
abstract
Returns an Enumerator providing a means to enumerate all jobs in the Beanstalkd pool.
-
#pretty_print_job ⇒ String
abstract
Returns a String representation of
job
in a pretty, human-readable format. -
#pretty_print_tube ⇒ String
abstract
Returns a String representation of the tube in a pretty, human-readable format.
-
#tube_matches? ⇒ Boolean
abstract
Returns a boolean indicating whether or not the provided
tube
matches the given Hash ofoptions
. -
#tubes ⇒ Enumerator<Strategy::Tube>
abstract
Returns an Enumerator providing a means to enumerate all tubes in the beanstalkd pool.
Class Method Details
.inherited(subclass) ⇒ Object
Used to maintain an index of classes known to subclass Strategy.
29 30 31 32 |
# File 'lib/bean_counter/strategy.rb', line 29 def self.inherited(subclass) identifier = subclass.name || subclass.to_s[8, subclass.to_s.length - 9] @@strategies[identifier.to_sym] = subclass end |
.known_strategy?(strategy_identifier) ⇒ Boolean
Determines if the provided strategy_identifer
corresponds to a known
subclass of strategy. The provided strategy_identifer
can be a class
or any object that implements :to_sym. Classes are compared directly.
For non-class objects that respond to :to_sym, the symbolized form
of strategy_identifer
is used as a key to attempt to retrieve a strategy
from the strategies Hash.
52 53 54 55 56 57 58 |
# File 'lib/bean_counter/strategy.rb', line 52 def self.known_strategy?(strategy_identifier) return true if strategy_identifier.is_a?(Class) && strategy_identifier <= BeanCounter::Strategy return true if strategy_identifier.respond_to?(:to_sym) && strategies.key?(strategy_identifier.to_sym) return false end |
.materialize_strategy(strategy_identifier) ⇒ BeanCounter::Strategy
Materialize the provided strategy_identifier
into a subclass of Strategy.
If strategy_identifer
is already a subclass of Strategy,
strategy_identifier
is returned. Otherwise, strategy_identifer
is
converted into a Symbol and is used as a key to retrieve a strategy from
the known subclasses of Strategy. If strategy_identifier
does not map to
a known subclass of Strategy, an ArgumentError is raised.
75 76 77 78 79 80 81 82 83 |
# File 'lib/bean_counter/strategy.rb', line 75 def self.materialize_strategy(strategy_identifier) unless BeanCounter::Strategy.known_strategy?(strategy_identifier) raise( ArgumentError, "Could not find #{strategy_identifier} among known strategies: #{strategies.keys.to_s}" ) end return strategy_identifier.is_a?(Class) ? strategy_identifier : strategies[strategy_identifier.to_sym] end |
.strategies ⇒ Array<BeanCounter::Strategy>
Returns a list of known classes that inherit from BeanCounter::Strategy. Typically this list represents the strategies available for interacting with beanstalkd.
92 93 94 |
# File 'lib/bean_counter/strategy.rb', line 92 def self.strategies return @@strategies.dup end |
Instance Method Details
#collect_new_jobs { ... } ⇒ Array<Strategy::Job>
Subclasses must override to implement
Provide a means of collecting jobs enqueued during the execution of the
provided block
. Returns an Array of Jobs as implemented by the Strategy.
Used internally to reduce the set of jobs that must be examined to evaluate
the truth of an assertion to only those enqueued during the evaluation of
the given block
.
114 115 116 |
# File 'lib/bean_counter/strategy.rb', line 114 def collect_new_jobs raise NotImplementedError end |
#delete_job ⇒ Boolean
Subclasses must override to implement
Provide a means for deleting a job specific to the job interface used by the strategy. Should return true if the job was deleted successfully or no longer exists and false if the job could not be deleted.
Used internally to delete a job, allowing the beanstalkd pool to be reset.
133 134 135 |
# File 'lib/bean_counter/strategy.rb', line 133 def delete_job raise NotImplementedError end |
#job_matches? ⇒ Boolean
Subclasses must override to implement
Returns a boolean indicating whether or not the provided job
matches the
given Hash of options
. Each key
in options
is a String or a Symbol that
identifies an attribute of job
that the corresponding value
should be
compared against. True is returned if every value
in options
evaluates to
true when compared to the attribute of job
identified by the corresponding
key
. False is returned if any of the comparisons evaluates to false.
If no options are given, returns true for any job that exists at the time of evaluation.
Each attribute comparison is performed using the triple-equal (===)
operator/method of value
with the attribute of job
identified by key
passed into the method. Use of === allows for more complex comparisons
using Procs, Ranges, Regexps, etc.
Consult MATCHABLE_JOB_ATTRIBUTES for a list of which attributes of job
can be matched against.
Used internally to evaluate if a job matches an assertion.
175 176 177 |
# File 'lib/bean_counter/strategy.rb', line 175 def job_matches? raise NotImplementedError end |
#jobs ⇒ Enumerator<Strategy::Job>
Subclasses must override to implement
Returns an Enumerator providing a means to enumerate all jobs in the Beanstalkd pool.
Used internally to enumerate all jobs to find jobs matching an assertion.
188 189 190 |
# File 'lib/bean_counter/strategy.rb', line 188 def jobs raise NotImplementedError end |
#pretty_print_job ⇒ String
Subclasses must override to implement
Returns a String representation of job
in a pretty, human-readable
format.
Used internally to print a job when an assertion fails.
202 203 204 |
# File 'lib/bean_counter/strategy.rb', line 202 def pretty_print_job raise NotImplementedError end |
#pretty_print_tube ⇒ String
Subclasses must override to implement
Returns a String representation of the tube in a pretty, human-readable format.
Used internally to print a tube when an assertion fails.
216 217 218 |
# File 'lib/bean_counter/strategy.rb', line 216 def pretty_print_tube raise NotImplementedError end |
#tube_matches? ⇒ Boolean
Subclasses must override to implement
Returns a boolean indicating whether or not the provided tube
matches the
given Hash of options
. Each key
in options
is a String or a Symbol that
identifies an attribute of tube
that the corresponding value
should be
compared against. True is returned if every value
in options
evaluates to
true when compared to the attribute of tube
identified by the corresponding
key
. False is returned if any of the comparisons evaluates to false.
If no options are given, returns true for any tube that exists at the time of evaluation.
Each attribute comparison is performed using the triple-equal (===)
operator/method of value
with the attribute of job
identified by key
passed into the method. Use of === allows for more complex comparisons using
Procs, Ranges, Regexps, etc.
Consult MATCHABLE_TUBE_ATTRIBUTES for a list of which attributes of tube
can be matched against.
Used internally to evaluate if a tube matches an assertion.
257 258 259 |
# File 'lib/bean_counter/strategy.rb', line 257 def tube_matches? raise NotImplementedError end |
#tubes ⇒ Enumerator<Strategy::Tube>
Subclasses must override to implement
Returns an Enumerator providing a means to enumerate all tubes in the beanstalkd pool.
Used internally to enumerate all tubes to find tubes matching an assertion.
270 271 272 |
# File 'lib/bean_counter/strategy.rb', line 270 def tubes raise NotImplementedError end |