module BeanstalkdView::BeanstalkdUtils

Constants

GUESS_PEEK_RANGE

Public Instance Methods

add_chart_data_to_hash(tube, jobs, items) click to toggle source
# File lib/beanstalkd_view/beanstalkd_utils.rb, line 53
def add_chart_data_to_hash(tube, jobs, items)
  if jobs > 0
    datum = {}
    datum["label"] = tube.name
    datum["data"] = jobs
    items << datum
  end
end
beanstalk() click to toggle source
# File lib/beanstalkd_view/beanstalkd_utils.rb, line 9
def beanstalk
  @@beanstalk ||= Beaneater.new(beanstalk_address)
end
beanstalk_address() click to toggle source
# File lib/beanstalkd_view/beanstalkd_utils.rb, line 18
def beanstalk_address
  uris = beanstalk_url.split(/[\s,]+/)
  beanstalk_host_and_port(uris.first)
end
beanstalk_host_and_port(uri_string) click to toggle source
# File lib/beanstalkd_view/beanstalkd_utils.rb, line 23
def beanstalk_host_and_port(uri_string)
  uri = URI.parse(uri_string)
  raise(BadURL, uri_string) if uri.scheme != 'beanstalk'
  "#{uri.host}:#{uri.port || 11300}"
end
beanstalk_url() click to toggle source
# File lib/beanstalkd_view/beanstalkd_utils.rb, line 13
def beanstalk_url
  return @@url if defined?(@@url) and @@url
  ENV['BEANSTALK_URL'] || 'beanstalk://127.0.0.1/'
end
close_connection() click to toggle source
# File lib/beanstalkd_view/beanstalkd_utils.rb, line 85
def close_connection
  begin
    beanstalk.close
  rescue Beaneater::NotConnected
    # Ignore not being able to connect if trying to close
  ensure
    @@beanstalk = nil
  end
end
get_chart_data_hash(tubes) click to toggle source

Return the stats data in a format for the Bluff JS UI Charts

# File lib/beanstalkd_view/beanstalkd_utils.rb, line 39
def get_chart_data_hash(tubes)
  chart_data = {}
  chart_data["total_jobs_data"] = Hash.new
  chart_data["buried_jobs_data"] = Hash.new
  chart_data["total_jobs_data"]["items"] = Array.new
  chart_data["buried_jobs_data"]["items"] = Array.new 
  tubes.each do |tube|
    stats = tube.stats
    add_chart_data_to_hash(tube, stats[:total_jobs], chart_data["total_jobs_data"]["items"])
    add_chart_data_to_hash(tube, stats[:current_jobs_buried], chart_data["buried_jobs_data"]["items"])
  end
  chart_data
end
guess_max_peek_range(min) click to toggle source

Pick a Minimum Peek Range Based on the minimum

# File lib/beanstalkd_view/beanstalkd_utils.rb, line 81
def guess_max_peek_range(min)
  (min+GUESS_PEEK_RANGE)-1
end
guess_min_peek_range(tubes) click to toggle source

Pick a Minimum Peek Range Based on minumum ready jobs on all tubes

# File lib/beanstalkd_view/beanstalkd_utils.rb, line 63
def guess_min_peek_range(tubes)
  min = 0
  tubes.each do |tube|
    response = tube.peek('ready')
    if response
      if min == 0
        min = response.id.to_i
      else
        min = [min, response.id.to_i].min
      end
    end
  end
  # Add some jitter in the opposite direction of 1/4 range
  jitter_min = (min-(GUESS_PEEK_RANGE*0.25)).to_i
  [1, jitter_min].max
end
job_to_hash(job) click to toggle source

Convert Beaneater::Job to hash

# File lib/beanstalkd_view/beanstalkd_utils.rb, line 30
def job_to_hash(job)
  ret_value = {}
  job_stats = job.stats
  job_stats.keys.each { |key| ret_value[key] = job_stats[key] }
  ret_value['body'] = job.body.inspect
  ret_value
end