counter_server 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,89 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Script for running the counter server.
4
+ #
5
+ # Inspiration and code borrowed from
6
+ # https://github.com/quasor/statsd
7
+ # and
8
+ # https://github.com/fetep/ruby-statsd
9
+
10
+ require "rubygems"
11
+ require "ostruct"
12
+ require "optparse"
13
+ require "counter_server"
14
+
15
+ options = OpenStruct.new
16
+ options.bind_host = '0.0.0.0'
17
+ options.bind_port = 8126
18
+ options.flush_interval = 10
19
+
20
+ redis_options = OpenStruct.new
21
+ redis_options.host = '127.0.0.1'
22
+ redis_options.port = 6379
23
+ redis_options.db = 0
24
+
25
+ opts = OptionParser.new do |opts|
26
+ opts.on("-b", "--bind=host:port", String,
27
+ "host:port to bind to for UDP listener, default 0.0.0.0:8126") do |x|
28
+ host, port = x.split(":", 2)
29
+ if ! port # just given a port
30
+ port = host
31
+ host = "0.0.0.0"
32
+ end
33
+ options.bind_host = host
34
+ options.bind_port = port.to_i
35
+ end
36
+
37
+ opts.on("-r", "--redis=host:port", String,
38
+ "host:port of redis server, default 127.0.0.1:6379") do |x|
39
+ host, port = x.split(":", 2)
40
+ redis_options.host = host
41
+ redis_options.port = port.to_i
42
+ end
43
+
44
+ opts.on("--redis_db=int", String,
45
+ "ID of Redis database (usually 1-16), default 0") do |x|
46
+ redis_options.db = x.to_i
47
+ end
48
+
49
+ opts.on("-i", "--interval=seconds", Integer,
50
+ "Flush interval in seconds, default 10") do |x|
51
+ options.flush_interval = x
52
+ end
53
+ end
54
+
55
+ begin
56
+ opts.parse!
57
+ if options.bind_port <= 0 or options.bind_port > 2**32
58
+ raise "bind port #{options.bind_port} out of range"
59
+ end
60
+ if redis_options.port <= 0 or redis_options.port > 2**32
61
+ raise "redis port #{redis_options.port} out of range"
62
+ end
63
+ rescue
64
+ $stderr.puts "#{File.basename($0)}: #{$!}"
65
+ $stderr.puts opts
66
+ exit(1)
67
+ end
68
+
69
+ StatsD.initialize_redis_backend(redis_options)
70
+
71
+ EM.run do
72
+ begin
73
+ EM.add_periodic_timer(options.flush_interval) do
74
+ EM.defer do
75
+ begin
76
+ StatsD.flush
77
+ rescue
78
+ StatsD.logger.error("trouble flushing: #{$!}")
79
+ end
80
+ end
81
+ end
82
+
83
+ EM.open_datagram_socket(options.bind_host, options.bind_port, StatsD)
84
+ rescue
85
+ StatsD.logger.error "Exception inside of EM.run: #{$!}"
86
+ EM.stop_event_loop
87
+ exit 1
88
+ end
89
+ end
@@ -1,3 +1,3 @@
1
1
  module CounterServer
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: counter_server
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - Jason Katz-Brown
@@ -48,8 +48,8 @@ dependencies:
48
48
  description: Count things, saves in Redis
49
49
  email:
50
50
  - jasonkb@airbnb.com
51
- executables: []
52
-
51
+ executables:
52
+ - counter_server
53
53
  extensions: []
54
54
 
55
55
  extra_rdoc_files: []
@@ -58,6 +58,7 @@ files:
58
58
  - .gitignore
59
59
  - Gemfile
60
60
  - Rakefile
61
+ - bin/counter_server
61
62
  - counter_server.gemspec
62
63
  - lib/counter_server.rb
63
64
  - lib/counter_server/version.rb