class Blamescope::Server
Constants
- TABLE_QUERY
Public Class Methods
start(options = {})
click to toggle source
# File lib/blamescope/server.rb, line 20 def self.start(options = {}) # Parse options optparse = OptionParser.new do |opts| opts.banner = "Usage: blamescope [options]" opts.on("-d", "--database CONF", "Database connection in format postgres://user:password@host/database") do |d| options[:database] = d end opts.on("-r", "--rabbit CONF", "RabbitMQ connection in format amqp://user:pass@host:port/vhost") do |r| options[:rabbit] = r end opts.on("-v", "--[no-]verbose", "Run verbosely") do |v| options[:verbose] = v end end optparse.parse! if options[:database] && options[:rabbit] new.listen(options) else puts optparse end end
Public Instance Methods
listen(config)
click to toggle source
# File lib/blamescope/server.rb, line 43 def listen(config) rabbit = rabbit_connection(config[:rabbit]) db = postgres_connection(config[:database]) rabbit[:queue].subscribe(block: true) do |delivery_info, properties, body| store_event(db, body) end rescue Interrupt => _ rabbit[:channel].close rabbit[:conn].close end
postgres_connection(config)
click to toggle source
# File lib/blamescope/server.rb, line 67 def postgres_connection(config) config = URI.parse(config) params = {host: config.host, dbname: config.path[1..-1]} params[:user] = config.user if config.user params[:password] = config.password if config.password params[:port] = config.port if config.port conn = PG.connect(params) conn.exec(TABLE_QUERY) conn.prepare("insert_event", "INSERT INTO blamescope (email, action, model, attrs, created_at) VALUES ($1, $2, $3, $4, $5)") return conn end
rabbit_connection(config)
click to toggle source
# File lib/blamescope/server.rb, line 59 def rabbit_connection(config) rabbit = Blamescope.rabbit_connection(config) queue = rabbit[:channel].queue("", exclusive: true) queue.bind(rabbit[:exchange]) rabbit[:queue] = queue return rabbit end
store_event(db, body, time = Time.now.utc)
click to toggle source
# File lib/blamescope/server.rb, line 54 def store_event(db, body, time = Time.now.utc) event = JSON.parse(body) db.exec_prepared('insert_event', [event['email'], event['action'], event['model'], event['attrs'].to_json, time]) end