Module: UsageTrackerServer
- Defined in:
- lib/usage_tracker_server.rb,
lib/usage_tracker_server/log.rb,
lib/usage_tracker_server/runner.rb,
lib/usage_tracker_server/adapter.rb,
lib/usage_tracker_server/reactor.rb,
lib/usage_tracker_server/adapters/couchdb.rb,
lib/usage_tracker_server/adapters/mongodb.rb
Defined Under Namespace
Modules: Adapters, Reactor Classes: Adapter, Error, Log
Class Method Summary collapse
- .adapter ⇒ Object
-
.connect! ⇒ Object
Connects to the configured CouchDB and memoizes the CouchRest::Database connection into an instance variable and calls
load_views!
. - .database ⇒ Object
- .log(message = nil) ⇒ Object
- .raise(message) ⇒ Object
-
.run! ⇒ Object
Code to run inside EventMachine.
-
.settings ⇒ Object
Memoizes settings from the ./config/usage_tracker_server.yml file, relative from __FILE__ and searches for the “usage_tracker_server” configuration block.
-
.sigexit(sig) ⇒ Object
Setup signal handlers.
- .write_pid!(pid = $$) ⇒ Object
Class Method Details
.adapter ⇒ Object
48 49 50 |
# File 'lib/usage_tracker_server.rb', line 48 def adapter @adapter or raise "Not connected to the database adapter" end |
.connect! ⇒ Object
Connects to the configured CouchDB and memoizes the CouchRest::Database connection into an instance variable and calls load_views!
Raises RuntimeError if the connection could not be established
58 59 60 61 |
# File 'lib/usage_tracker_server.rb', line 58 def connect! @adapter = Adapter::new settings @database = @adapter.database end |
.database ⇒ Object
44 45 46 |
# File 'lib/usage_tracker_server.rb', line 44 def database @database or raise "Not connected to the database" end |
.log(message = nil) ⇒ Object
82 83 84 85 |
# File 'lib/usage_tracker_server.rb', line 82 def log( = nil) @log ||= Log.new ? @log.info() : @log end |
.raise(message) ⇒ Object
87 88 89 90 |
# File 'lib/usage_tracker_server.rb', line 87 def raise() log.error Kernel.raise Error, end |
.run! ⇒ Object
Code to run inside EventMachine
64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/usage_tracker_server.rb', line 64 def run! host, port = UsageTrackerServer.settings.host, UsageTrackerServer.settings.port unless (1024..65535).include? port.to_i raise "Please set a listening port between 1024 and 65535" end EventMachine.open_datagram_socket host, port, Reactor log "Listening on #{host}:#{port} UDP" write_pid! end |
.settings ⇒ Object
Memoizes settings from the ./config/usage_tracker_server.yml file, relative from __FILE__ and searches for the “usage_tracker_server” configuration block. Raises RuntimeError if it cannot find the configuration.
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/usage_tracker_server.rb', line 17 def settings @settings ||= begin rc_file = Pathname.new('.').join('config', 'usage_tracker_server.yml') settings = YAML.load(rc_file.read) if rc_file.exist? if settings == nil raise "Configuration missing." elsif settings.values_at(*%w(adapter database listen)).any?(&:nil?) raise "Incomplete configuration: please set the 'adapter', 'database' and 'listen' keys" end host, port = settings.delete('listen').split(':') if [host, port].any? {|x| x.strip.empty?} raise "Please specify where to listen as host:port" end settings['host'], settings['port'] = host, port.to_i settings['log_level'] ||= :warn log.level = settings['log_level'] OpenStruct.new settings end end |
.sigexit(sig) ⇒ Object
Setup signal handlers
* INT, TERM: graceful exit
* USR1 : rotate logs
22 23 24 25 |
# File 'lib/usage_tracker_server/runner.rb', line 22 def self.sigexit(sig) log "Received SIG#{sig}" EventMachine.stop_event_loop end |
.write_pid!(pid = $$) ⇒ Object
76 77 78 79 80 |
# File 'lib/usage_tracker_server.rb', line 76 def write_pid!(pid = $$) dir = Pathname.new('.').join('tmp', 'pids') dir = Pathname.new(Dir.tmpdir) unless dir.directory? dir.join('usage_tracker_server.pid').open('w+') {|f| f.write(pid)} end |