Module: BeanCounter

Defined in:
lib/bean_counter/core.rb,
lib/bean_counter/version.rb

Defined Under Namespace

Modules: SpecMatchers, TestAssertions Classes: EnqueuedExpectation, Strategy, TubeExpectation

Constant Summary collapse

DEFAULT_STRATEGY =

Use StalkClimberStrategy by default because it works with standard Beanstalkd

:'BeanCounter::Strategy::StalkClimberStrategy'
VERSION =

BeanCounter version number

'0.1.0'

Class Method Summary collapse

Class Method Details

.beanstalkd_urlArray<String>

Returns an Array of parsed Beanstalkd URLs. Server URLs provided via the environment variable BEANSTALKD_URL are given precedence. When setting beanstalkd_url from the environment, urls are expected in a comma separated list. If ENV['BEANSTALKD_URL'] is not set, the BeanCounter.beanstalkd_url instance variable is checked and parsed next. Finally, if the BeanCounter.beanstalkd_url instance variable has not been set, the configuration for Beaneater is checked and parsed. If no beanstalkd_url can be determined a RuntimeError is raised. Beanstalkd URLs can be provided in any of three supported formats shown in in the examples below.

In short, a host is the only required component. If no port is provided, the default beanstalkd port of 11300 is assumed. If a URI scheme other than beanstalk is provided the strategy in use will likely raise an error.

Examples:

Valid beanstalkd_url formats

# host only:
'localhost'

# host and port:
'192.168.1.100:11300'

# host and port prefixed by beanstalk URI scheme:
'beanstalk://127.0.0.1:11300'

BeanCounter.beanstalkd_url provided via ENV['BEANSTALKD_URL']

# $ BEANSTALKD_URL='127.0.0.1,beanstalk://localhost:11300,localhost:11301' rake test

BeanCounter.beanstalkd_url
  #=> ['127.0.0.1', 'beanstalk://localhost:11300', 'localhost:11301']

BeanCounter.beanstalkd_url set explicitly

BeanCounter.beanstalkd_url = 'beanstalk://localhost'
BeanCounter.beanstalkd_url
  #=> 'beanstalk://localhost'

BeanCounter.beanstalkd_url provided by Beaneater

Beaneater.configure do |config|
  config.beanstalkd_url = ['localhost', 'localhost:11301']
end

BeanCounter.beanstalkd_url
  #=> ['localhost', 'localhost:11301']

Returns:

  • (Array<String>)

    An Array of Beanstalkd URLs.



58
59
60
61
62
63
64
# File 'lib/bean_counter/core.rb', line 58

def self.beanstalkd_url
  @hosts_from_env = ENV['BEANSTALKD_URL']
  @hosts_from_env = @hosts_from_env.split(',').map!(&:strip) unless @hosts_from_env.nil?
  beanstalkd_url = @hosts_from_env || @beanstalkd_url || Beaneater.configuration.beanstalkd_url
  raise 'Could not determine beanstalkd url' if beanstalkd_url.to_s == ''
  return beanstalkd_url.is_a?(Array) ? beanstalkd_url : [beanstalkd_url]
end

.beanstalkd_url=(value) ⇒ Object

Sets the Beanstalkd server URLs to be used by BeanCounter.

Parameters:

  • value (String, Array<String>)

    The new value to to assign to beanstalkd_urls

Returns:

  • (Object)

    Returns the value given



10
11
12
# File 'lib/bean_counter/core.rb', line 10

def self.beanstalkd_url=(value)
  @beanstalkd_url = value
end

.default_strategyBeanCounter::Strategy

Returns a previously materialized strategy or materializes a new default strategy for use.

See BeanCounter::Strategy.materialize_strategy for more information on the materialization process.

Returns:

See Also:



75
76
77
# File 'lib/bean_counter/core.rb', line 75

def self.default_strategy
  return @default_strategy ||= BeanCounter::Strategy.materialize_strategy(DEFAULT_STRATEGY)
end

.reset!(tube_name = nil) ⇒ Boolean

Uses strategy to delete all jobs from the beanstalkd pool or from the tube specified by tube_name.

It should be noted that jobs that are reserved can only be deleted by the reserving connection and thus cannot be deleted via this method. As such, care may need to be taken to ensure that jobs are not left in a reserved state.

Parameters:

  • tube_name (String) (defaults to: nil)

    a particular tube to clear all jobs from. If not given, all jobs in the Beanstalkd pool will be} deleted.

Returns:

  • (Boolean)

    Returns true if all encountered jobs were deleted successfully. Returns false if any of the jobs enumerated could not be deleted.



92
93
94
95
96
97
98
99
# File 'lib/bean_counter/core.rb', line 92

def self.reset!(tube_name = nil)
  partial_failure = false
  strategy.jobs.each do |job|
    success = strategy.delete_job(job) if tube_name.nil? || strategy.job_matches?(job, :tube => tube_name)
    partial_failure ||= success
  end
  return !partial_failure
end

.strategiesArray<BeanCounter::Strategy>

Returns a list of known subclasses of BeanCounter::Strategy. Typically this list represents the strategies available for interacting with Beanstalkd.

Returns:



107
108
109
# File 'lib/bean_counter/core.rb', line 107

def self.strategies
  return BeanCounter::Strategy.strategies
end

.strategyBeanCounter::Strategy

Returns a previously materialized strategy or instantiates a new instance of the default strategy. If no previous strategy exists, a new instance of the default strategy is instantiated and returned.

Returns:



137
138
139
# File 'lib/bean_counter/core.rb', line 137

def self.strategy
  return @strategy ||= default_strategy.new
end

.strategy=(strategy_identifier) ⇒ Object

Sets the strategy that BeanCounter should use when interacting with Beanstalkd. The value provided for strategy_identifier will be used to materialize an instance of the matching strategy class. If the provided strategy_identifier is nil, any existing strategy will be cleared and the next call to BeanCounter.strategy will use the default strategy.

Parameters:

  • strategy_identifier (Object)

    A class or an Object that responds to :to_sym identifying the subclass of BeanCounter::Strategy to use when communicating with the beanstalkd pool

Returns:

  • (Object)

    Returns the value given



122
123
124
125
126
127
128
# File 'lib/bean_counter/core.rb', line 122

def self.strategy=(strategy_identifier)
  if strategy_identifier.nil?
    @strategy = nil
  else
    @strategy = BeanCounter::Strategy.materialize_strategy(strategy_identifier).new
  end
end