workers 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +13 -0
- data/lib/workers/.pool.rb.swp +0 -0
- data/lib/workers/.worker.rb.swp +0 -0
- data/lib/workers/event.rb +11 -0
- data/lib/workers/helpers.rb +23 -0
- data/lib/workers/log_proxy.rb +25 -0
- data/lib/workers/pool.rb +37 -0
- data/lib/workers/version.rb +3 -0
- data/lib/workers/worker.rb +69 -0
- data/lib/workers.rb +8 -0
- data/workers.gemspec +19 -0
- metadata +61 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Chad Remesch
|
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,29 @@
|
|
1
|
+
# Workers
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'workers'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install workers
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
Binary file
|
Binary file
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module Workers
|
2
|
+
module Helpers
|
3
|
+
def log_debug(msg, e = nil)
|
4
|
+
@logger.debug(concat_e(msg, e))
|
5
|
+
end
|
6
|
+
|
7
|
+
def log_info(msg, e = nil)
|
8
|
+
@logger.info(concat_e(msg, e))
|
9
|
+
end
|
10
|
+
|
11
|
+
def log_warn(msg, e = nil)
|
12
|
+
@logger.warn(concat_e(msg, e))
|
13
|
+
end
|
14
|
+
|
15
|
+
def log_error(msg, e = nil)
|
16
|
+
@logger.error(concat_e(msg, e))
|
17
|
+
end
|
18
|
+
|
19
|
+
def concat_e(msg, e = nil)
|
20
|
+
return e ? "#{msg}\nEXCEPTION: #{e.message}\n#{e.backtrace.join("\n")}\n--" : msg
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module Workers
|
2
|
+
class LogProxy
|
3
|
+
attr_accessor :logger
|
4
|
+
|
5
|
+
def initialize(logger)
|
6
|
+
@logger = logger
|
7
|
+
end
|
8
|
+
|
9
|
+
def debug(msg)
|
10
|
+
@logger.debug(msg) if @logger
|
11
|
+
end
|
12
|
+
|
13
|
+
def info(msg)
|
14
|
+
@logger.info(msg) if @logger
|
15
|
+
end
|
16
|
+
|
17
|
+
def warn(msg)
|
18
|
+
@logger.warn(msg) if @logger
|
19
|
+
end
|
20
|
+
|
21
|
+
def error(msg)
|
22
|
+
@logger.error(msg) if @logger
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/lib/workers/pool.rb
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
module Workers
|
2
|
+
class Pool
|
3
|
+
include Workers::Helpers
|
4
|
+
|
5
|
+
DEFAULT_POOL_SIZE = 20
|
6
|
+
|
7
|
+
def initialize(options = {})
|
8
|
+
@size = options[:size] || Workers::Pool::DEFAULT_POOL_SIZE
|
9
|
+
@logger = Workers::LogProxy.new(options[:logger])
|
10
|
+
@input_queue = Queue.new
|
11
|
+
@workers = []
|
12
|
+
@size.times { @workers << Workers::Worker.new(:input_queue => @input_queue) }
|
13
|
+
end
|
14
|
+
|
15
|
+
def perform(options = {}, &block)
|
16
|
+
enqueue(:perform, block)
|
17
|
+
|
18
|
+
return nil
|
19
|
+
end
|
20
|
+
|
21
|
+
def shutdown(options = {}, &block)
|
22
|
+
@size.times { enqueue(:shutdown, block) }
|
23
|
+
|
24
|
+
return nil
|
25
|
+
end
|
26
|
+
|
27
|
+
def join(max_wait = nil)
|
28
|
+
return @workers.map { |w| w.join(max_wait) }
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def enqueue(command, data)
|
34
|
+
@input_queue.push(Event.new(command, data))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module Workers
|
2
|
+
class Worker
|
3
|
+
include Workers::Helpers
|
4
|
+
|
5
|
+
def initialize(options = {})
|
6
|
+
@logger = Workers::LogProxy.new(options[:logger])
|
7
|
+
@input_queue = options[:input_queue] || Queue.new
|
8
|
+
@thread = Thread.new { start_event_loop }
|
9
|
+
end
|
10
|
+
|
11
|
+
def perform(options = {}, &block)
|
12
|
+
enqueue(:perform, block)
|
13
|
+
|
14
|
+
return nil
|
15
|
+
end
|
16
|
+
|
17
|
+
def shutdown(options = {}, &block)
|
18
|
+
enqueue(:shutdown, block)
|
19
|
+
|
20
|
+
return nil
|
21
|
+
end
|
22
|
+
|
23
|
+
def join(max_wait = nil)
|
24
|
+
raise "Worker can't join itself!" if @thread == Thread.current
|
25
|
+
|
26
|
+
return true if !@thread.join(max_wait).nil?
|
27
|
+
|
28
|
+
@thread.kill and return false
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def enqueue(command, data)
|
34
|
+
@input_queue.push(Event.new(command, data))
|
35
|
+
end
|
36
|
+
|
37
|
+
def start_event_loop
|
38
|
+
while true
|
39
|
+
event = @input_queue.pop # Blocking.
|
40
|
+
|
41
|
+
case event.command
|
42
|
+
when :shutdown
|
43
|
+
try_callback(event.data) do |e|
|
44
|
+
log_error("Worker failed run 'shutdown' callback.", e)
|
45
|
+
end
|
46
|
+
return
|
47
|
+
when :perform
|
48
|
+
try_callback(event.data) do |e|
|
49
|
+
log_error("Worker failed run 'perform' callback.", e)
|
50
|
+
end
|
51
|
+
else
|
52
|
+
process_event(event)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def try_callback(callback, &block)
|
58
|
+
begin
|
59
|
+
callback.call
|
60
|
+
rescue Exception => e
|
61
|
+
block.call(e)
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def process_event(event)
|
66
|
+
raise 'Subclass and override if you need custom commands.'
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/workers.rb
ADDED
data/workers.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'workers/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "workers"
|
8
|
+
gem.version = Workers::VERSION
|
9
|
+
gem.authors = ["Chad Remesch"]
|
10
|
+
gem.email = ["chad@remesch.com"]
|
11
|
+
gem.description = %q{Simple to use Ruby workers for performing work in background threads.}
|
12
|
+
gem.summary = %q{Simple to use Ruby workers featuring high performance, simple to use API, customizable workers, and thread pooling.}
|
13
|
+
gem.homepage = "https://github.com/chadrem/workers"
|
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
|
+
end
|
metadata
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: workers
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Chad Remesch
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-12 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Simple to use Ruby workers for performing work in background threads.
|
15
|
+
email:
|
16
|
+
- chad@remesch.com
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- Gemfile
|
23
|
+
- LICENSE.txt
|
24
|
+
- README.md
|
25
|
+
- Rakefile
|
26
|
+
- lib/workers.rb
|
27
|
+
- lib/workers/.pool.rb.swp
|
28
|
+
- lib/workers/.worker.rb.swp
|
29
|
+
- lib/workers/event.rb
|
30
|
+
- lib/workers/helpers.rb
|
31
|
+
- lib/workers/log_proxy.rb
|
32
|
+
- lib/workers/pool.rb
|
33
|
+
- lib/workers/version.rb
|
34
|
+
- lib/workers/worker.rb
|
35
|
+
- workers.gemspec
|
36
|
+
homepage: https://github.com/chadrem/workers
|
37
|
+
licenses: []
|
38
|
+
post_install_message:
|
39
|
+
rdoc_options: []
|
40
|
+
require_paths:
|
41
|
+
- lib
|
42
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
43
|
+
none: false
|
44
|
+
requirements:
|
45
|
+
- - ! '>='
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
requirements: []
|
55
|
+
rubyforge_project:
|
56
|
+
rubygems_version: 1.8.24
|
57
|
+
signing_key:
|
58
|
+
specification_version: 3
|
59
|
+
summary: Simple to use Ruby workers featuring high performance, simple to use API,
|
60
|
+
customizable workers, and thread pooling.
|
61
|
+
test_files: []
|