Class: BeanCounter::Strategy

Inherits:
Object
  • Object
show all
Defined in:
lib/bean_counter/strategy.rb

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

Instance Method Summary collapse

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.

Examples:

BeanCounter::Strategy.known_strategy?(Object)
  #=> false

BeanCounter::Strategy.known_strategy?(BeanCounter::Strategy)
  #=> true

Parameters:

  • strategy_identifier (Object)

    A class or any Object that implements :to_sym

Returns:

  • (Boolean)

    Returns true if strategy_identifier is a known strategy or maps to a known strategy. Otherwise, false is returned.



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.

Examples:

BeanCounter::Strategy.materialize_strategy(:'BeanCounter::Strategy::StalkClimberStrategy')
  #=> BeanCounter::Strategy::StalkClimberStrategy

Parameters:

  • strategy_identifier (Object)

    A class or any Object that implements :to_sym

Returns:



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

.strategiesArray<BeanCounter::Strategy>

Returns a list of known classes that inherit from BeanCounter::Strategy. Typically this list represents the strategies available for interacting with beanstalkd.

Returns:



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>

This method is abstract.

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.

Examples:

new_jobs = strategy.collect_new_jobs do
  ...
end
  #=> [job_enqueued_during_block, job_enqueued_during_block]

Yields:

  • Nothing yielded to provided block

Yield Returns:

  • (void)

    No specific value is expected from provided block

Returns:

  • (Array<Strategy::Job>)

    Returns an Array of Jobs as implemented by the Strategy.

Raises:

  • (NotImplementedError)


114
115
116
# File 'lib/bean_counter/strategy.rb', line 114

def collect_new_jobs
  raise NotImplementedError
end

#delete_jobBoolean

This method is abstract.

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.

Examples:

strategy.delete_job(job)
  #=> true

Parameters:

  • job (Strategy::Job)

    The job to be deleted.

Returns:

  • (Boolean)

    True if the job was deleted successfully or no longer exists, false if the job could not be deleted.

Raises:

  • (NotImplementedError)


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

def delete_job
  raise NotImplementedError
end

#job_matches?Boolean

This method is abstract.

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.

Examples:

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

Parameters:

  • job (Strategy::Job)

    The job to evaluate for a match.

  • options (Hash{Symbol, String => Numeric, Proc, Range, Regexp, String, Symbol})

    Options used to evaluate match.

Returns:

  • (Boolean)

    True if every value of the options Hash evaluates to true when comared to the attribute of job identified by the corresponding key. False is returned if any of the comparisons evaluates to false.

Raises:

  • (NotImplementedError)


175
176
177
# File 'lib/bean_counter/strategy.rb', line 175

def job_matches?
  raise NotImplementedError
end

#jobsEnumerator<Strategy::Job>

This method is abstract.

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.

Returns:

  • (Enumerator<Strategy::Job>)

    An Enumerator of all jobs in the Beanstalkd pool

Raises:

  • (NotImplementedError)


188
189
190
# File 'lib/bean_counter/strategy.rb', line 188

def jobs
  raise NotImplementedError
end

#pretty_print_jobString

This method is abstract.

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.

Parameters:

  • job (Strategy::Job)

    The job to represent in a pretty, human-readable format

Returns:

  • (String)

    job in a more human-readable format

Raises:

  • (NotImplementedError)


202
203
204
# File 'lib/bean_counter/strategy.rb', line 202

def pretty_print_job
  raise NotImplementedError
end

#pretty_print_tubeString

This method is abstract.

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.

Parameters:

  • tube (Strategy::Tube)

    The tube to represent in a pretty, human-readable format

Returns:

  • (String)

    tube in a more human-readable format.

Raises:

  • (NotImplementedError)


216
217
218
# File 'lib/bean_counter/strategy.rb', line 216

def pretty_print_tube
  raise NotImplementedError
end

#tube_matches?Boolean

This method is abstract.

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.

Examples:

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

Parameters:

  • tube (Strategy::Tube)

    The tube to evaluate for a match.

  • options (Hash{Symbol, String => Numeric, Proc, Range, Regexp, String, Symbol})

    Options used to evaluate match.

Returns:

  • (Boolean)

    True if every value of the options Hash evaluates to true when comared to the attribute of tube identified by the corresponding key. False is returned if any of the comparisons evaluates to false.

Raises:

  • (NotImplementedError)


257
258
259
# File 'lib/bean_counter/strategy.rb', line 257

def tube_matches?
  raise NotImplementedError
end

#tubesEnumerator<Strategy::Tube>

This method is abstract.

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.

Returns:

  • (Enumerator<Strategy::Tube>)

    An Enumerator of all tubes in the Beanstalkd pool

Raises:

  • (NotImplementedError)


270
271
272
# File 'lib/bean_counter/strategy.rb', line 270

def tubes
  raise NotImplementedError
end