Skip to content

Commit 9b696e0

Browse files
committed
initial work on mapper daemon
1 parent ff83adb commit 9b696e0

File tree

6 files changed

+125
-23
lines changed

6 files changed

+125
-23
lines changed

classes/jp_config_loader.rb

Lines changed: 33 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,41 @@
11
#!/usr/bin/env ruby
22
# Find a config file and load it
33
module Jp
4-
def self.load_config
5-
# Where we look for config files
4+
def self.load_server_config
5+
load config_file 'jp-config'
6+
c = JpConfig.new
7+
def c.options
8+
m = self.public_methods - Object.public_methods - [:options]
9+
h = Hash.new
10+
m.each do |method|
11+
h[method] = self.send method
12+
end
13+
h
14+
end
15+
c.options
16+
end
17+
18+
def self.load_mapper_config
19+
load config_file 'jp-mapper-config'
20+
JpMapperConfig.new
21+
end
22+
23+
private
24+
def self.config_file name
25+
if ARGV[0] && File.exists?(ARGV[0])
26+
return File.expand_path ARGV[0]
27+
end
28+
29+
# Prefixes
630
[
7-
ARGV[0],
8-
'./jp-config.rb',
9-
'~/jp-config.rb',
10-
'~/.jp-config.rb',
11-
'/etc/jp-config.rb',
12-
].each do |path|
13-
next unless path
14-
full = File.expand_path path
31+
'./',
32+
'~/',
33+
'~/.',
34+
'/etc/',
35+
].each do |prefix|
36+
full = File.expand_path "%s%s.rb" % [prefix, name]
1537
if File.exists? full
16-
load full
17-
c = JpConfig.new
18-
def c.options
19-
m = self.public_methods - Object.public_methods - [:options]
20-
h = Hash.new
21-
m.each do |method|
22-
h[method] = self.send method
23-
end
24-
h
25-
end
26-
return c.options
38+
return full
2739
end
2840
end
2941
raise 'No config file found.'

classes/jp_mapper.rb

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
#!/usr/bin/env ruby
2+
$LOAD_PATH.push File.dirname(__FILE__) + '/../gen-rb/'
3+
4+
require 'job_pool_instrumented'
5+
require 'ruby-1.9.0-compat'
6+
7+
include Jp
8+
9+
class JpMapper
10+
def initialize config, options = {}
11+
if config.respond_to? :port_number
12+
port_number = config.port_number
13+
end
14+
port_number ||= 9090
15+
raise ArgumentError.new "config object doesn't provide mapping" unless config.respond_to? :host_for_pool
16+
@config = config
17+
18+
# Setup Thrift server (allowing dependency injection)
19+
if options.member? :injected_thrift_server then
20+
@server = options[:injected_thrift_server]
21+
else
22+
processor = options[:thrift_processor] # For testing, and allow instrumented server to override
23+
processor ||= JobPool::Processor.new self
24+
socket = Thrift::ServerSocket.new port_number
25+
transportFactory = Thrift::BufferedTransportFactory.new
26+
27+
@server = Thrift::ThreadedServer.new processor, socket, transportFactory
28+
end
29+
end
30+
31+
def serve
32+
@server.serve
33+
end
34+
35+
def add pool, message
36+
upstream_for_pool(pool).add(pool, message)
37+
end
38+
39+
def acquire pool
40+
upstream_for_pool(pool).acquire(pool)
41+
end
42+
43+
def purge pool, id
44+
upstream_for_pool(pool).purge(pool, id)
45+
end
46+
47+
private
48+
def upstream_for_pool pool
49+
@clients ||= Hash.new
50+
upstream = @config.host_for_pool pool
51+
52+
# Well... in practise, you'll want to specify one or the other ;)
53+
upstream[:host] ||= 'localhost'
54+
upstream[:port] ||= 9090
55+
56+
unless @clients.member? upstream
57+
socket = Thrift::Socket.new upstream[:host], upstream[:port]
58+
transport = Thrift::BufferedTransport.new socket
59+
protocol = Thrift::BinaryProtocol.new transport
60+
client = JobPool::Client.new protocol
61+
@clients[upstream] = client
62+
end
63+
@clients[upstream]
64+
end
65+
end

examples/jp-mapper-config.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class JpMapperConfig
2+
def host_for_pool pool
3+
case pool
4+
when 'foo'
5+
{host: 'localhost', port: 9090}
6+
when /^foo/
7+
{host: 'localhost', port: 9091}
8+
when 'bar'
9+
{host: 'localhost', port: 9092}
10+
else
11+
{host: 'localhost', port: 9093}
12+
# Alternatively:
13+
# raise Jp::NoSuchPool.new
14+
end
15+
end
16+
end

jp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ $LOAD_PATH.push File.dirname(__FILE__) + '/classes/'
55
require 'jp_config_loader'
66
require 'jp_instrumented_server'
77

8-
s = JpInstrumentedServer.new Jp::load_config
8+
s = JpInstrumentedServer.new Jp::load_server_config
99
s.serve

jp-mapper

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env ruby
2+
$LOAD_PATH.push File.dirname(__FILE__)
3+
$LOAD_PATH.push File.dirname(__FILE__) + '/classes/'
4+
5+
require 'jp_config_loader'
6+
require 'jp_mapper'
7+
8+
s = JpMapper.new Jp::load_mapper_config
9+
s.serve

jp-unlocker

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ $LOAD_PATH.push File.dirname(__FILE__) + '/classes/'
55
require 'jp_config_loader'
66
require 'jp_unlocker'
77

8-
s = JpUnlocker.new Jp::load_config
8+
s = JpUnlocker.new Jp::load_server_config
99
s.run

0 commit comments

Comments
 (0)