gb_dispatch 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.idea/.name +1 -0
- data/.idea/encodings.xml +6 -0
- data/.idea/misc.xml +14 -0
- data/.idea/modules.xml +8 -0
- data/.idea/vcs.xml +6 -0
- data/.rspec +3 -0
- data/Gemfile +10 -0
- data/Gemfile.lock +45 -0
- data/LICENSE.txt +22 -0
- data/README.md +45 -0
- data/Rakefile +18 -0
- data/gb_dispatch.gemspec +24 -0
- data/lib/gb_dispatch/central_dispatch.rb +20 -0
- data/lib/gb_dispatch/manager.rb +34 -0
- data/lib/gb_dispatch/queue.rb +43 -0
- data/lib/gb_dispatch/runner.rb +31 -0
- data/lib/gb_dispatch/version.rb +3 -0
- data/lib/gb_dispatch.rb +47 -0
- data/spec/queue_spec.rb +135 -0
- data/spec/runner_spec.rb +53 -0
- data/spec/spec_helper.rb +104 -0
- metadata +111 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 2d9b3773249dedae96894b9cc9addb02ca59c1e2
|
4
|
+
data.tar.gz: c3ee56270eb83c3cf0e68dc0568e450fd718c8c6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: cf2c44fe50c484e501bd7d89deb51ee47c0349bae1ec9c76f55329600336ea9cddfc5d10456b7ef78a591b7a8e7781465ab9524857ca0c827bf1978ad440a894
|
7
|
+
data.tar.gz: 796181ec676e815b3c57266d58d6224f9fd20d5033c86b7b1b83f7d8c65c55a370f8bb556418ca68de48ea105c4f081be910bda220a6e203d29ed2f43ad507e2
|
data/.gitignore
ADDED
data/.idea/.name
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
gb_dispatch
|
data/.idea/encodings.xml
ADDED
data/.idea/misc.xml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
|
4
|
+
<OptionsSetting value="true" id="Add" />
|
5
|
+
<OptionsSetting value="true" id="Remove" />
|
6
|
+
<OptionsSetting value="true" id="Checkout" />
|
7
|
+
<OptionsSetting value="true" id="Update" />
|
8
|
+
<OptionsSetting value="true" id="Status" />
|
9
|
+
<OptionsSetting value="true" id="Edit" />
|
10
|
+
<ConfirmationsSetting value="0" id="Add" />
|
11
|
+
<ConfirmationsSetting value="0" id="Remove" />
|
12
|
+
</component>
|
13
|
+
<component name="ProjectRootManager" version="2" project-jdk-name="RVM: ruby-1.9.3-p551 [global]" project-jdk-type="RUBY_SDK" />
|
14
|
+
</project>
|
data/.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<project version="4">
|
3
|
+
<component name="ProjectModuleManager">
|
4
|
+
<modules>
|
5
|
+
<module fileurl="file://$PROJECT_DIR$/.idea/gb_dispatch.iml" filepath="$PROJECT_DIR$/.idea/gb_dispatch.iml" />
|
6
|
+
</modules>
|
7
|
+
</component>
|
8
|
+
</project>
|
data/.idea/vcs.xml
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
gb_dispatch (0.0.1)
|
5
|
+
celluloid (~> 0.15.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
celluloid (0.15.2)
|
11
|
+
timers (~> 1.1.0)
|
12
|
+
diff-lcs (1.2.5)
|
13
|
+
docile (1.1.5)
|
14
|
+
json (1.8.3)
|
15
|
+
rake (10.3.2)
|
16
|
+
rspec (3.4.0)
|
17
|
+
rspec-core (~> 3.4.0)
|
18
|
+
rspec-expectations (~> 3.4.0)
|
19
|
+
rspec-mocks (~> 3.4.0)
|
20
|
+
rspec-core (3.4.1)
|
21
|
+
rspec-support (~> 3.4.0)
|
22
|
+
rspec-expectations (3.4.0)
|
23
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
24
|
+
rspec-support (~> 3.4.0)
|
25
|
+
rspec-mocks (3.4.1)
|
26
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
27
|
+
rspec-support (~> 3.4.0)
|
28
|
+
rspec-support (3.4.1)
|
29
|
+
simplecov (0.10.0)
|
30
|
+
docile (~> 1.1.0)
|
31
|
+
json (~> 1.8)
|
32
|
+
simplecov-html (~> 0.10.0)
|
33
|
+
simplecov-html (0.10.0)
|
34
|
+
timers (1.1.0)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
bundler (~> 1.6)
|
41
|
+
celluloid (~> 0.15.2)
|
42
|
+
gb_dispatch!
|
43
|
+
rake (~> 10.0)
|
44
|
+
rspec
|
45
|
+
simplecov
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Kacper Kawecki
|
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,45 @@
|
|
1
|
+
# GbDispatch
|
2
|
+
|
3
|
+
Library allows easy to dispatch block of code for queues.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'gb_dispatch'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
$ bundle
|
16
|
+
|
17
|
+
Or install it yourself as:
|
18
|
+
|
19
|
+
$ gem install gb_dispatch
|
20
|
+
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
To dispatch asynchronously
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
GBDispatch.dispatch_async_on_queue :my_queue do
|
27
|
+
puts 'my code here'
|
28
|
+
end
|
29
|
+
```
|
30
|
+
|
31
|
+
for synchronous execution
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
GBDispatch.dispatch_sync_on_queue :my_queue do
|
35
|
+
puts 'my code here'
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
## Contributing
|
40
|
+
|
41
|
+
1. Fork it ( https://github.com/[my-github-username]/gb_dispatch/fork )
|
42
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
43
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
44
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
45
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
#require 'bundler/gem_tasks'
|
2
|
+
begin
|
3
|
+
require 'rspec/core/rake_task'
|
4
|
+
RSpec::Core::RakeTask.new(:spec)
|
5
|
+
|
6
|
+
task :default => :spec
|
7
|
+
rescue LoadError
|
8
|
+
end
|
9
|
+
|
10
|
+
task :build => :spec do
|
11
|
+
sh 'gem build ./gb_dispatch.gemspec'
|
12
|
+
end
|
13
|
+
|
14
|
+
task :release do
|
15
|
+
require './lib/gb_dispatch/version'
|
16
|
+
sh "gem push gb_dispatch-#{GBDispatch::VERSION}.gem --host http://tools.bobisdead.com/gems"
|
17
|
+
end
|
18
|
+
|
data/gb_dispatch.gemspec
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'gb_dispatch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'gb_dispatch'
|
8
|
+
spec.version = GBDispatch::VERSION
|
9
|
+
spec.authors = ['Kacper Kawecki']
|
10
|
+
spec.email = ['kacper@geniebelt.com']
|
11
|
+
spec.summary = %q{GCD emulation for ruby}
|
12
|
+
spec.description = %q{Allows to dispatch block of code for queues}
|
13
|
+
spec.homepage = 'https://github.com/GenieBelt/gb-dispatch'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'celluloid', '~> 0.15.2'
|
22
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
23
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
24
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'gb_dispatch/manager'
|
3
|
+
class CentralDispatch
|
4
|
+
include Singleton
|
5
|
+
|
6
|
+
def dispatch_async(block=nil)
|
7
|
+
GBDispatch::Manager.instance.run_async_on_queue GBDispatch::Manager.instance.get_queue do
|
8
|
+
block ? block.call : yield
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class << self
|
13
|
+
def dispatch_async(block=nil)
|
14
|
+
GBDispatch::Manager.instance.run_async_on_queue GBDispatch::Manager.instance.get_queue do
|
15
|
+
block ? block.call : yield
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'gb_dispatch/runner'
|
3
|
+
require 'gb_dispatch/queue'
|
4
|
+
module GBDispatch
|
5
|
+
class Manager
|
6
|
+
include Singleton
|
7
|
+
|
8
|
+
def get_queue(name=:default)
|
9
|
+
queue = Celluloid::Actor[name.to_sym]
|
10
|
+
unless queue
|
11
|
+
supervisor = Queue.supervise_as name, name, @pool
|
12
|
+
queue = supervisor.actors.first
|
13
|
+
end
|
14
|
+
queue
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param queue [GBDispatch::Queue]
|
18
|
+
def run_async_on_queue(queue)
|
19
|
+
queue.async.perform ->() { yield }
|
20
|
+
end
|
21
|
+
|
22
|
+
# @param queue [GBDispatch::Queue]
|
23
|
+
def run_sync_on_queue(queue)
|
24
|
+
future = queue.future.perform ->() { yield }
|
25
|
+
future.value
|
26
|
+
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def initialize
|
31
|
+
@pool = Runner.pool
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'celluloid'
|
2
|
+
module GBDispatch
|
3
|
+
class Queue
|
4
|
+
include Celluloid
|
5
|
+
|
6
|
+
# @return [String] queue name
|
7
|
+
attr_reader :name
|
8
|
+
|
9
|
+
# @param name [String] queue name, should be the same as is register in Celluloid
|
10
|
+
# @param thread_pool [Celluloid::Pool] pool of runners for executing code.
|
11
|
+
def initialize(name, thread_pool)
|
12
|
+
@name = name
|
13
|
+
@thread_pool = thread_pool
|
14
|
+
end
|
15
|
+
|
16
|
+
#
|
17
|
+
# @param block [Proc]
|
18
|
+
# @yield if there is no block given it yield without param.
|
19
|
+
def perform(block=nil)
|
20
|
+
Thread.current[:name] ||= name
|
21
|
+
if defined?(Rails) && defined?(ActiveRecord::Base)
|
22
|
+
thread_block = ->() do
|
23
|
+
begin
|
24
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
25
|
+
block ? block.call : yield
|
26
|
+
end
|
27
|
+
ensure
|
28
|
+
ActiveRecord::Base.clear_active_connections!
|
29
|
+
end
|
30
|
+
end
|
31
|
+
else
|
32
|
+
thread_block = block ? block : ->() { yield }
|
33
|
+
end
|
34
|
+
exclusive do
|
35
|
+
begin
|
36
|
+
@thread_pool.execute thread_block, name: name
|
37
|
+
rescue Exception => e
|
38
|
+
return e
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'celluloid'
|
2
|
+
module GBDispatch
|
3
|
+
class Runner
|
4
|
+
include Celluloid
|
5
|
+
|
6
|
+
# Execute given block.
|
7
|
+
# If there is an exception thrown, it log it and crash actor.
|
8
|
+
# For more information about error handling, check Celluloid documentation.
|
9
|
+
# @param block [Proc]
|
10
|
+
# @param options [Hash]
|
11
|
+
# @option options [Proc] :condition celluloid condition block called on finish with result
|
12
|
+
# @option options [String] :name queue name used for debugging and better logging.
|
13
|
+
def execute(block, options=Hash.new)
|
14
|
+
begin
|
15
|
+
condition = options[:condition]
|
16
|
+
result = block.call
|
17
|
+
condition.call(result) if condition
|
18
|
+
result
|
19
|
+
rescue Exception => e
|
20
|
+
name = options[:name]
|
21
|
+
if defined?(Opbeat)
|
22
|
+
Opbeat.set_context extra: {queue: name} if name
|
23
|
+
Opbeat.capture_exception(e)
|
24
|
+
end
|
25
|
+
Celluloid.logger.error "Failed execution of queue #{name} with error #{e.message}"
|
26
|
+
condition.call(e) if condition
|
27
|
+
raise e
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
data/lib/gb_dispatch.rb
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'gb_dispatch/version'
|
2
|
+
require 'gb_dispatch/manager'
|
3
|
+
|
4
|
+
# Library to dispatch block on queues.
|
5
|
+
# It is inspired by GCD but implementation is based on Celluloid.
|
6
|
+
# Current implementation ensure that block on queue will be executed in the same order
|
7
|
+
# as added, however it doesn't ensure on which thread it will run.
|
8
|
+
#
|
9
|
+
# == Threading
|
10
|
+
# Each queue have it is own thread, but their only for synchronisation.
|
11
|
+
# All execution happens on runners thread pool. Pool size is limited to number of cores
|
12
|
+
# of the machine.
|
13
|
+
#
|
14
|
+
module GBDispatch
|
15
|
+
# Dispatch asynchronously on queue
|
16
|
+
# @param queue [Symbol, GBDispatch::Queue] queue object or name
|
17
|
+
# @yield block to execute
|
18
|
+
def self.dispatch_async_on_queue(queue)
|
19
|
+
queue = GBDispatch::Manager.instance.get_queue(queue) unless queue.is_a? GBDispatch::Queue
|
20
|
+
GBDispatch::Manager.instance.run_async_on_queue queue do
|
21
|
+
yield
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# Get queue of given name
|
26
|
+
# @return [GBDispatch:Queue]
|
27
|
+
# @param name [#to_sym]
|
28
|
+
def self.get_queue(name)
|
29
|
+
GBDispatch::Manager.instance.get_queue(name)
|
30
|
+
end
|
31
|
+
|
32
|
+
# Dispatch synchronously on queue and return result
|
33
|
+
# @param queue [Symbol, GBDispatch::Queue] queue object or name
|
34
|
+
# @yield block to execute
|
35
|
+
def self.dispatch_sync_on_queue(queue)
|
36
|
+
queue = GBDispatch::Manager.instance.get_queue(queue) unless queue.is_a? GBDispatch::Queue
|
37
|
+
GBDispatch::Manager.instance.run_async_on_queue queue do
|
38
|
+
yield
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
# Setup logger. By default it use Celluloid logger
|
43
|
+
# @param logger [Logger]
|
44
|
+
def self.logger=(logger)
|
45
|
+
Celluloid.logger = logger
|
46
|
+
end
|
47
|
+
end
|
data/spec/queue_spec.rb
ADDED
@@ -0,0 +1,135 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'gb_dispatch/queue'
|
3
|
+
require 'gb_dispatch/runner'
|
4
|
+
|
5
|
+
|
6
|
+
describe GBDispatch::Queue do
|
7
|
+
before(:all) { @pool = GBDispatch::Runner.pool }
|
8
|
+
after(:all) { @pool.terminate }
|
9
|
+
|
10
|
+
context 'synchronous' do
|
11
|
+
it 'should execute lambda' do
|
12
|
+
a = :foo
|
13
|
+
queue = GBDispatch::Queue.new(:test, @pool)
|
14
|
+
queue.perform ->() { a = :bar }
|
15
|
+
expect(a).to eq :bar
|
16
|
+
queue.terminate
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should execute block' do
|
20
|
+
a = :foo
|
21
|
+
queue = GBDispatch::Queue.new(:test, @pool)
|
22
|
+
queue.perform do
|
23
|
+
a = :bar
|
24
|
+
end
|
25
|
+
expect(a).to eq :bar
|
26
|
+
queue.terminate
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'should return value of the block' do
|
30
|
+
queue = GBDispatch::Queue.new(:test, @pool)
|
31
|
+
a = queue.perform do
|
32
|
+
:bar
|
33
|
+
end
|
34
|
+
expect(a).to eq :bar
|
35
|
+
queue.terminate
|
36
|
+
end
|
37
|
+
|
38
|
+
it 'should return exception object on failure' do
|
39
|
+
queue = GBDispatch::Queue.new(:test, @pool)
|
40
|
+
a = queue.perform ->() do
|
41
|
+
raise StandardError.new 'Test error'
|
42
|
+
end
|
43
|
+
expect(a).to be_kind_of StandardError
|
44
|
+
queue.terminate
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'asynchronous' do
|
49
|
+
it 'should execute in proper order - one after each other' do
|
50
|
+
a = []
|
51
|
+
queue = GBDispatch::Queue.new(:test, @pool)
|
52
|
+
queue.async.perform ->() do
|
53
|
+
(0..10).each { |x| (a << x) && sleep(0.005) }
|
54
|
+
end
|
55
|
+
queue.async.perform ->() do
|
56
|
+
(11..20).each { |x| a << x }
|
57
|
+
end
|
58
|
+
sleep(0.06)
|
59
|
+
expect(a).to eq (0..20).to_a
|
60
|
+
queue.terminate
|
61
|
+
end
|
62
|
+
|
63
|
+
it 'should handle failures nicely' do
|
64
|
+
queue = GBDispatch::Queue.new(:test, @pool)
|
65
|
+
a = :foo
|
66
|
+
queue.async.perform ->() do
|
67
|
+
raise StandardError.new 'Test error'
|
68
|
+
end
|
69
|
+
queue.async.perform ->() do
|
70
|
+
a = :bar
|
71
|
+
end
|
72
|
+
sleep(0.05)
|
73
|
+
expect(a).to eq :bar
|
74
|
+
queue.terminate
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
context 'rails' do
|
79
|
+
before(:each) do
|
80
|
+
class Rails
|
81
|
+
end
|
82
|
+
module ActiveRecord
|
83
|
+
class Base
|
84
|
+
class DummyConnectionPool
|
85
|
+
def with_connection
|
86
|
+
yield
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def self.connection_pool
|
91
|
+
@pool ||= DummyConnectionPool.new
|
92
|
+
end
|
93
|
+
|
94
|
+
def self.clear_active_connections!
|
95
|
+
true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
after(:each) do
|
102
|
+
begin
|
103
|
+
Kernel.send :remove_const, 'Rails'
|
104
|
+
Kernel.send :remove_const, 'ActiveRecord'
|
105
|
+
rescue
|
106
|
+
nil
|
107
|
+
end
|
108
|
+
begin
|
109
|
+
Object.send :remove_const, 'Rails'
|
110
|
+
Object.send :remove_const, 'ActiveRecord'
|
111
|
+
rescue
|
112
|
+
nil
|
113
|
+
end
|
114
|
+
begin
|
115
|
+
BasicObject.send :remove_const, 'Rails'
|
116
|
+
BasicObject.send :remove_const, 'ActiveRecord'
|
117
|
+
rescue
|
118
|
+
nil
|
119
|
+
end
|
120
|
+
raise 'dupa' if defined? Rails
|
121
|
+
end
|
122
|
+
|
123
|
+
it 'should wrap execution with connection pool' do
|
124
|
+
a = :foo
|
125
|
+
queue = GBDispatch::Queue.new(:test, @pool)
|
126
|
+
expect(ActiveRecord::Base.connection_pool).to receive(:with_connection).and_call_original
|
127
|
+
expect(ActiveRecord::Base).to receive(:clear_active_connections!)
|
128
|
+
queue.perform do
|
129
|
+
a = :bar
|
130
|
+
end
|
131
|
+
expect(a).to eq :bar
|
132
|
+
queue.terminate
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
data/spec/runner_spec.rb
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'gb_dispatch/runner'
|
3
|
+
|
4
|
+
describe GBDispatch::Runner do
|
5
|
+
|
6
|
+
it 'should execute synchronously' do
|
7
|
+
a = :foo
|
8
|
+
actor = GBDispatch::Runner.new
|
9
|
+
actor.execute ->() { a=:bar }
|
10
|
+
expect(a).to eq :bar
|
11
|
+
actor.terminate
|
12
|
+
end
|
13
|
+
|
14
|
+
it 'should return result of passed block' do
|
15
|
+
actor = GBDispatch::Runner.new
|
16
|
+
a = actor.execute ->() { :bar }
|
17
|
+
expect(a).to eq :bar
|
18
|
+
actor.terminate
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should execute async' do
|
22
|
+
a = :foo
|
23
|
+
actor = GBDispatch::Runner.new
|
24
|
+
actor.async.execute ->() do
|
25
|
+
sleep(0.01)
|
26
|
+
a = :bar
|
27
|
+
end
|
28
|
+
expect(a).to eq :foo
|
29
|
+
sleep(0.015)
|
30
|
+
expect(a).to eq :bar
|
31
|
+
actor.terminate
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should support Celluloid Conditions' do
|
35
|
+
a = :foo
|
36
|
+
actor = GBDispatch::Runner.new
|
37
|
+
actor.async.execute ->() { 5+5 }, :condition => ->(result) { a = result }
|
38
|
+
sleep(0.01)
|
39
|
+
expect(a).to eq 10
|
40
|
+
actor.terminate
|
41
|
+
end
|
42
|
+
|
43
|
+
context 'error handling' do
|
44
|
+
it 'should call Celluloid Conditions with exception' do
|
45
|
+
a = :foo
|
46
|
+
actor = GBDispatch::Runner.new
|
47
|
+
actor.async.execute ->() { raise StandardError.new 'Test' }, :condition => ->(result) { a = result }, name: :fail_test
|
48
|
+
sleep(0.01)
|
49
|
+
expect(a).to be_kind_of StandardError
|
50
|
+
expect(actor.alive?).to be_falsey
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,104 @@
|
|
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
|
+
# The generated `.rspec` file contains `--require spec_helper` which will cause
|
4
|
+
# this file to always be loaded, without a need to explicitly require it in any
|
5
|
+
# files.
|
6
|
+
#
|
7
|
+
# Given that it is always loaded, you are encouraged to keep this file as
|
8
|
+
# light-weight as possible. Requiring heavyweight dependencies from this file
|
9
|
+
# will add to the boot time of your test suite on EVERY test run, even for an
|
10
|
+
# individual file that may not need all of that loaded. Instead, consider making
|
11
|
+
# a separate helper file that requires the additional dependencies and performs
|
12
|
+
# the additional setup, and require it from the spec files that actually need
|
13
|
+
# it.
|
14
|
+
#
|
15
|
+
# The `.rspec` file also contains a few flags that are not defaults but that
|
16
|
+
# users commonly want.
|
17
|
+
#
|
18
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
19
|
+
require 'simplecov'
|
20
|
+
require 'ostruct'
|
21
|
+
SimpleCov.start do
|
22
|
+
add_filter '/spec/'
|
23
|
+
end
|
24
|
+
|
25
|
+
SimpleCov.minimum_coverage 90
|
26
|
+
|
27
|
+
RSpec.configure do |config|
|
28
|
+
# rspec-expectations config goes here. You can use an alternate
|
29
|
+
# assertion/expectation library such as wrong or the stdlib/minitest
|
30
|
+
# assertions if you prefer.
|
31
|
+
config.expect_with :rspec do |expectations|
|
32
|
+
# This option will default to `true` in RSpec 4. It makes the `description`
|
33
|
+
# and `failure_message` of custom matchers include text for helper methods
|
34
|
+
# defined using `chain`, e.g.:
|
35
|
+
# be_bigger_than(2).and_smaller_than(4).description
|
36
|
+
# # => "be bigger than 2 and smaller than 4"
|
37
|
+
# ...rather than:
|
38
|
+
# # => "be bigger than 2"
|
39
|
+
expectations.include_chain_clauses_in_custom_matcher_descriptions = true
|
40
|
+
end
|
41
|
+
|
42
|
+
# rspec-mocks config goes here. You can use an alternate test double
|
43
|
+
# library (such as bogus or mocha) by changing the `mock_with` option here.
|
44
|
+
config.mock_with :rspec do |mocks|
|
45
|
+
# Prevents you from mocking or stubbing a method that does not exist on
|
46
|
+
# a real object. This is generally recommended, and will default to
|
47
|
+
# `true` in RSpec 4.
|
48
|
+
mocks.verify_partial_doubles = true
|
49
|
+
end
|
50
|
+
|
51
|
+
# The settings below are suggested to provide a good initial experience
|
52
|
+
# with RSpec, but feel free to customize to your heart's content.
|
53
|
+
=begin
|
54
|
+
# These two settings work together to allow you to limit a spec run
|
55
|
+
# to individual examples or groups you care about by tagging them with
|
56
|
+
# `:focus` metadata. When nothing is tagged with `:focus`, all examples
|
57
|
+
# get run.
|
58
|
+
config.filter_run :focus
|
59
|
+
config.run_all_when_everything_filtered = true
|
60
|
+
|
61
|
+
# Allows RSpec to persist some state between runs in order to support
|
62
|
+
# the `--only-failures` and `--next-failure` CLI options. We recommend
|
63
|
+
# you configure your source control system to ignore this file.
|
64
|
+
config.example_status_persistence_file_path = "spec/examples.txt"
|
65
|
+
|
66
|
+
# Limits the available syntax to the non-monkey patched syntax that is
|
67
|
+
# recommended. For more details, see:
|
68
|
+
# - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
|
69
|
+
# - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
|
70
|
+
# - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
|
71
|
+
config.disable_monkey_patching!
|
72
|
+
|
73
|
+
# This setting enables warnings. It's recommended, but in some cases may
|
74
|
+
# be too noisy due to issues in dependencies.
|
75
|
+
config.warnings = true
|
76
|
+
|
77
|
+
# Many RSpec users commonly either run the entire suite or an individual
|
78
|
+
# file, and it's useful to allow more verbose output when running an
|
79
|
+
# individual spec file.
|
80
|
+
if config.files_to_run.one?
|
81
|
+
# Use the documentation formatter for detailed output,
|
82
|
+
# unless a formatter has already been configured
|
83
|
+
# (e.g. via a command-line flag).
|
84
|
+
config.default_formatter = 'doc'
|
85
|
+
end
|
86
|
+
|
87
|
+
# Print the 10 slowest examples and example groups at the
|
88
|
+
# end of the spec run, to help surface which specs are running
|
89
|
+
# particularly slow.
|
90
|
+
config.profile_examples = 10
|
91
|
+
|
92
|
+
# Run specs in random order to surface order dependencies. If you find an
|
93
|
+
# order dependency and want to debug it, you can fix the order by providing
|
94
|
+
# the seed, which is printed after each run.
|
95
|
+
# --seed 1234
|
96
|
+
config.order = :random
|
97
|
+
|
98
|
+
# Seed global randomization in this process using the `--seed` CLI option.
|
99
|
+
# Setting this allows you to use `--seed` to deterministically reproduce
|
100
|
+
# test failures related to randomization by passing the same `--seed` value
|
101
|
+
# as the one that triggered the failure.
|
102
|
+
Kernel.srand config.seed
|
103
|
+
=end
|
104
|
+
end
|
metadata
ADDED
@@ -0,0 +1,111 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: gb_dispatch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Kacper Kawecki
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-01-12 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: celluloid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.15.2
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.15.2
|
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.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
description: Allows to dispatch block of code for queues
|
56
|
+
email:
|
57
|
+
- kacper@geniebelt.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".idea/.name"
|
64
|
+
- ".idea/encodings.xml"
|
65
|
+
- ".idea/misc.xml"
|
66
|
+
- ".idea/modules.xml"
|
67
|
+
- ".idea/vcs.xml"
|
68
|
+
- ".rspec"
|
69
|
+
- Gemfile
|
70
|
+
- Gemfile.lock
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- gb_dispatch.gemspec
|
75
|
+
- lib/gb_dispatch.rb
|
76
|
+
- lib/gb_dispatch/central_dispatch.rb
|
77
|
+
- lib/gb_dispatch/manager.rb
|
78
|
+
- lib/gb_dispatch/queue.rb
|
79
|
+
- lib/gb_dispatch/runner.rb
|
80
|
+
- lib/gb_dispatch/version.rb
|
81
|
+
- spec/queue_spec.rb
|
82
|
+
- spec/runner_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
homepage: https://github.com/GenieBelt/gb-dispatch
|
85
|
+
licenses:
|
86
|
+
- MIT
|
87
|
+
metadata: {}
|
88
|
+
post_install_message:
|
89
|
+
rdoc_options: []
|
90
|
+
require_paths:
|
91
|
+
- lib
|
92
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - ">="
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 2.4.8
|
105
|
+
signing_key:
|
106
|
+
specification_version: 4
|
107
|
+
summary: GCD emulation for ruby
|
108
|
+
test_files:
|
109
|
+
- spec/queue_spec.rb
|
110
|
+
- spec/runner_spec.rb
|
111
|
+
- spec/spec_helper.rb
|