officer 0.5.3 → 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -77,6 +77,9 @@ Run Officer in the background (production mode) and listen on a specific IP and
77
77
 
78
78
  - Retrieve the list of locks for the current connection.
79
79
  - Client: Option to abort a lock request if there is already a certain number of clients waiting for the lock.
80
+ - Client: Namespaced lock names.
81
+ - Server: configure the daemons gem to allow multiple server processes to run on one box.
82
+ - Tests
80
83
 
81
84
  ## Copyright
82
85
 
data/Rakefile CHANGED
@@ -12,7 +12,6 @@ begin
12
12
  gem.authors = ["Chad Remesch"]
13
13
  gem.add_development_dependency "eventmachine", ">= 0"
14
14
  gem.add_development_dependency "json", ">= 0"
15
- gem.add_development_dependency "activesupport", ">= 0"
16
15
  gem.add_development_dependency "daemons", ">= 0"
17
16
  gem.add_development_dependency "choice", ">= 0"
18
17
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.3
1
+ 0.6.0
data/bin/officer CHANGED
@@ -29,10 +29,16 @@ def parse_command_line
29
29
  cast Integer
30
30
  end
31
31
 
32
- option :verbose do
33
- short '-v'
34
- long '--verbose'
35
- desc 'Print lock information to stdout every five seconds (default: off)'
32
+ option :log_level do
33
+ short '-l'
34
+ long '--log-level'
35
+ desc 'Set the log level to debug, info, or error (default: error)'
36
+ end
37
+
38
+ option :stats do
39
+ short '-s'
40
+ long '--stats'
41
+ desc 'Log stats every 5 seconds (default: off, requires log level: info)'
36
42
  end
37
43
 
38
44
  option :help do
@@ -2,7 +2,6 @@ require 'socket'
2
2
  require 'fcntl'
3
3
 
4
4
  require 'rubygems'
5
- require 'active_support'
6
5
  require 'json'
7
6
 
8
7
  module Officer
@@ -15,10 +14,8 @@ module Officer
15
14
 
16
15
  class Client
17
16
  def initialize options={}
18
- options.reverse_merge! :host => 'localhost', :port => 11500
19
-
20
- @host = options[:host]
21
- @port = options[:port]
17
+ @host = options[:host] || 'localhost'
18
+ @port = options[:port] || 11500
22
19
 
23
20
  connect
24
21
  end
@@ -19,7 +19,7 @@ module Officer
19
19
  class Base
20
20
  class << self
21
21
  def register
22
- Factory.register to_s.split('::').last.underscore, self
22
+ Factory.register underscore(to_s.split('::').last), self
23
23
  end
24
24
  end
25
25
 
@@ -37,6 +37,17 @@ module Officer
37
37
  end
38
38
 
39
39
  private
40
+ class << self
41
+ # Originally from ActiveSupport
42
+ def underscore camel_cased_word
43
+ camel_cased_word.to_s.gsub(/::/, '/').
44
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
45
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
46
+ tr("-", "_").
47
+ downcase
48
+ end
49
+ end
50
+
40
51
  def setup # Override if necessary.
41
52
  end
42
53
 
@@ -5,16 +5,20 @@ module Officer
5
5
  def post_init
6
6
  @connected = true
7
7
  @timers = {} # name => Timer
8
+
9
+ Officer::Log.info "Connected: #{to_host_s}"
8
10
  end
9
11
 
10
12
  def receive_line line
11
13
  line.chomp!
12
14
 
15
+ Officer::Log.debug "#{to_host_s} received line: #{line}"
16
+
13
17
  command = Officer::Command::Factory.create line, self
14
18
  command.execute
15
19
 
16
20
  rescue Exception => e
17
- L.debug_exception e
21
+ Officer::Log.error e
18
22
  raise
19
23
  end
20
24
 
@@ -22,6 +26,8 @@ module Officer
22
26
  @connected = false
23
27
 
24
28
  Officer::LockStore.instance.reset self
29
+
30
+ Officer::Log.info "Disconnected: #{to_host_s}"
25
31
  end
26
32
  end
27
33
 
@@ -29,24 +35,24 @@ module Officer
29
35
  def acquired name
30
36
  @timers.delete(name).cancel if @timers[name]
31
37
 
32
- send_line({:result => 'acquired', :name => name}.to_json)
38
+ send_result 'acquired', :name => name
33
39
  end
34
40
 
35
41
  def already_acquired name
36
- send_line({:result => 'already_acquired', :name => name}.to_json)
42
+ send_result 'already_acquired', :name => name
37
43
  end
38
44
 
39
45
  def released name
40
- send_line({:result => 'released', :name => name}.to_json)
46
+ send_result 'released', :name => name
41
47
  end
42
48
 
43
49
  def release_failed name
44
- send_line({:result => 'release_failed', :name => name}.to_json)
50
+ send_result 'release_failed', :name => name
45
51
  end
46
52
 
47
53
  def reset_succeeded
48
54
  @timers.values.each {|timer| timer.cancel}
49
- send_line({:result => 'reset_succeeded'}.to_json)
55
+ send_result 'reset_succeeded'
50
56
  end
51
57
 
52
58
  def queued name, options={}
@@ -61,15 +67,15 @@ module Officer
61
67
 
62
68
  def timed_out name
63
69
  @timers.delete name
64
- send_line({:result => 'timed_out', :name => name}.to_json)
70
+ send_result 'timed_out', :name => name
65
71
  end
66
72
 
67
73
  def locks locks_hash
68
- send_line({:result => 'locks', :value => locks_hash}.to_json)
74
+ send_result 'locks', :value => locks_hash
69
75
  end
70
76
 
71
77
  def connections conns_hash
72
- send_line({:result => 'connections', :value => conns_hash}.to_json)
78
+ send_result 'connections', :value => conns_hash
73
79
  end
74
80
  end
75
81
 
@@ -78,16 +84,29 @@ module Officer
78
84
  include LockStoreCallbacks
79
85
 
80
86
  def to_host_s
81
- port, ip = Socket.unpack_sockaddr_in get_peername
82
- "#{ip}:#{port}"
87
+ @to_host_s ||= non_cached_to_host_s
83
88
  end
84
89
 
85
90
  private
86
91
  attr_reader :connected
87
92
 
93
+ def non_cached_to_host_s
94
+ port, ip = Socket.unpack_sockaddr_in get_peername
95
+ "#{ip}:#{port}"
96
+ end
97
+
88
98
  def send_line line
99
+ Officer::Log.debug "#{to_host_s} sent line: #{line}"
100
+
89
101
  send_data "#{line}\n" if @connected
90
102
  end
103
+
104
+ def send_result result, options={}
105
+ params = options.dup
106
+ params[:result] = result
107
+
108
+ send_line params.to_json
109
+ end
91
110
  end
92
111
 
93
112
  end
@@ -20,27 +20,29 @@ module Officer
20
20
  end
21
21
 
22
22
  def log_state
23
- L.info '-----'
23
+ l = Officer::Log
24
24
 
25
- L.info 'LOCK STORE:'
26
- L.info ''
27
-
28
- L.info "locks:"
25
+ l.info '-----'
26
+
27
+ l.info 'LOCK STORE:'
28
+ l.info ''
29
+
30
+ l.info "locks:"
29
31
  @locks.each do |name, lock|
30
- L.info "#{name}: connections=[#{lock.queue.map{|c| c.object_id}.join(', ')}]"
32
+ l.info "#{name}: connections=[#{lock.queue.map{|c| c.to_host_s}.join(', ')}]"
31
33
  end
32
- L.info ''
34
+ l.info ''
33
35
 
34
- L.info "Connections:"
36
+ l.info "Connections:"
35
37
  @connections.each do |connection, names|
36
- L.info "#{connection.object_id}: names=[#{names.to_a.join(', ')}]"
38
+ l.info "#{connection.to_host_s}: names=[#{names.to_a.join(', ')}]"
37
39
  end
38
- L.info ''
40
+ l.info ''
39
41
 
40
- L.info "Acquire Rate: #{@acquire_counter.to_f / 5}/s"
42
+ l.info "Acquire Rate: #{@acquire_counter.to_f / 5}/s"
41
43
  @acquire_counter = 0
42
44
 
43
- L.info '-----'
45
+ l.info '-----'
44
46
  end
45
47
 
46
48
  def acquire name, connection, options={}
@@ -60,7 +62,7 @@ module Officer
60
62
  end
61
63
 
62
64
  def release name, connection, options={}
63
- options.reverse_merge! :callback => true
65
+ options[:callback] ||= true
64
66
 
65
67
  lock = @locks[name]
66
68
  names = @connections[connection]
data/lib/officer/log.rb CHANGED
@@ -1,36 +1,37 @@
1
1
  module Officer
2
2
 
3
- class Log
3
+ class Log < SimpleDelegator
4
4
  include Singleton
5
5
 
6
- def debug message
7
- return unless [:debug].include?(LOG_LEVEL)
6
+ class << self
7
+ def debug msg
8
+ instance.debug msg
9
+ end
8
10
 
9
- write message
10
- end
11
+ def info msg
12
+ instance.info msg
13
+ end
11
14
 
12
- def info message
13
- return unless [:debug, :info].include?(LOG_LEVEL)
15
+ def error msg
16
+ instance.error msg
17
+ end
14
18
 
15
- write message
16
- end
19
+ def set_log_level level
20
+ level = case level
21
+ when 'debug' then Logger::DEBUG
22
+ when 'info' then Logger::INFO
23
+ else Logger::ERROR
24
+ end
17
25
 
18
- def debug_exception e
19
- debug '-----'
20
- debug "EXCEPTION: "
21
- debug e
22
- debug e.backtrace.join "\n "
23
- debug '-----'
24
- debug "ARGV: #{ARGV.inspect}"
26
+ instance.level = level
27
+ end
25
28
  end
26
29
 
27
- private
28
- def write message
29
- puts message
30
+ def initialize
31
+ @logger = Logger.new STDOUT
32
+
33
+ super @logger
30
34
  end
31
35
  end
32
-
33
- end
34
36
 
35
- L = Officer::Log.instance
36
- LOG_LEVEL = :debug
37
+ end
@@ -1,25 +1,26 @@
1
1
  module Officer
2
2
 
3
3
  class Server
4
- def initialize opts={}
5
- @port = opts[:port] || 11500
6
- @host = opts[:host] || '0.0.0.0'
7
- @verbose = opts[:verbose] || false
4
+ def initialize params={}
5
+ @params = params
6
+
7
+ params[:port] ||= 11500
8
+ params[:host] ||= '0.0.0.0'
9
+ params[:stats] ||= false
10
+ params[:log_level] ||= 'error'
11
+
12
+ Officer::Log.set_log_level params[:log_level]
8
13
  end
9
14
 
10
15
  def run
11
- EM.error_handler do |e|
12
- L.debug_exception e
13
- end
14
-
16
+ EM.error_handler {|e| Officer::Log.error e}
17
+
15
18
  EM::run do
16
- if @verbose
17
- EM::PeriodicTimer.new(5) do
18
- Officer::LockStore.instance.log_state
19
- end
19
+ if @params[:stats]
20
+ EM::PeriodicTimer.new(5) {Officer::LockStore.instance.log_state}
20
21
  end
21
22
 
22
- EM::start_server @host, @port, Connection::Connection
23
+ EM::start_server @params[:host], @params[:port], Connection::Connection
23
24
  end
24
25
  end
25
26
  end
data/lib/officer.rb CHANGED
@@ -1,10 +1,11 @@
1
1
  # Standard Ruby.
2
2
  require 'singleton'
3
3
  require 'set'
4
+ require 'logger'
5
+ require 'delegate'
4
6
 
5
7
  # Gems.
6
8
  require 'rubygems'
7
- require 'active_support'
8
9
  require 'eventmachine'
9
10
  require 'json'
10
11
  require 'daemons'
data/officer.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{officer}
8
- s.version = "0.5.3"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Chad Remesch"]
12
- s.date = %q{2010-02-11}
12
+ s.date = %q{2010-02-13}
13
13
  s.default_executable = %q{officer}
14
14
  s.description = %q{Distributed lock server and client written in Ruby and EventMachine}
15
15
  s.email = %q{chad@remesch.com}
@@ -54,20 +54,17 @@ Gem::Specification.new do |s|
54
54
  if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
55
55
  s.add_development_dependency(%q<eventmachine>, [">= 0"])
56
56
  s.add_development_dependency(%q<json>, [">= 0"])
57
- s.add_development_dependency(%q<activesupport>, [">= 0"])
58
57
  s.add_development_dependency(%q<daemons>, [">= 0"])
59
58
  s.add_development_dependency(%q<choice>, [">= 0"])
60
59
  else
61
60
  s.add_dependency(%q<eventmachine>, [">= 0"])
62
61
  s.add_dependency(%q<json>, [">= 0"])
63
- s.add_dependency(%q<activesupport>, [">= 0"])
64
62
  s.add_dependency(%q<daemons>, [">= 0"])
65
63
  s.add_dependency(%q<choice>, [">= 0"])
66
64
  end
67
65
  else
68
66
  s.add_dependency(%q<eventmachine>, [">= 0"])
69
67
  s.add_dependency(%q<json>, [">= 0"])
70
- s.add_dependency(%q<activesupport>, [">= 0"])
71
68
  s.add_dependency(%q<daemons>, [">= 0"])
72
69
  s.add_dependency(%q<choice>, [">= 0"])
73
70
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: officer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.3
4
+ version: 0.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chad Remesch
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-11 00:00:00 -05:00
12
+ date: 2010-02-13 00:00:00 -05:00
13
13
  default_executable: officer
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -32,16 +32,6 @@ dependencies:
32
32
  - !ruby/object:Gem::Version
33
33
  version: "0"
34
34
  version:
35
- - !ruby/object:Gem::Dependency
36
- name: activesupport
37
- type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
40
- requirements:
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: "0"
44
- version:
45
35
  - !ruby/object:Gem::Dependency
46
36
  name: daemons
47
37
  type: :development