mutual 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/LICENSE +7 -0
- data/README.md +3 -0
- data/lib/incrementable.rb +10 -0
- data/lib/messaging/result_manager.rb +78 -0
- data/lib/messaging/task_manager.rb +52 -0
- data/lib/mutual.rb +76 -0
- metadata +70 -0
data/LICENSE
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
Copyright (c) 2012 Daniel Yoder
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
|
+
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
6
|
+
|
7
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,78 @@
|
|
1
|
+
module Mutual
|
2
|
+
class Messaging
|
3
|
+
|
4
|
+
class ResultManager
|
5
|
+
|
6
|
+
attr_reader :tasks
|
7
|
+
def initialize(messaging, configuration={})
|
8
|
+
@messaging = messaging
|
9
|
+
@timeout = configuration[:timeout] || 8 # seconds
|
10
|
+
|
11
|
+
# will be used as a blocking client
|
12
|
+
@client = @messaging.create_redis_client
|
13
|
+
@timeouts = []
|
14
|
+
@tasks = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
# BLOCKING!
|
18
|
+
def wait
|
19
|
+
@messaging.pop_queue(@messaging.id)
|
20
|
+
end
|
21
|
+
|
22
|
+
def listen(task, options={}, &block)
|
23
|
+
now = Time.now
|
24
|
+
ttl = options[:timeout] || 8
|
25
|
+
expire = (now + ttl).to_i
|
26
|
+
@timeouts << [expire, task.task_id]
|
27
|
+
@tasks[task.task_id] = block
|
28
|
+
end
|
29
|
+
|
30
|
+
def match(result)
|
31
|
+
@tasks.delete(result[:task_id])
|
32
|
+
end
|
33
|
+
|
34
|
+
def handle_timeouts
|
35
|
+
now = Time.now.to_i
|
36
|
+
while @timeouts[0] && @timeouts[0][0] > now
|
37
|
+
expire, task_id = @timeouts.shift
|
38
|
+
if listener = @tasks.delete(task_id)
|
39
|
+
listener.call :timeout => true
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def run
|
45
|
+
Thread.new do
|
46
|
+
thread = Thread.current
|
47
|
+
thread[:run] = true
|
48
|
+
while thread[:run]
|
49
|
+
sleep 0.5
|
50
|
+
handle_timeouts
|
51
|
+
end
|
52
|
+
end
|
53
|
+
read do |result|
|
54
|
+
if listener = match(result)
|
55
|
+
listener.call(result)
|
56
|
+
else
|
57
|
+
#print "\ngot result no one was expecting: #{result.inspect}"
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
def read(&block)
|
63
|
+
@thread = Thread.new do
|
64
|
+
thread = Thread.current
|
65
|
+
thread[:run] = true
|
66
|
+
while thread[:run]
|
67
|
+
if result = @messaging.pop_queue(@messaging.id)
|
68
|
+
yield(result)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Mutual
|
2
|
+
class Messaging
|
3
|
+
class TaskManager
|
4
|
+
|
5
|
+
def initialize(messaging)
|
6
|
+
@messaging = messaging
|
7
|
+
@client = @messaging.create_redis_client
|
8
|
+
end
|
9
|
+
|
10
|
+
|
11
|
+
def create(options)
|
12
|
+
options[:task_id] = @messaging.generate_id
|
13
|
+
if options.delete(:response)
|
14
|
+
options[:return_address] = @messaging.id
|
15
|
+
end
|
16
|
+
Task.new(options)
|
17
|
+
end
|
18
|
+
|
19
|
+
def send(task)
|
20
|
+
@messaging.push_queue(task.queue, task)
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
class Task
|
26
|
+
attr_reader :queue, :task_id, :return_address
|
27
|
+
def initialize(options)
|
28
|
+
properties = options.values_at(:task_id, :type, :name, :body)
|
29
|
+
raise ArgumentError unless properties.all? {|p| p != nil }
|
30
|
+
@task_id, @type, @name, @body = properties
|
31
|
+
@return_address = options[:return_address]
|
32
|
+
@queue = "tasks.#{@type}"
|
33
|
+
end
|
34
|
+
|
35
|
+
def marshal
|
36
|
+
hash = {
|
37
|
+
:task_id => @task_id,
|
38
|
+
:name => @name,
|
39
|
+
:body => @body
|
40
|
+
}
|
41
|
+
hash[:return_address] = @return_address if @return_address
|
42
|
+
hash
|
43
|
+
end
|
44
|
+
|
45
|
+
def to_json(*args)
|
46
|
+
marshal.to_json(*args)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
data/lib/mutual.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require "rubygems"
|
2
|
+
gem "redis", ">= 3.0.1"
|
3
|
+
gem "json"
|
4
|
+
require "redis"
|
5
|
+
require "json"
|
6
|
+
|
7
|
+
require "incrementable"
|
8
|
+
require "messaging/task_manager"
|
9
|
+
require "messaging/result_manager"
|
10
|
+
|
11
|
+
Thread.abort_on_exception = true
|
12
|
+
module Mutual
|
13
|
+
|
14
|
+
class Messaging
|
15
|
+
# we need both class and instance counters
|
16
|
+
extend Incrementable
|
17
|
+
include Incrementable
|
18
|
+
|
19
|
+
attr_reader :configuration, :id, :result_manager, :task_manager
|
20
|
+
|
21
|
+
def initialize(process_name, configuration)
|
22
|
+
@process_name = process_name
|
23
|
+
@configuration = configuration
|
24
|
+
@id = "#{@process_name}-#{self.class.increment!}"
|
25
|
+
|
26
|
+
@pop_timeout = 8
|
27
|
+
@task_manager = TaskManager.new(self)
|
28
|
+
@result_manager = ResultManager.new(self)
|
29
|
+
@client = create_redis_client
|
30
|
+
@clients = {}
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def generate_id
|
35
|
+
"#{@id}-#{increment!}"
|
36
|
+
end
|
37
|
+
|
38
|
+
def create_redis_client
|
39
|
+
Redis.new(:host => @configuration[:host], :port => @configuration[:port])
|
40
|
+
end
|
41
|
+
|
42
|
+
def send_task(options, &block)
|
43
|
+
task = @task_manager.create(options)
|
44
|
+
if block
|
45
|
+
@result_manager.listen(task, &block)
|
46
|
+
end
|
47
|
+
@task_manager.send(task)
|
48
|
+
end
|
49
|
+
|
50
|
+
def multi_task(*args)
|
51
|
+
raise "unimplemented"
|
52
|
+
end
|
53
|
+
|
54
|
+
def send_result(*args)
|
55
|
+
raise "unimplemented"
|
56
|
+
end
|
57
|
+
|
58
|
+
def push_queue(queue, object)
|
59
|
+
message = object.to_json
|
60
|
+
@client.lpush(queue, message)
|
61
|
+
end
|
62
|
+
|
63
|
+
# WARNING: blocking call.
|
64
|
+
def pop_queue(queue)
|
65
|
+
@clients[queue] ||= create_redis_client
|
66
|
+
if result = @clients[queue].brpop(queue, :timeout => @pop_timeout)
|
67
|
+
_channel, message = result
|
68
|
+
JSON.parse(message, :symbolize_names => true)
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: mutual
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease:
|
5
|
+
version: 0.1.0
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Daniel Yoder
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
|
13
|
+
date: 2012-09-13 00:00:00 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: starter
|
17
|
+
prerelease: false
|
18
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
19
|
+
none: false
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.1.0
|
24
|
+
type: :development
|
25
|
+
version_requirements: *id001
|
26
|
+
description:
|
27
|
+
email:
|
28
|
+
executables: []
|
29
|
+
|
30
|
+
extensions: []
|
31
|
+
|
32
|
+
extra_rdoc_files: []
|
33
|
+
|
34
|
+
files:
|
35
|
+
- LICENSE
|
36
|
+
- README.md
|
37
|
+
- lib/incrementable.rb
|
38
|
+
- lib/messaging/result_manager.rb
|
39
|
+
- lib/messaging/task_manager.rb
|
40
|
+
- lib/mutual.rb
|
41
|
+
homepage:
|
42
|
+
licenses: []
|
43
|
+
|
44
|
+
post_install_message:
|
45
|
+
rdoc_options: []
|
46
|
+
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: "0"
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
requirements: []
|
62
|
+
|
63
|
+
rubyforge_project:
|
64
|
+
rubygems_version: 1.8.23
|
65
|
+
signing_key:
|
66
|
+
specification_version: 3
|
67
|
+
summary: Allows you to manage tasks using Redis as a message transport.
|
68
|
+
test_files: []
|
69
|
+
|
70
|
+
has_rdoc:
|