drmap 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +6 -0
- data/Manifest.txt +16 -0
- data/README.txt +49 -0
- data/Rakefile +14 -0
- data/bin/drirb +10 -0
- data/bin/drworker +16 -0
- data/examples/send_email.rb +7 -0
- data/lib/drmap.rb +16 -0
- data/lib/drmap/beanstalk_pool.rb +52 -0
- data/lib/drmap/beanstalk_worker.rb +45 -0
- data/lib/drmap/drmap_enumerable.rb +18 -0
- data/lib/drmap/version.rb +9 -0
- data/spec/drmap_spec.rb +12 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +11 -0
- data/tasks/rspec.rake +21 -0
- metadata +82 -0
data/History.txt
ADDED
data/Manifest.txt
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
History.txt
|
2
|
+
Manifest.txt
|
3
|
+
README.txt
|
4
|
+
Rakefile
|
5
|
+
bin/drirb
|
6
|
+
bin/drworker
|
7
|
+
examples/send_email.rb
|
8
|
+
lib/drmap.rb
|
9
|
+
lib/drmap/beanstalk_pool.rb
|
10
|
+
lib/drmap/beanstalk_worker.rb
|
11
|
+
lib/drmap/drmap_enumerable.rb
|
12
|
+
lib/drmap/version.rb
|
13
|
+
spec/drmap_spec.rb
|
14
|
+
spec/spec.opts
|
15
|
+
spec/spec_helper.rb
|
16
|
+
tasks/rspec.rake
|
data/README.txt
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
= drmap
|
2
|
+
|
3
|
+
* drmap.rubyforge.org
|
4
|
+
|
5
|
+
== DESCRIPTION:
|
6
|
+
|
7
|
+
drmap distributed Ruby blocks across processes
|
8
|
+
|
9
|
+
== FEATURES/PROBLEMS:
|
10
|
+
|
11
|
+
* TODO
|
12
|
+
|
13
|
+
== SYNOPSIS:
|
14
|
+
|
15
|
+
TODO
|
16
|
+
|
17
|
+
== REQUIREMENTS:
|
18
|
+
|
19
|
+
* Beanstalkd
|
20
|
+
* ruby2ruby gem
|
21
|
+
|
22
|
+
== INSTALL:
|
23
|
+
|
24
|
+
* sudo gem install drmap
|
25
|
+
|
26
|
+
== LICENSE:
|
27
|
+
|
28
|
+
(The MIT License)
|
29
|
+
|
30
|
+
Copyright (c) 2008 FIX
|
31
|
+
|
32
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
33
|
+
a copy of this software and associated documentation files (the
|
34
|
+
'Software'), to deal in the Software without restriction, including
|
35
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
36
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
37
|
+
permit persons to whom the Software is furnished to do so, subject to
|
38
|
+
the following conditions:
|
39
|
+
|
40
|
+
The above copyright notice and this permission notice shall be
|
41
|
+
included in all copies or substantial portions of the Software.
|
42
|
+
|
43
|
+
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
|
44
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
45
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
46
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
47
|
+
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
48
|
+
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
49
|
+
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './lib/drmap.rb'
|
6
|
+
|
7
|
+
Hoe.new('drmap', Drmap::VERSION) do |p|
|
8
|
+
p.rubyforge_name = 'drmap'
|
9
|
+
p.developer('Marc Chung', 'marc.chung@openrain.com')
|
10
|
+
end
|
11
|
+
|
12
|
+
Dir['tasks/**/*.rake'].each { |rake| load rake }
|
13
|
+
|
14
|
+
# vim: syntax=Ruby
|
data/bin/drirb
ADDED
data/bin/drworker
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'drmap'
|
5
|
+
|
6
|
+
if __FILE__ == $0
|
7
|
+
host = "localhost:11300"
|
8
|
+
if ARGV[0]
|
9
|
+
host = ARGV[0]
|
10
|
+
end
|
11
|
+
puts "Connecting to #{host}" # TODO Should probably test connection!
|
12
|
+
Drmap::BeanstalkPool.hosts = [host]
|
13
|
+
pool = Drmap::BeanstalkPool.new
|
14
|
+
worker = Drmap::BeanstalkWorker.new(pool)
|
15
|
+
worker.process
|
16
|
+
end
|
data/lib/drmap.rb
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless
|
2
|
+
$:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
3
|
+
|
4
|
+
require 'pp'
|
5
|
+
|
6
|
+
require 'beanstalk-client'
|
7
|
+
require 'ruby2ruby'
|
8
|
+
|
9
|
+
require 'drmap/drmap_enumerable'
|
10
|
+
require 'drmap/beanstalk_worker'
|
11
|
+
require 'drmap/beanstalk_pool'
|
12
|
+
require 'drmap/version'
|
13
|
+
|
14
|
+
module Drmap
|
15
|
+
VERSION = Drmap::Version::STRING
|
16
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Drmap
|
2
|
+
class BeanstalkPool
|
3
|
+
|
4
|
+
# Worst design ever!
|
5
|
+
def self.hosts=(bean_hosts)
|
6
|
+
@@hosts = bean_hosts
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize
|
10
|
+
@pool = Beanstalk::Pool.new(@@hosts)
|
11
|
+
end
|
12
|
+
|
13
|
+
def peers
|
14
|
+
@pool
|
15
|
+
end
|
16
|
+
|
17
|
+
def next_job
|
18
|
+
watch_jobs
|
19
|
+
@pool.reserve
|
20
|
+
end
|
21
|
+
|
22
|
+
def put_job(jid, proc_str, data)
|
23
|
+
@pool.use("job")
|
24
|
+
payload = {:pid => Process.pid, :jid => jid, :proc => proc_str, :data => data}
|
25
|
+
@pool.yput(payload)
|
26
|
+
end
|
27
|
+
|
28
|
+
def next_result(jid)
|
29
|
+
watch_results(jid)
|
30
|
+
job = @pool.reserve
|
31
|
+
job_payload = job.ybody[:result]
|
32
|
+
end
|
33
|
+
|
34
|
+
def put_result(jid, result)
|
35
|
+
@pool.use("result#{jid}")
|
36
|
+
payload = {:pid => Process.pid, :jid => jid, :result => result}
|
37
|
+
@pool.yput(payload)
|
38
|
+
end
|
39
|
+
|
40
|
+
def watch_jobs
|
41
|
+
@pool.watch("job")
|
42
|
+
@pool.ignore("default")
|
43
|
+
end
|
44
|
+
|
45
|
+
def watch_results(jid)
|
46
|
+
@pool.watch("result#{jid}")
|
47
|
+
@pool.ignore("default")
|
48
|
+
@pool.ignore("job")
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
module Drmap
|
2
|
+
class BeanstalkWorker
|
3
|
+
|
4
|
+
def initialize(pool)
|
5
|
+
@pool = pool
|
6
|
+
end
|
7
|
+
|
8
|
+
def process
|
9
|
+
loop do
|
10
|
+
begin
|
11
|
+
job_payload = next_job
|
12
|
+
pp job_payload
|
13
|
+
result = eval(job_payload[:proc]).call(job_payload[:data])
|
14
|
+
save(job_payload[:jid], result)
|
15
|
+
success!
|
16
|
+
rescue => e
|
17
|
+
fail!(e)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def next_job
|
23
|
+
@job = @pool.next_job
|
24
|
+
@job.ybody
|
25
|
+
end
|
26
|
+
|
27
|
+
def success!
|
28
|
+
@job.delete
|
29
|
+
end
|
30
|
+
|
31
|
+
def fail!(e)
|
32
|
+
puts "ERROR #{e.message}"
|
33
|
+
e.backtrace.each do |msg|
|
34
|
+
pp msg
|
35
|
+
end
|
36
|
+
@job.bury
|
37
|
+
end
|
38
|
+
|
39
|
+
def save(jid, result)
|
40
|
+
@pool.put_result(jid, result)
|
41
|
+
pp [@job[:pid], jid, result]
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
module Enumerable
|
2
|
+
|
3
|
+
def drmap(&block)
|
4
|
+
pool = Drmap::BeanstalkPool.new
|
5
|
+
|
6
|
+
jid = rand(100)
|
7
|
+
each_with_index do |element, idx|
|
8
|
+
pool.put_job(jid, block.to_ruby, element)
|
9
|
+
end
|
10
|
+
|
11
|
+
results = []
|
12
|
+
while results.size < length
|
13
|
+
results << pool.next_result(jid)
|
14
|
+
end
|
15
|
+
results
|
16
|
+
end
|
17
|
+
|
18
|
+
end
|
data/spec/drmap_spec.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
|
3
|
+
describe "Drmap" do
|
4
|
+
|
5
|
+
it "should distribute tasks" do
|
6
|
+
addresses = ["baz@example.com", "foo@example.com", "bar@example.com"]
|
7
|
+
addresses.drmap do |email|
|
8
|
+
puts "You are now emailing: #{email}"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
data/tasks/rspec.rake
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
begin
|
2
|
+
require 'spec'
|
3
|
+
rescue LoadError
|
4
|
+
require 'rubygems'
|
5
|
+
require 'spec'
|
6
|
+
end
|
7
|
+
begin
|
8
|
+
require 'spec/rake/spectask'
|
9
|
+
rescue LoadError
|
10
|
+
puts <<-EOS
|
11
|
+
To use rspec for testing you must install rspec gem:
|
12
|
+
gem install rspec
|
13
|
+
EOS
|
14
|
+
exit(0)
|
15
|
+
end
|
16
|
+
|
17
|
+
desc "Run the specs under spec/models"
|
18
|
+
Spec::Rake::SpecTask.new do |t|
|
19
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
20
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
21
|
+
end
|
metadata
ADDED
@@ -0,0 +1,82 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: drmap
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Marc Chung
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-09-27 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: hoe
|
17
|
+
type: :development
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 1.7.0
|
24
|
+
version:
|
25
|
+
description: drmap distributed Ruby blocks across processes
|
26
|
+
email:
|
27
|
+
- marc.chung@openrain.com
|
28
|
+
executables:
|
29
|
+
- drirb
|
30
|
+
- drworker
|
31
|
+
extensions: []
|
32
|
+
|
33
|
+
extra_rdoc_files:
|
34
|
+
- History.txt
|
35
|
+
- Manifest.txt
|
36
|
+
- README.txt
|
37
|
+
files:
|
38
|
+
- History.txt
|
39
|
+
- Manifest.txt
|
40
|
+
- README.txt
|
41
|
+
- Rakefile
|
42
|
+
- bin/drirb
|
43
|
+
- bin/drworker
|
44
|
+
- examples/send_email.rb
|
45
|
+
- lib/drmap.rb
|
46
|
+
- lib/drmap/beanstalk_pool.rb
|
47
|
+
- lib/drmap/beanstalk_worker.rb
|
48
|
+
- lib/drmap/drmap_enumerable.rb
|
49
|
+
- lib/drmap/version.rb
|
50
|
+
- spec/drmap_spec.rb
|
51
|
+
- spec/spec.opts
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
- tasks/rspec.rake
|
54
|
+
has_rdoc: true
|
55
|
+
homepage: drmap.rubyforge.org
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --main
|
59
|
+
- README.txt
|
60
|
+
require_paths:
|
61
|
+
- lib
|
62
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
69
|
+
requirements:
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: "0"
|
73
|
+
version:
|
74
|
+
requirements: []
|
75
|
+
|
76
|
+
rubyforge_project: drmap
|
77
|
+
rubygems_version: 1.2.0
|
78
|
+
signing_key:
|
79
|
+
specification_version: 2
|
80
|
+
summary: drmap distributed Ruby blocks across processes
|
81
|
+
test_files: []
|
82
|
+
|