Class: BeanCounter::Strategy
- Inherits:
-
Object
- Object
- BeanCounter::Strategy
- Defined in:
- lib/bean_counter/strategy.rb
Direct Known Subclasses
Defined Under Namespace
Classes: 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
Maintain an index of classes known to subclass Strategy.
-
.known_strategy?(strategy_identifier) ⇒ Boolean
:call-seq: known_strategy?(strategy_identifier) => Boolean.
-
.materialize_strategy(strategy_identifier) ⇒ Object
:call-seq: materialize_strategy(strategy_identifier) => subclass of Strategy.
-
.strategies ⇒ Object
:call-seq: strategies() => Hash.
Instance Method Summary collapse
-
#collect_new_jobs ⇒ Object
:call-seq: collect_new_jobs { block } => Array[Strategy Job].
-
#delete_job ⇒ Object
:call-seq: delete_job(job) => Boolean.
-
#job_matches? ⇒ Boolean
:call-seq: job_matches?(job, options => => Numeric,Proc,Range,Regexp,String) => Boolean.
-
#jobs ⇒ Object
:call-seq: jobs() => Enumerator.
-
#pretty_print_job ⇒ Object
:call-seq: pretty_print_job(job) => String.
-
#pretty_print_tube ⇒ Object
:call-seq: pretty_print_tube(tube) => String.
-
#tube_matches? ⇒ Boolean
:call-seq: tube_matches?(tube, options => => Numeric,Proc,Range,Regexp,String) => Boolean.
-
#tubes ⇒ Object
:call-seq: tubes() => Enumerator.
Class Method Details
.inherited(subclass) ⇒ Object
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
:call-seq:
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 responds to :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.
Returns true if strategy_identifier isa known strategy or maps to a known strategy. Otherwise, false is returned.
BeanCounter::Strategy.known_strategy?(Object)
#=> false
BeanCounter::Strategy.known_strategy?(BeanCounter::Strategy)
#=> true
53 54 55 56 57 58 59 |
# File 'lib/bean_counter/strategy.rb', line 53 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) ⇒ Object
:call-seq:
materialize_strategy(strategy_identifier) => subclass of 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.
BeanCounter::Strategy.materialize_strategy(:'BeanCounter::Strategy::StalkClimberStrategy')
#=> BeanCounter::Strategy::StalkClimberStrategy
74 75 76 77 78 79 80 81 82 |
# File 'lib/bean_counter/strategy.rb', line 74 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 ⇒ Object
:call-seq:
strategies() => Hash
Returns a list of known classes that inherit from BeanCounter::Strategy. Typically this list represents the strategies available for interacting with beanstalkd.
91 92 93 |
# File 'lib/bean_counter/strategy.rb', line 91 def self.strategies return @@strategies.dup end |
Instance Method Details
#collect_new_jobs ⇒ Object
:call-seq:
collect_new_jobs { block } => Array[Strategy Job]
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.
new_jobs = strategy.collect_new_jobs do
...
end
#=> [job_enqueued_during_block, job_enqueued_during_block]
110 111 112 |
# File 'lib/bean_counter/strategy.rb', line 110 def collect_new_jobs raise NotImplementedError end |
#delete_job ⇒ Object
:call-seq:
delete_job(job) => Boolean
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.
strategy.delete_job(job)
#=> true
126 127 128 |
# File 'lib/bean_counter/strategy.rb', line 126 def delete_job raise NotImplementedError end |
#job_matches? ⇒ Boolean
:call-seq:
job_matches?(job, => {Symbol,String => Numeric,Proc,Range,Regexp,String}) => Boolean
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.
strategy.job_matches?(reserved_job, :state => 'reserved')
#=> true
strategy.job_matches(small_job, :body => lambda {|body| body.length > 50 })
#=> false
strategy.job_matches(unreliable_job, :buries => 6..100)
#=> true
162 163 164 |
# File 'lib/bean_counter/strategy.rb', line 162 def job_matches? raise NotImplementedError end |
#jobs ⇒ Object
:call-seq:
jobs() => Enumerator
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.
174 175 176 |
# File 'lib/bean_counter/strategy.rb', line 174 def jobs raise NotImplementedError end |
#pretty_print_job ⇒ Object
:call-seq:
pretty_print_job(job) => String
Returns a String representation of job in a pretty, human readable format.
Used internally to print a job when an assertion fails.
186 187 188 |
# File 'lib/bean_counter/strategy.rb', line 186 def pretty_print_job raise NotImplementedError end |
#pretty_print_tube ⇒ Object
:call-seq:
pretty_print_tube(tube) => String
Returns a String representation of the tube in a pretty, human readable format. Used internally to print a tube when an assertion fails.
Used internally to print a tube when an assertion fails.
198 199 200 |
# File 'lib/bean_counter/strategy.rb', line 198 def pretty_print_tube raise NotImplementedError end |
#tube_matches? ⇒ Boolean
:call-seq:
tube_matches?(tube, => {Symbol,String => Numeric,Proc,Range,Regexp,String}) => Boolean
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.
strategy.tube_matches?(paused_tube, :state => 'paused')
#=> true
strategy.tube_matches(test_tube, :name => /test/)
#=> true
strategy.tube_matches(backed_up_tube, 'current-jobs-ready' => 50..100)
#=> true
234 235 236 |
# File 'lib/bean_counter/strategy.rb', line 234 def tube_matches? raise NotImplementedError end |
#tubes ⇒ Object
:call-seq:
tubes() => Enumerator
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.
246 247 248 |
# File 'lib/bean_counter/strategy.rb', line 246 def tubes raise NotImplementedError end |