sideband 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +29 -0
- data/Rakefile +10 -0
- data/lib/sideband/manager.rb +48 -0
- data/lib/sideband/queue.rb +24 -0
- data/lib/sideband/thread.rb +30 -0
- data/lib/sideband/version.rb +3 -0
- data/lib/sideband/worker.rb +14 -0
- data/lib/sideband.rb +48 -0
- data/sideband.gemspec +19 -0
- data/test/helper.rb +15 -0
- data/test/test_manager.rb +49 -0
- data/test/test_queue.rb +22 -0
- data/test/test_sideband.rb +38 -0
- data/test/test_thread.rb +32 -0
- data/test/test_worker.rb +24 -0
- metadata +70 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Mike Evans
|
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
|
+
# Sideband
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sideband'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sideband
|
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
@@ -0,0 +1,48 @@
|
|
1
|
+
module Sideband
|
2
|
+
class Manager
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@pid = ::Process.pid
|
6
|
+
thread!
|
7
|
+
queue!
|
8
|
+
end
|
9
|
+
|
10
|
+
def queue
|
11
|
+
handle_fork
|
12
|
+
@queue
|
13
|
+
end
|
14
|
+
|
15
|
+
def thread
|
16
|
+
@thread
|
17
|
+
end
|
18
|
+
|
19
|
+
def join
|
20
|
+
@queue.kill
|
21
|
+
@thread.join
|
22
|
+
end
|
23
|
+
|
24
|
+
def kill
|
25
|
+
@thread.kill
|
26
|
+
@thread = @queue = nil
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def queue!
|
32
|
+
@queue = Sideband::Queue.new
|
33
|
+
end
|
34
|
+
|
35
|
+
def thread!
|
36
|
+
@thread.kill if @thread
|
37
|
+
@thread = Sideband::Thread.new(self)
|
38
|
+
end
|
39
|
+
|
40
|
+
def handle_fork
|
41
|
+
if ::Process.pid != @pid
|
42
|
+
@pid = ::Process.pid
|
43
|
+
thread!
|
44
|
+
queue!
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Sideband
|
2
|
+
class Queue
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@queue = ::Queue.new
|
6
|
+
end
|
7
|
+
|
8
|
+
def kill
|
9
|
+
@queue << nil
|
10
|
+
end
|
11
|
+
|
12
|
+
def push(work)
|
13
|
+
return false if work.nil?
|
14
|
+
@queue.push(work)
|
15
|
+
true
|
16
|
+
end
|
17
|
+
alias_method :<<, :push
|
18
|
+
|
19
|
+
def pop
|
20
|
+
@queue.pop
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Sideband
|
2
|
+
class Thread
|
3
|
+
|
4
|
+
attr_reader :thread
|
5
|
+
|
6
|
+
def initialize(manager)
|
7
|
+
@manager = manager
|
8
|
+
@thread = ::Thread.new do
|
9
|
+
while work = @manager.queue.pop
|
10
|
+
exit if work.nil?
|
11
|
+
|
12
|
+
begin
|
13
|
+
work.call
|
14
|
+
rescue Exception
|
15
|
+
# Sideband will ignore all Exceptions,
|
16
|
+
# better to handle in your workers.
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def join
|
23
|
+
thread.join
|
24
|
+
end
|
25
|
+
|
26
|
+
def kill
|
27
|
+
thread.kill
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/sideband.rb
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'thread'
|
2
|
+
require 'sideband/version'
|
3
|
+
require 'sideband/manager'
|
4
|
+
require 'sideband/queue'
|
5
|
+
require 'sideband/thread'
|
6
|
+
require 'sideband/worker'
|
7
|
+
|
8
|
+
module Sideband
|
9
|
+
|
10
|
+
class NotInitializedError < Exception; end
|
11
|
+
|
12
|
+
def self.initialize!
|
13
|
+
new_manager = Manager.new
|
14
|
+
puts 'Sideband initialized!'
|
15
|
+
|
16
|
+
if block_given?
|
17
|
+
begin
|
18
|
+
::Thread.current['sideband.manager'] = new_manager
|
19
|
+
yield
|
20
|
+
ensure
|
21
|
+
join
|
22
|
+
end
|
23
|
+
else
|
24
|
+
::Thread.current['sideband.manager'] = new_manager
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def self.join
|
29
|
+
manager.join
|
30
|
+
kill
|
31
|
+
end
|
32
|
+
|
33
|
+
def self.kill
|
34
|
+
manager.kill
|
35
|
+
::Thread.current['sideband.manager'] = nil
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.queue
|
39
|
+
manager.queue
|
40
|
+
end
|
41
|
+
|
42
|
+
def self.manager
|
43
|
+
manager = ::Thread.current['sideband.manager']
|
44
|
+
raise NotInitializedError.new('Sideband must be initialized! before using.') if manager.nil?
|
45
|
+
manager
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
data/sideband.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 'sideband/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = 'sideband'
|
8
|
+
gem.version = Sideband::VERSION
|
9
|
+
gem.authors = ['Mike Evans']
|
10
|
+
gem.email = ['mike@urlgonomics.com']
|
11
|
+
gem.description = %q{Run simple workers in a separate thread}
|
12
|
+
gem.summary = %q{Run simple workers in a separate thread}
|
13
|
+
gem.homepage = 'https://github.com/mje113/sideband'
|
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
|
data/test/helper.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestManager < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@manager = Sideband::Manager.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_has_queue
|
10
|
+
assert_kind_of Sideband::Queue, @manager.queue
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_has_thread
|
14
|
+
assert_kind_of Sideband::Thread, @manager.thread
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_can_queue_and_process_proc
|
18
|
+
work = 'work'
|
19
|
+
@manager.queue << -> { work = 'finished' }
|
20
|
+
sleep 0.1
|
21
|
+
assert_equal 'finished', work
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_can_queue_and_process_worker
|
25
|
+
worker = EmailWorker.new
|
26
|
+
assert_equal 'work', worker.work
|
27
|
+
@manager.queue << worker
|
28
|
+
sleep 0.1
|
29
|
+
assert_equal 'finished', worker.work
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_fork_handling
|
33
|
+
queue = @manager.queue
|
34
|
+
thread = @manager.thread
|
35
|
+
|
36
|
+
Process.stub(:pid, Process.pid + 1) do
|
37
|
+
@manager.queue << -> { 'work' }
|
38
|
+
refute_equal queue, @manager.queue
|
39
|
+
refute_equal thread, @manager.thread
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_killed
|
44
|
+
@manager.kill
|
45
|
+
assert_nil @manager.queue
|
46
|
+
assert_nil @manager.thread
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
data/test/test_queue.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestQueue < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@queue = Sideband::Queue.new
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_killed
|
10
|
+
@queue.kill
|
11
|
+
assert_nil @queue.pop
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_can_push_and_pop
|
15
|
+
assert_equal true, @queue.push(:a)
|
16
|
+
assert_equal :a, @queue.pop
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_nil_blocked
|
20
|
+
refute @queue << nil
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestSideband < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def test_must_be_initialized_before_use
|
6
|
+
assert_raises Sideband::NotInitializedError do
|
7
|
+
Sideband.queue << -> { 'work' }
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_has_queue
|
12
|
+
Sideband.initialize! do
|
13
|
+
assert_kind_of Sideband::Queue, Sideband.queue
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_manager_stored_in_thread_current
|
18
|
+
Sideband.initialize! do
|
19
|
+
assert_kind_of Sideband::Manager, ::Thread.current['sideband.manager']
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_can_be_used_in_separate_threads
|
24
|
+
work_a, work_b = 'work', 'work'
|
25
|
+
Sideband.initialize! do
|
26
|
+
Sideband.queue << -> { work_a = 'finished' }
|
27
|
+
|
28
|
+
Thread.new {
|
29
|
+
Sideband.initialize! do
|
30
|
+
Sideband.queue << -> { work_b = 'finished' }
|
31
|
+
end
|
32
|
+
}.join
|
33
|
+
end
|
34
|
+
|
35
|
+
assert_equal 'finished', work_a
|
36
|
+
assert_equal 'finished', work_b
|
37
|
+
end
|
38
|
+
end
|
data/test/test_thread.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestThread < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def setup
|
6
|
+
@manager = OpenStruct.new(queue: OpenStruct.new)
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_initialized
|
10
|
+
thread = Sideband::Thread.new(@manager)
|
11
|
+
assert_kind_of ::Thread, thread.thread
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_killed
|
15
|
+
thread = Sideband::Thread.new(@manager)
|
16
|
+
thread.kill
|
17
|
+
sleep 0.1
|
18
|
+
refute thread.thread.alive?
|
19
|
+
end
|
20
|
+
|
21
|
+
def test_joined
|
22
|
+
work = 'work'
|
23
|
+
@manager.queue = Queue.new
|
24
|
+
@manager.queue << -> { work = 'finished' }
|
25
|
+
@manager.queue << nil
|
26
|
+
thread = Sideband::Thread.new(@manager)
|
27
|
+
|
28
|
+
thread.join
|
29
|
+
refute thread.thread.alive?
|
30
|
+
assert_equal 'finished', work
|
31
|
+
end
|
32
|
+
end
|
data/test/test_worker.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
require 'helper'
|
2
|
+
|
3
|
+
class TestWorker < MiniTest::Unit::TestCase
|
4
|
+
|
5
|
+
def test_can_queue_itself
|
6
|
+
Sideband.initialize! do
|
7
|
+
assert Sideband::Worker.new.enqueue
|
8
|
+
assert Sideband::Worker.new.queue
|
9
|
+
assert EmailWorker.new.enqueue
|
10
|
+
assert EmailWorker.new.queue
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_raises_error_if_call_not_implemented
|
15
|
+
assert_raises NotImplementedError do
|
16
|
+
Sideband::Worker.new.call
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_call_implemented
|
21
|
+
assert_equal 'finished', EmailWorker.new.call
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,70 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sideband
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mike Evans
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-02-08 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Run simple workers in a separate thread
|
15
|
+
email:
|
16
|
+
- mike@urlgonomics.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/sideband.rb
|
27
|
+
- lib/sideband/manager.rb
|
28
|
+
- lib/sideband/queue.rb
|
29
|
+
- lib/sideband/thread.rb
|
30
|
+
- lib/sideband/version.rb
|
31
|
+
- lib/sideband/worker.rb
|
32
|
+
- sideband.gemspec
|
33
|
+
- test/helper.rb
|
34
|
+
- test/test_manager.rb
|
35
|
+
- test/test_queue.rb
|
36
|
+
- test/test_sideband.rb
|
37
|
+
- test/test_thread.rb
|
38
|
+
- test/test_worker.rb
|
39
|
+
homepage: https://github.com/mje113/sideband
|
40
|
+
licenses: []
|
41
|
+
post_install_message:
|
42
|
+
rdoc_options: []
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
none: false
|
47
|
+
requirements:
|
48
|
+
- - ! '>='
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '0'
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
none: false
|
53
|
+
requirements:
|
54
|
+
- - ! '>='
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
requirements: []
|
58
|
+
rubyforge_project:
|
59
|
+
rubygems_version: 1.8.24
|
60
|
+
signing_key:
|
61
|
+
specification_version: 3
|
62
|
+
summary: Run simple workers in a separate thread
|
63
|
+
test_files:
|
64
|
+
- test/helper.rb
|
65
|
+
- test/test_manager.rb
|
66
|
+
- test/test_queue.rb
|
67
|
+
- test/test_sideband.rb
|
68
|
+
- test/test_thread.rb
|
69
|
+
- test/test_worker.rb
|
70
|
+
has_rdoc:
|