From 3a09bd392391a32d5b2b9138337c530300572536 Mon Sep 17 00:00:00 2001 From: Benjamin ter Kuile Date: Tue, 30 Sep 2014 19:18:35 +0200 Subject: [PATCH] Self regulating drb counter, intended to become stable together with monit --- bin/drb_counter.rb | 77 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 73 insertions(+), 4 deletions(-) diff --git a/bin/drb_counter.rb b/bin/drb_counter.rb index d3e1d362..ae2b38b2 100755 --- a/bin/drb_counter.rb +++ b/bin/drb_counter.rb @@ -1,13 +1,82 @@ #!/usr/bin/env ruby # Make drb server require 'rubygems' -require File.expand_path('../lib/in_memory_q_counter', File.dirname(__FILE__)) +#require File.expand_path('../lib/in_memory_q_counter', File.dirname(__FILE__)) +# This is a non thread safe replacement for the +# couchbase counter mechanism since every test needs +# a clean start and Hash#clear is soooo much faster than +# a couchbase bucket flush +class InMemoryQCounter + attr_reader :store, :environment + + def initialize(reload_stats: false, environment: nil) + @store = {} + @environment = environment || 'development' + reload_stats! if reload_stats + end + + def get(key, options = {}) + store[key] + end + + def set(key, value) + store[key] = value + end + + def incr(key, options = {}) + # store[key] ||= options[:initial].to_i + # store[key] += 1 + if store[key] + store[key] += 1 + else + store[key] = options[:initial].to_i + end + end + + def decr(key, options = {}) + # store[key] ||= options[:initial].to_i + # store[key] -= 1 + if store[key] + store[key] -= 1 + else + store[key] = options[:initial].to_i + end + end + + def flush + store.clear + end + + def reload_stats! + require 'yaml' + require 'couchrest' + couch_settings_path = File.join(ENV['MOZO_PATH'], 'config/couchdb.yml') + puts "Couch settings path: #{couch_settings_path}" + puts "Environment: #{environment.inspect}" + couch_settings = YAML.load_file(couch_settings_path)[environment] + database = couch_settings['database'] + db = CouchRest.database(database) + view_result = db.view("_design/order/_view/by_supplier_id_and_state", reduce: true, group_level: 2) + counts = view_result ? view_result['rows'] : nil + if counts + counts.each do |count_spec| + supplier_id, order_state = count_spec['key'] + order_count = count_spec['value'] + case order_state + when "placed" then set("supplier_counter:#{supplier_id}:orders_placed", order_count) + when "active" then set("supplier_counter:#{supplier_id}:orders_in_process", order_count) + end + end + end + rescue => e + raise e + end +end require 'drb' require 'daemons' drb_port = 9022 -puts "Counter server running at port #{drb_port}" Daemons.run_proc('DRBcounter', dir_mode: :normal, dir: File.expand_path("#{File.dirname(__FILE__)}/../tmp/pids")) do - DRb.start_service "druby://:#{drb_port}", InMemoryQCounter.new - # trap("INT") { DRb.stop_service } + environment = (%w[production staging development test] & ARGV).first + DRb.start_service "druby://:#{drb_port}", InMemoryQCounter.new(reload_stats: true, environment: environment) DRb.thread.join end