em-worker 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +17 -0
- data/.rspec +2 -0
- data/.travis.yml +6 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/em-worker.gemspec +25 -0
- data/lib/em/worker.rb +33 -0
- data/spec/async_helpers.rb +21 -0
- data/spec/spec_helper.rb +17 -0
- data/spec/worker_spec.rb +65 -0
- metadata +115 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 36c1f745c922f5642586de1dd655903c00d83f3f
|
4
|
+
data.tar.gz: 6f324eeba738f4206a671871cc2fd441fd50f716
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 46995ca518615c9ce85a31508e6e17acf3fd2631fad36825ce257cfa9056522a39a55ad47484fe2fbe2ca4dbe36db1f0fd42360b4676a224f1f35fccb6b43928
|
7
|
+
data.tar.gz: 343abd0e38f47c5f6153b7f7b642a5455b8cabc46d17e4a0cbd01a2387d457f6a9b88fe42d22f31e1eedde4c19cf7fc655a8ae9f89a33f7c9328680d48ecab40
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Sean Porter
|
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,43 @@
|
|
1
|
+
# EM::Worker
|
2
|
+
|
3
|
+
Provides a simple task worker for EventMachine, with a task concurrency limit. Tasks are executed in the EventMachine threadpool.
|
4
|
+
|
5
|
+
[![Build Status](https://secure.travis-ci.org/portertech/em-worker.png)](https://travis-ci.org/portertech/em-worker)
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
gem 'em-worker'
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install em-worker
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
``` ruby
|
24
|
+
@worker = EM::Worker.new(:concurrency => 12) # defaults to 10
|
25
|
+
|
26
|
+
@worker.enqueue do
|
27
|
+
puts "winning"
|
28
|
+
end
|
29
|
+
|
30
|
+
task = Proc.new { "tiger blood" }
|
31
|
+
callback = Proc.new do |result|
|
32
|
+
puts result
|
33
|
+
end
|
34
|
+
@worker.enqueue(task, callback)
|
35
|
+
```
|
36
|
+
|
37
|
+
## Contributing
|
38
|
+
|
39
|
+
1. Fork it
|
40
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
41
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
42
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
43
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/em-worker.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "em-worker"
|
7
|
+
spec.version = "0.0.1"
|
8
|
+
spec.authors = ["Sean Porter"]
|
9
|
+
spec.email = ["portertech@gmail.com"]
|
10
|
+
spec.description = "Provides a simple task worker, with a task concurrency limit."
|
11
|
+
spec.summary = "Provides a simple task worker, with a task concurrency limit."
|
12
|
+
spec.homepage = "https://github.com/portertech/em-worker"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files`.split($/)
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "eventmachine"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
23
|
+
spec.add_development_dependency "rspec"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
end
|
data/lib/em/worker.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
require "eventmachine"
|
2
|
+
|
3
|
+
module EM
|
4
|
+
class Worker
|
5
|
+
def initialize(options={})
|
6
|
+
@task_queue = EM::Queue.new
|
7
|
+
@concurrency = options.fetch(:concurrency, 10)
|
8
|
+
start
|
9
|
+
end
|
10
|
+
|
11
|
+
def enqueue(task=nil, callback=nil, options={}, &block)
|
12
|
+
task ||= block || Proc.new {}
|
13
|
+
@task_queue.push([task, callback, options])
|
14
|
+
true
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def do_task
|
20
|
+
@task_queue.pop do |task, callback, options|
|
21
|
+
EM.defer(task, Proc.new { |result| callback.call(result) if callback; do_task })
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def start
|
26
|
+
@concurrency.times do
|
27
|
+
EM.next_tick do
|
28
|
+
do_task
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module AsyncHelpers
|
2
|
+
def timer(delay, &block)
|
3
|
+
periodic_timer = EM::PeriodicTimer.new(delay) do
|
4
|
+
block.call
|
5
|
+
periodic_timer.cancel
|
6
|
+
end
|
7
|
+
end
|
8
|
+
|
9
|
+
def async_wrapper(&block)
|
10
|
+
EM::run do
|
11
|
+
timer(10) do
|
12
|
+
raise "test timed out"
|
13
|
+
end
|
14
|
+
block.call
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def async_done
|
19
|
+
EM::stop_event_loop
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
2
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
3
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
4
|
+
# loaded once.
|
5
|
+
#
|
6
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
7
|
+
RSpec.configure do |config|
|
8
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
9
|
+
config.run_all_when_everything_filtered = true
|
10
|
+
config.filter_run :focus
|
11
|
+
|
12
|
+
# Run specs in random order to surface order dependencies. If you find an
|
13
|
+
# order dependency and want to debug it, you can fix the order by providing
|
14
|
+
# the seed, which is printed after each run.
|
15
|
+
# --seed 1234
|
16
|
+
config.order = 'random'
|
17
|
+
end
|
data/spec/worker_spec.rb
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "async_helpers"
|
3
|
+
require File.dirname(__FILE__) + "/../lib/em/worker.rb"
|
4
|
+
|
5
|
+
describe "EM::Worker" do
|
6
|
+
include AsyncHelpers
|
7
|
+
|
8
|
+
before do
|
9
|
+
@worker = EM::Worker.new
|
10
|
+
end
|
11
|
+
|
12
|
+
it "can enqueue a task" do
|
13
|
+
async_wrapper do
|
14
|
+
@worker.enqueue do
|
15
|
+
puts "winning"
|
16
|
+
end.should eql true
|
17
|
+
async_done
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
it "does work" do
|
22
|
+
async_wrapper do
|
23
|
+
task = Proc.new { "tiger blood" }
|
24
|
+
callback = Proc.new do |result|
|
25
|
+
result.should eql "tiger blood"
|
26
|
+
async_done
|
27
|
+
end
|
28
|
+
@worker.enqueue(task, callback)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
it "will do a maximum of 10 tasks at a time by default" do
|
33
|
+
@concurrency = Queue.new
|
34
|
+
async_wrapper do
|
35
|
+
20.times do
|
36
|
+
@worker.enqueue do
|
37
|
+
@concurrency << 1
|
38
|
+
sleep 1000
|
39
|
+
end
|
40
|
+
end
|
41
|
+
timer(1) do
|
42
|
+
@concurrency.size.should eql 10
|
43
|
+
async_done
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
it "will complete tasks in the correct order" do
|
49
|
+
@results = []
|
50
|
+
async_wrapper do
|
51
|
+
1000.times do |i|
|
52
|
+
task = Proc.new { i }
|
53
|
+
callback = Proc.new do |result|
|
54
|
+
@results << result
|
55
|
+
end
|
56
|
+
@worker.enqueue(task, callback)
|
57
|
+
end
|
58
|
+
timer(2) do
|
59
|
+
@results.size.should eql 1000
|
60
|
+
@results.should eql @results.sort
|
61
|
+
async_done
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: em-worker
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sean Porter
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2013-12-15 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: eventmachine
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.3'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ~>
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.3'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
description: Provides a simple task worker, with a task concurrency limit.
|
70
|
+
email:
|
71
|
+
- portertech@gmail.com
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- .gitignore
|
77
|
+
- .rspec
|
78
|
+
- .travis.yml
|
79
|
+
- Gemfile
|
80
|
+
- LICENSE.txt
|
81
|
+
- README.md
|
82
|
+
- Rakefile
|
83
|
+
- em-worker.gemspec
|
84
|
+
- lib/em/worker.rb
|
85
|
+
- spec/async_helpers.rb
|
86
|
+
- spec/spec_helper.rb
|
87
|
+
- spec/worker_spec.rb
|
88
|
+
homepage: https://github.com/portertech/em-worker
|
89
|
+
licenses:
|
90
|
+
- MIT
|
91
|
+
metadata: {}
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
requirements:
|
98
|
+
- - '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
102
|
+
requirements:
|
103
|
+
- - '>='
|
104
|
+
- !ruby/object:Gem::Version
|
105
|
+
version: '0'
|
106
|
+
requirements: []
|
107
|
+
rubyforge_project:
|
108
|
+
rubygems_version: 2.0.6
|
109
|
+
signing_key:
|
110
|
+
specification_version: 4
|
111
|
+
summary: Provides a simple task worker, with a task concurrency limit.
|
112
|
+
test_files:
|
113
|
+
- spec/async_helpers.rb
|
114
|
+
- spec/spec_helper.rb
|
115
|
+
- spec/worker_spec.rb
|