zack 0.3.0 → 0.3.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/lib/zack/client.rb +32 -57
- data/lib/zack/server.rb +15 -53
- data/lib/zack.rb +8 -3
- metadata +17 -6
data/History.txt
CHANGED
data/lib/zack/client.rb
CHANGED
@@ -1,67 +1,42 @@
|
|
1
1
|
|
2
|
-
require 'uuid'
|
3
2
|
|
4
|
-
|
5
|
-
#
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
def initialize(tube_name, opts={})
|
10
|
-
server = opts[:server] || 'beanstalk:11300'
|
11
|
-
# Only respond to these messages
|
12
|
-
@only = opts[:only] || proc { true }
|
13
|
-
# These have answers (wait for the server to answer)
|
14
|
-
@with_answer = opts[:with_answer] || []
|
3
|
+
module Zack
|
4
|
+
# Client for a simple client-server RPC connection.
|
5
|
+
#
|
6
|
+
class Client
|
7
|
+
attr_reader :service
|
15
8
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
9
|
+
# Constructs a client for the service given by tube_name. Optional
|
10
|
+
# arguments are:
|
11
|
+
#
|
12
|
+
# :server :: beanstalkd server location url
|
13
|
+
# :only :: ignores all messages not in this hash
|
14
|
+
# :with_answer :: these messages wait for an answer from the service
|
15
|
+
#
|
16
|
+
def initialize(tube_name, opts={})
|
17
|
+
server = opts[:server] || 'beanstalk:11300'
|
18
|
+
# Only respond to these messages
|
19
|
+
@only = opts[:only] || proc { true }
|
20
|
+
# These have answers (wait for the server to answer)
|
21
|
+
@with_answer = opts[:with_answer] || []
|
22
|
+
|
23
|
+
@outgoing = Cod.beanstalk(server, tube_name)
|
24
|
+
unless @with_answer.empty?
|
25
|
+
@incoming = Cod.beanstalk(server,
|
26
|
+
UniqueName.new(tube_name))
|
27
|
+
end
|
21
28
|
|
22
|
-
|
23
|
-
|
29
|
+
@service = Cod::Client.new(@outgoing, @incoming, 1)
|
30
|
+
end
|
24
31
|
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
def method_missing(sym, *args, &block)
|
29
|
-
super unless respond_to?(sym)
|
30
|
-
|
31
|
-
raise ArgumentError, "Can't call methods remotely with a block" if block
|
32
|
+
def respond_to?(msg)
|
33
|
+
!! @only[msg]
|
34
|
+
end
|
32
35
|
|
33
|
-
|
34
|
-
|
35
|
-
else
|
36
|
-
service.notify [sym, args]
|
37
|
-
return nil
|
36
|
+
def has_answer?(sym)
|
37
|
+
@with_answer.include?(sym.to_sym)
|
38
38
|
end
|
39
|
-
rescue Cod::Channel::TimeoutError
|
40
|
-
raise Zack::ServiceTimeout, "No response from server in the allowed time."
|
41
|
-
end
|
42
|
-
|
43
|
-
def has_answer?(sym)
|
44
|
-
@with_answer.include?(sym.to_sym)
|
45
|
-
end
|
46
39
|
|
47
|
-
|
48
|
-
# Pretend that UUIDs don't collide for now.
|
49
|
-
#
|
50
|
-
def unique_tube_name(name)
|
51
|
-
"name.#{uuid}"
|
52
|
-
end
|
53
|
-
def uuid
|
54
|
-
uuid_generator.generate
|
55
|
-
end
|
56
|
-
def uuid_generator
|
57
|
-
generator=Thread.current[:zack_uuid_generator]
|
58
|
-
return generator if generator
|
59
|
-
|
60
|
-
# assert: generator is nil
|
61
|
-
|
62
|
-
# Pretend we've just forked, because that might be the case.
|
63
|
-
UUID.generator.next_sequence
|
64
|
-
|
65
|
-
Thread.current[:zack_uuid_generator]=generator=UUID.new
|
40
|
+
include TransparentProxy
|
66
41
|
end
|
67
42
|
end
|
data/lib/zack/server.rb
CHANGED
@@ -1,61 +1,23 @@
|
|
1
1
|
|
2
|
-
|
3
|
-
#
|
4
|
-
|
5
|
-
class
|
6
|
-
|
7
|
-
end
|
8
|
-
|
9
|
-
attr_reader :service
|
2
|
+
module Zack
|
3
|
+
# Server side for RPC calls.
|
4
|
+
#
|
5
|
+
class Server < Target
|
6
|
+
attr_reader :service
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
if opts.has_key? :factory
|
15
|
-
@factory = opts[:factory]
|
16
|
-
elsif opts.has_key? :simple
|
17
|
-
@factory = SimpleFactory.new(opts[:simple])
|
18
|
-
else
|
19
|
-
raise ArgumentError, "Either :factory or :simple argument must be given."
|
20
|
-
end
|
8
|
+
def initialize(tube_name, opts={})
|
9
|
+
super
|
21
10
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
25
|
-
|
26
|
-
# Handles exactly one request.
|
27
|
-
#
|
28
|
-
def handle_request
|
29
|
-
service.one { |(sym, args)|
|
30
|
-
instance = @factory.produce
|
31
|
-
|
32
|
-
instance.send(sym, *args)
|
33
|
-
}
|
34
|
-
end
|
35
|
-
|
36
|
-
# Runs the server and keeps running until the world ends (or the process,
|
37
|
-
# whichever comes first).
|
38
|
-
#
|
39
|
-
def run(&block)
|
40
|
-
loop do
|
41
|
-
exception_handling(block) do
|
42
|
-
handle_request
|
43
|
-
end
|
11
|
+
channel = Cod.beanstalk(server, tube_name)
|
12
|
+
@service = Cod::Service.new(channel)
|
44
13
|
end
|
45
|
-
end
|
46
14
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
yield
|
54
|
-
rescue => exception
|
55
|
-
exception_handler.call(exception)
|
56
|
-
end
|
57
|
-
else
|
58
|
-
yield
|
15
|
+
# Handles exactly one request.
|
16
|
+
#
|
17
|
+
def handle_request
|
18
|
+
service.one { |(sym, args)|
|
19
|
+
process_request(sym, args)
|
20
|
+
}
|
59
21
|
end
|
60
22
|
end
|
61
23
|
end
|
data/lib/zack.rb
CHANGED
@@ -2,11 +2,16 @@
|
|
2
2
|
require 'cod'
|
3
3
|
|
4
4
|
module Zack
|
5
|
-
autoload :Server, 'zack/server'
|
6
|
-
autoload :Client, 'zack/client'
|
7
|
-
|
8
5
|
# Gets raised when the server doesn't answer within the currently configured
|
9
6
|
# timeout for a message that waits for an answer.
|
10
7
|
#
|
11
8
|
class ServiceTimeout < StandardError; end
|
12
9
|
end
|
10
|
+
|
11
|
+
require 'zack/transparent_proxy'
|
12
|
+
require 'zack/unique_name'
|
13
|
+
|
14
|
+
require 'zack/target'
|
15
|
+
|
16
|
+
require 'zack/server'
|
17
|
+
require 'zack/client'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: zack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,12 +10,12 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2011-
|
13
|
+
date: 2011-08-31 00:00:00.000000000 +02:00
|
14
14
|
default_executable:
|
15
15
|
dependencies:
|
16
16
|
- !ruby/object:Gem::Dependency
|
17
17
|
name: cod
|
18
|
-
requirement: &
|
18
|
+
requirement: &2166707740 !ruby/object:Gem::Requirement
|
19
19
|
none: false
|
20
20
|
requirements:
|
21
21
|
- - ~>
|
@@ -23,10 +23,10 @@ dependencies:
|
|
23
23
|
version: '0.3'
|
24
24
|
type: :runtime
|
25
25
|
prerelease: false
|
26
|
-
version_requirements: *
|
26
|
+
version_requirements: *2166707740
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: beanstalk-client
|
29
|
-
requirement: &
|
29
|
+
requirement: &2166707260 !ruby/object:Gem::Requirement
|
30
30
|
none: false
|
31
31
|
requirements:
|
32
32
|
- - ~>
|
@@ -34,7 +34,18 @@ dependencies:
|
|
34
34
|
version: '1.0'
|
35
35
|
type: :runtime
|
36
36
|
prerelease: false
|
37
|
-
version_requirements: *
|
37
|
+
version_requirements: *2166707260
|
38
|
+
- !ruby/object:Gem::Dependency
|
39
|
+
name: uuid
|
40
|
+
requirement: &2166706800 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '2.3'
|
46
|
+
type: :runtime
|
47
|
+
prerelease: false
|
48
|
+
version_requirements: *2166706800
|
38
49
|
description:
|
39
50
|
email:
|
40
51
|
- kaspar.schiess@absurd.li
|