arya-pandemic 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -2,7 +2,7 @@ require 'rubygems'
2
2
  require 'rake'
3
3
  require 'echoe'
4
4
 
5
- Echoe.new('pandemic', '0.4.3') do |p|
5
+ Echoe.new('pandemic', '0.4.4') do |p|
6
6
  p.description = "A framework for distributing work for real-time services and offline tasks."
7
7
  p.url = "https://github.com/arya/pandemic/"
8
8
  p.author = "Arya Asemanfar"
data/lib/pandemic.rb CHANGED
@@ -6,6 +6,7 @@ require 'monitor'
6
6
  require 'yaml'
7
7
  require 'digest/md5'
8
8
  require 'logger'
9
+ require 'optparse'
9
10
 
10
11
  require 'pandemic/util'
11
12
  require 'pandemic/connection_pool'
@@ -2,19 +2,9 @@ module Pandemic
2
2
  module ServerSide
3
3
  class Config
4
4
  class << self
5
- attr_accessor :bind_to, :servers, :response_timeout, :fork_for_processor
5
+ attr_accessor :bind_to, :servers, :response_timeout, :fork_for_processor, :pid_file
6
6
  def load
7
- path = extract_config_path
8
- yaml = YAML.load_file(path)
9
-
10
- @server_map = yaml['servers'] || []
11
- @servers = @server_map.is_a?(Hash) ? @server_map.values : @server_map
12
- @servers = @servers.collect { |s| s.is_a?(Hash) ? s.keys.first : s }
13
-
14
- @response_timeout = (yaml['response_timeout'] || 1).to_f
15
- @bind_to = extract_bind_to
16
- @fork_for_processor = yaml['fork_for_processor']
17
-
7
+ parse_args!
18
8
  raise "Interface to bind to is nil." unless @bind_to
19
9
  end
20
10
 
@@ -23,33 +13,60 @@ module Pandemic
23
13
  end
24
14
 
25
15
  private
26
- def extract_bind_to
27
- index = ARGV.index('-i')
28
- index2 = ARGV.index('-a')
16
+
17
+ def parse_args!
18
+ config_path = "pandemic_server.yml"
19
+ index = nil
20
+ attach = nil
21
+
22
+ @bind_to = nil
23
+ @pid_file = nil
24
+ OptionParser.new do |opts|
25
+ opts.on("-c", "--config [CONFIG-PATH]", "Specify the path to the config file") do |path|
26
+ config_path = path
27
+ end
28
+
29
+ opts.on("-i", "--index [SERVER-INDEX]", "Specify the index of the server to attach to from the YAML file") do |i|
30
+ index = i
31
+ end
29
32
 
30
- if index && (key = ARGV[index + 1])
31
- key = key.to_i if @server_map.is_a?(Array)
32
- server = @server_map[key]
33
- if server.is_a?(Hash)
33
+ opts.on("-a", "--attach [SERVER:PORT]", "Specify the host and port to attach to") do |a|
34
+ attach = a
35
+ end
36
+
37
+ opts.on("-P", "--pid-file [PATH]", "Specify the path to write the PID to") do |path|
38
+ @pid_file = path
39
+ end
40
+ end.parse!
41
+
42
+ read_config_file(config_path)
43
+
44
+ if index
45
+ index = index.to_i if @server_map.is_a?(Array)
46
+ server = @server_map[index]
47
+
48
+ @bind_to = if server.is_a?(Hash)
34
49
  @options = server.values.first # there should only be one
35
- @server_map[key].keys.first
50
+ @server_map[index].keys.first
36
51
  else
37
52
  server
38
53
  end
39
- elsif index2 && (host = ARGV[index2 + 1])
40
- host
41
- else
42
- raise "You must specify which interface to bind to."
54
+ elsif attach
55
+ @bind_to = attach
43
56
  end
57
+
44
58
  end
45
-
46
- def extract_config_path
47
- index = ARGV.index('-c')
48
- if index && (path = ARGV[index + 1])
49
- path
50
- else
51
- "pandemic_server.yml"
52
- end
59
+
60
+ def read_config_file(path)
61
+ yaml = YAML.load_file(path)
62
+
63
+ @server_map = yaml['servers'] || []
64
+ @servers = @server_map.is_a?(Hash) ? @server_map.values : @server_map
65
+ @servers = @servers.collect { |s| s.is_a?(Hash) ? s.keys.first : s }
66
+
67
+ @response_timeout = (yaml['response_timeout'] || 1).to_f
68
+
69
+ @fork_for_processor = yaml['fork_for_processor']
53
70
  end
54
71
  end
55
72
  end
@@ -52,6 +52,7 @@ module Pandemic
52
52
  raise "You must specify a handler" unless @handler
53
53
 
54
54
  @listener = TCPServer.new(@host, @port)
55
+ write_pid_file
55
56
  @running = true
56
57
  @running_since = Time.now
57
58
 
@@ -72,6 +73,7 @@ module Pandemic
72
73
  end
73
74
  rescue StopServer
74
75
  info("Stopping server")
76
+ remove_pid_file
75
77
  @listener.close if @listener
76
78
  @peers.values.each { |p| p.disconnect }
77
79
  @clients.each {|c| c.close }
@@ -265,6 +267,18 @@ module Pandemic
265
267
  results
266
268
  end
267
269
 
270
+ def remove_pid_file
271
+ File.unlink(Config.pid_file) if Config.pid_file && File.exists?(Config.pid_file)
272
+ end
273
+
274
+ def write_pid_file
275
+ return if Config.pid_file.nil?
276
+ File.open(Config.pid_file,"w") do |f|
277
+ f.write(Process.pid)
278
+ File.chmod(0644, Config.pid_file)
279
+ end
280
+ end
281
+
268
282
  end
269
283
  end
270
284
  end
data/pandemic.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{pandemic}
5
- s.version = "0.4.3"
5
+ s.version = "0.4.4"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["Arya Asemanfar"]
data/test/server_test.rb CHANGED
@@ -30,6 +30,32 @@ class ServerTest < Test::Unit::TestCase
30
30
  end
31
31
  end
32
32
 
33
+ should "create a pid file" do
34
+ ignore_threads = Thread.list
35
+
36
+ Pandemic::ServerSide::Config.expects(:pid_file).at_least_once.returns("test/pandemic.pid")
37
+ Pandemic::ServerSide::Config.expects(:servers).at_least_once.returns([])
38
+ @server = Pandemic::ServerSide::Server.new("localhost:4000")
39
+
40
+ @tcpserver = mock()
41
+ TCPServer.expects(:new).with("localhost", 4000).returns(@tcpserver)
42
+
43
+ file = mock()
44
+ File.expects(:open).with("test/pandemic.pid", "w").yields(file)
45
+ file.expects(:write).with(Process.pid)
46
+ File.expects(:chmod)
47
+ File.expects(:exists?).with("test/pandemic.pid").returns(true)
48
+ File.expects(:unlink).with("test/pandemic.pid")
49
+
50
+ @tcpserver.expects(:accept).once.raises(Pandemic::ServerSide::Server::StopServer)
51
+
52
+ @tcpserver.expects(:close)
53
+
54
+ @server.handler = mock(:new)
55
+ @server.start
56
+ wait_for_threads(ignore_threads)
57
+ end
58
+
33
59
  should "initialize peers" do
34
60
  Pandemic::ServerSide::Config.expects(:servers).returns(["localhost:4000", "localhost:4001"])
35
61
  Pandemic::ServerSide::Peer.expects(:new).with("localhost:4001", is_a(Pandemic::ServerSide::Server))
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: arya-pandemic
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Arya Asemanfar