dockerize faye and counters

This commit is contained in:
2015-02-10 15:44:46 +01:00
parent cab883b3ce
commit 461bbdb3ec
15 changed files with 388 additions and 26 deletions
+28
View File
@@ -0,0 +1,28 @@
FROM ruby:2.2.0
# throw errors if Gemfile has been modified since Gemfile.lock
RUN bundle config --global frozen 1
RUN mkdir -p /usr/src/app
WORKDIR /usr/src/app
# Test
#RUN apt-get update && apt-get install -y qt5-default libqt5webkit5-dev --no-install-recommends && rm -rf /var/lib/apt/lists/*
# Standard
#RUN apt-get update && apt-get install -y nodejs --no-install-recommends && rm -rf /var/lib/apt/lists/*
#RUN apt-get update && apt-get install -y mysql-client postgresql-client sqlite3 --no-install-recommends && rm -rf /var/lib/apt/lists/*
# Specific
#RUN apt-get update && apt-get install -y dos2unix --no-install-recommends && rm -rf /var/lib/apt/lists/*
COPY Gemfile /usr/src/app/
COPY Gemfile.lock /usr/src/app/
#COPY vendor/cache /usr/src/app/vendor/cache
#RUN bundle install --local
RUN rm -rf /usr/src/app/.bundle
RUN bundle install
COPY . /usr/src/app
EXPOSE 9022
CMD ["ruby", "drb_counter.rb"]
+4
View File
@@ -0,0 +1,4 @@
source 'https://rubygems.org'
gem "couchrest"
gem "pry"
+196
View File
@@ -0,0 +1,196 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.0)
couchrest (1.2.0)
mime-types (~> 1.15)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
multi_json (~> 1.0)
rest-client (~> 1.6.1)
method_source (0.8.2)
mime-types (1.25.1)
multi_json (1.10.1)
pry (0.10.1)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rdoc (4.2.0)
rest-client (1.6.8)
mime-types (~> 1.16)
rdoc (>= 2.4.2)
slop (3.6.0)
PLATFORMS
ruby
DEPENDENCIES
couchrest
pry
+8
View File
@@ -0,0 +1,8 @@
views:
by_supplier_id_and_state:
map: |
function(doc) {
if(doc.ruby_class && doc.ruby_class == 'Order') emit([doc['supplier_id'], doc['state']], 1);
}
reduce: _sum
language: "javascript"
+110
View File
@@ -0,0 +1,110 @@
#!/usr/bin/env ruby
# Make drb server
require 'rubygems'
require 'drb'
require 'pry'
#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 = {})
value = store[key]
debug "Get key #{key} (#{value})"
value
end
def set(key, value)
debug "Set key #{key} to #{value}"
store[key] = value
end
def incr(key, options = {})
debug "Increment key #{key}"
# 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 = {})
debug "Decrement key #{key}"
# 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
debug "Flushing store"
store.clear
end
def debug(message)
puts message
end
def reload_stats!
require 'yaml'
require 'couchrest'
require 'pry'
couch_settings_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)
design_doc = "_design/order_counter"
view_path = File.join design_doc, "_view/by_supplier_id_and_state"
tries = 0
begin
view_result = db.view(view_path, reduce: true, group: true, group_level: 2)
rescue RestClient::ResourceNotFound => e
puts "Database view #{view_path} not found"
design_doc_path = File.expand_path('../couchdb_design.yml', __FILE__)
doc = YAML.load_file design_doc_path
doc['_id'] = design_doc
db.save_doc(doc)
if tries < 3
tries += 1
retry
else
raise e
end
# view not available, initialize as zero
end
counts = view_result ? view_result['rows'] : nil
puts "Initialize with: #{counts.to_yaml}"
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
drb_port = 9022
environment = (%w[production staging development test] & ARGV).first || 'development'
DRb.start_service "druby://:#{drb_port}", InMemoryQCounter.new(reload_stats: true, environment: environment)
DRb.thread.join