thread_man 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/CHANGELOG.md +3 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +22 -0
- data/README.md +74 -0
- data/Rakefile +6 -0
- data/lib/thread_man/version.rb +3 -0
- data/lib/thread_man.rb +115 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/thread_man_spec.rb +68 -0
- data/thread_man.gemspec +23 -0
- metadata +107 -0
data/.gitignore
ADDED
data/.rspec
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2012 Kapil
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
## ThreadMan [![Build Status](https://secure.travis-ci.org/kapso/thread_man.png)](http://travis-ci.org/kapso/thread_man)
|
2
|
+
|
3
|
+
ThreadMan uses `Celluloid` to abstract out concurrent requests pattern.
|
4
|
+
|
5
|
+
### Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'thread_man'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install thread_man
|
18
|
+
|
19
|
+
### Usage
|
20
|
+
|
21
|
+
Sample `Celluloid` object:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
class Car
|
25
|
+
include Celluloid
|
26
|
+
|
27
|
+
def initialize(start_speed = 10)
|
28
|
+
@start_speed = start_speed
|
29
|
+
@name = "Audi #{rand(10)}"
|
30
|
+
end
|
31
|
+
|
32
|
+
def drive(speed = 80)
|
33
|
+
sleep [1, 1.5, 2].sample
|
34
|
+
{ name: @name, current_speed: speed, start_speed: @start_speed }
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
ThreadMan usage inside a rails controller to make async requests:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class HomeController < ApplicationController
|
43
|
+
def index
|
44
|
+
car = Car.new(70)
|
45
|
+
tm = ThreadMan.new(car)
|
46
|
+
|
47
|
+
# Async invocation of "drive" method.
|
48
|
+
4.times { tm.submit(:drive, 76) }
|
49
|
+
|
50
|
+
# Check if async tasks are still running.
|
51
|
+
puts "\nTasks running: #{tm.tasks_running?}"
|
52
|
+
|
53
|
+
# Get response of first submitted request. This call will block until a response is returned.
|
54
|
+
first_response = tm.next_response
|
55
|
+
|
56
|
+
# Get all responses for the submitted requests. Blocking call.
|
57
|
+
# Array of responses. Order is the same as the "submit" order.
|
58
|
+
all_responses = tm.response
|
59
|
+
|
60
|
+
# NOTE: This is required for GC. Ideally you will call this inside "ensure"
|
61
|
+
tm.terminate!
|
62
|
+
|
63
|
+
render json: all_responses
|
64
|
+
end
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
### Contributing
|
69
|
+
|
70
|
+
1. Fork it
|
71
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
72
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
73
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
74
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/thread_man.rb
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
require 'thread_man/version'
|
2
|
+
|
3
|
+
class ThreadMan
|
4
|
+
class << self
|
5
|
+
attr_accessor :logger
|
6
|
+
end
|
7
|
+
|
8
|
+
def initialize(actor = nil)
|
9
|
+
raise 'Not a valid actor object' if actor && !actor.is_a?(Celluloid)
|
10
|
+
|
11
|
+
@init_actor = actor
|
12
|
+
@futures = []
|
13
|
+
@actors = []
|
14
|
+
@response = []
|
15
|
+
@next_response_ctr = 0
|
16
|
+
end
|
17
|
+
|
18
|
+
def submit(method, *params)
|
19
|
+
raise 'Not initialized with a valid actor object' if !@init_actor
|
20
|
+
|
21
|
+
@futures << @init_actor.future(method.to_sym, *params)
|
22
|
+
@actors << @init_actor
|
23
|
+
end
|
24
|
+
|
25
|
+
def submit_actor(actor, method, *params)
|
26
|
+
raise 'Not a valid actor object' if !actor || !actor.is_a?(Celluloid)
|
27
|
+
|
28
|
+
@futures << actor.future(method.to_sym, *params)
|
29
|
+
@actors << actor
|
30
|
+
end
|
31
|
+
|
32
|
+
def response
|
33
|
+
if @futures.size <= 0
|
34
|
+
raise 'No actors/requests submitted, cannot process response'
|
35
|
+
elsif @response.size == @futures.size
|
36
|
+
@response
|
37
|
+
else
|
38
|
+
(@next_response_ctr..(@futures.size - 1)).each do |index|
|
39
|
+
@response << process_request(index)
|
40
|
+
@next_response_ctr += 1
|
41
|
+
end
|
42
|
+
|
43
|
+
@response
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def next_response
|
48
|
+
if @futures.size <= 0
|
49
|
+
raise 'No actors/requests submitted, cannot process response'
|
50
|
+
elsif @next_response_ctr >= @futures.size
|
51
|
+
nil
|
52
|
+
else
|
53
|
+
value = process_request(@next_response_ctr)
|
54
|
+
@response << value
|
55
|
+
@next_response_ctr += 1
|
56
|
+
value
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def terminate!
|
61
|
+
actor_arr = @actors.uniq
|
62
|
+
|
63
|
+
if actor_arr.size > 0
|
64
|
+
actor_arr.each do |actor|
|
65
|
+
begin
|
66
|
+
actor.terminate if actor.alive?
|
67
|
+
rescue => e
|
68
|
+
log "Terminating actor #{actor.inspect}: #{e.message}"
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
@actors.clear
|
73
|
+
@futures.clear
|
74
|
+
@next_response_ctr = 0
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def tasks_running?
|
79
|
+
@actors.uniq.each do |actor|
|
80
|
+
actor.tasks.each { |task| return true if task.running? }
|
81
|
+
end
|
82
|
+
|
83
|
+
false
|
84
|
+
end
|
85
|
+
|
86
|
+
def each_response
|
87
|
+
while resp = next_response
|
88
|
+
yield resp
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
def any_nil_response?
|
93
|
+
response.include? nil
|
94
|
+
end
|
95
|
+
|
96
|
+
protected
|
97
|
+
|
98
|
+
def process_request(index)
|
99
|
+
if @actors[index].alive?
|
100
|
+
@futures[index].value
|
101
|
+
else
|
102
|
+
raise "Actor is dead possibly due to some earlier exception, cannot process response: #{@actors[index].inspect}"
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def log(text)
|
107
|
+
if ThreadMan.logger && ThreadMan.logger.respond_to?(:info)
|
108
|
+
ThreadMan.logger.info("[ThreadMan] #{text}")
|
109
|
+
elsif defined? Rails
|
110
|
+
Rails.logger.info("[ThreadMan] #{text}")
|
111
|
+
else
|
112
|
+
$stdout.print("[ThreadMan] #{text}") and $stdout.flush
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class Car
|
4
|
+
include Celluloid
|
5
|
+
|
6
|
+
def initialize(start_speed = 10)
|
7
|
+
@start_speed = start_speed
|
8
|
+
@name = "Audi #{rand(10)}"
|
9
|
+
end
|
10
|
+
|
11
|
+
def drive(speed = 80)
|
12
|
+
sleep [1, 1.5, 2].sample
|
13
|
+
raise "You cannot drive at 100 or more." if speed >= 100
|
14
|
+
{ name: @name, current_speed: speed, start_speed: @start_speed }
|
15
|
+
end
|
16
|
+
|
17
|
+
def start_speed
|
18
|
+
@start_speed
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe ThreadMan do
|
23
|
+
let(:thread_count) { 5 }
|
24
|
+
let(:car_start_speed) { rand(70) }
|
25
|
+
let(:car) { Car.new(car_start_speed) }
|
26
|
+
|
27
|
+
context "single actor object" do
|
28
|
+
let(:tm) { ThreadMan.new(car) }
|
29
|
+
|
30
|
+
it "should return response after submitting single actor requests" do
|
31
|
+
thread_count.times { tm.submit(:drive, rand(90)) }
|
32
|
+
tm.tasks_running?.should == true
|
33
|
+
tm.response.size.should == thread_count
|
34
|
+
tm.tasks_running?.should == false
|
35
|
+
tm.response.first[:start_speed].should == car_start_speed
|
36
|
+
expect { tm.terminate! }.to_not raise_error
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should throw an exception when getting response without submitting actor requests" do
|
40
|
+
expect { tm.response }.to raise_error
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should raise an error on response" do
|
44
|
+
tm.submit(:drive, 100)
|
45
|
+
expect { tm.response }.to raise_error
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
context "multiple actor objects" do
|
50
|
+
let(:tm) { ThreadMan.new }
|
51
|
+
|
52
|
+
it "should return response after submitting multiple actor requests" do
|
53
|
+
tm.submit_actor(car, :drive, rand(80))
|
54
|
+
thread_count.times { tm.submit_actor(Car.new(rand(20)), :drive, rand(90)) }
|
55
|
+
|
56
|
+
resp = []
|
57
|
+
while r = tm.next_response
|
58
|
+
resp << r
|
59
|
+
end
|
60
|
+
resp.size.should == thread_count + 1
|
61
|
+
|
62
|
+
tm.response.first[:start_speed].should == car.start_speed
|
63
|
+
tm.response.size.should == thread_count + 1
|
64
|
+
|
65
|
+
expect { tm.terminate! }.to_not raise_error
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
data/thread_man.gemspec
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'thread_man/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'thread_man'
|
8
|
+
gem.version = ThreadMans::VERSION
|
9
|
+
gem.authors = ['Kapil']
|
10
|
+
gem.email = ['kapil.israni@gmail.com']
|
11
|
+
gem.description = 'ThreadMan uses Celluloid to abstract out concurrent requests pattern.'
|
12
|
+
gem.summary = gem.description
|
13
|
+
gem.homepage = 'http://github.com/kapso/thread_man'
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map { |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ['lib']
|
19
|
+
|
20
|
+
gem.add_dependency 'celluloid', '~> 0.11.1'
|
21
|
+
gem.add_development_dependency 'rspec'
|
22
|
+
gem.add_development_dependency 'rake'
|
23
|
+
end
|
metadata
ADDED
@@ -0,0 +1,107 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: thread_man
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Kapil
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-08-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: celluloid
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 0.11.1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 0.11.1
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rspec
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
description: ThreadMan uses Celluloid to abstract out concurrent requests pattern.
|
63
|
+
email:
|
64
|
+
- kapil.israni@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- .rspec
|
71
|
+
- CHANGELOG.md
|
72
|
+
- Gemfile
|
73
|
+
- LICENSE.txt
|
74
|
+
- README.md
|
75
|
+
- Rakefile
|
76
|
+
- lib/thread_man.rb
|
77
|
+
- lib/thread_man/version.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/thread_man_spec.rb
|
80
|
+
- thread_man.gemspec
|
81
|
+
homepage: http://github.com/kapso/thread_man
|
82
|
+
licenses: []
|
83
|
+
post_install_message:
|
84
|
+
rdoc_options: []
|
85
|
+
require_paths:
|
86
|
+
- lib
|
87
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
88
|
+
none: false
|
89
|
+
requirements:
|
90
|
+
- - ! '>='
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
requirements: []
|
100
|
+
rubyforge_project:
|
101
|
+
rubygems_version: 1.8.24
|
102
|
+
signing_key:
|
103
|
+
specification_version: 3
|
104
|
+
summary: ThreadMan uses Celluloid to abstract out concurrent requests pattern.
|
105
|
+
test_files:
|
106
|
+
- spec/spec_helper.rb
|
107
|
+
- spec/thread_man_spec.rb
|