spawnling 2.1.1 → 2.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +2 -0
- data/.travis.yml +19 -0
- data/Gemfile +2 -0
- data/README.md +12 -3
- data/Rakefile +18 -0
- data/gemfiles/rails2.gemfile +7 -0
- data/gemfiles/rails3.gemfile +6 -0
- data/gemfiles/rails4.gemfile +6 -0
- data/lib/patches.rb +1 -1
- data/lib/spawnling.rb +9 -5
- data/lib/spawnling/version.rb +3 -0
- data/spawnling.gemspec +34 -0
- data/spec/spawn/spawn_spec.rb +12 -28
- data/spec/spec_helper.rb +26 -6
- metadata +101 -25
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: afe2261ea1a39c0cbb459886da3f12af8757874c
|
4
|
+
data.tar.gz: c53f9cda870af2c9e361ca1c45769ae7bb61dcf1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d2f4925825d2577cb98bd380c2a2d10d1083d4de2b2be8764d3748d00480c48dde529ce999195709cc850b035dbb675078fa58d200f69a178c5c541e4126b5f7
|
7
|
+
data.tar.gz: defaaebc1e9d420bb0d229b03b6ee1e4e84c1ba5dd936c18c9fc9f12ddab320903a110ecd011f6759537ccf4ae7a3bea93a49145a2e2c2f4251739fc12932f71
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 1.8.7
|
4
|
+
- 1.9.3
|
5
|
+
- 2.0
|
6
|
+
- 2.1
|
7
|
+
gemfile:
|
8
|
+
- gemfiles/rails2.gemfile
|
9
|
+
- gemfiles/rails3.gemfile
|
10
|
+
- gemfiles/rails4.gemfile
|
11
|
+
matrix:
|
12
|
+
exclude:
|
13
|
+
- rvm: 2.0
|
14
|
+
gemfile: gemfiles/rails2.gemfile
|
15
|
+
- rvm: 2.1
|
16
|
+
gemfile: gemfiles/rails2.gemfile
|
17
|
+
- rvm: 1.8.7
|
18
|
+
gemfile: gemfiles/rails4.gemfile
|
19
|
+
script: "bundle exec rake coverage"
|
data/Gemfile
ADDED
data/README.md
CHANGED
@@ -1,3 +1,12 @@
|
|
1
|
+
Spawnling
|
2
|
+
=========
|
3
|
+
|
4
|
+
[![Gem Version](https://badge.fury.io/rb/geokit.png)](http://badge.fury.io/rb/geokit)
|
5
|
+
[![Build Status](https://travis-ci.org/tra/spawnling.png?branch=master)](https://travis-ci.org/tra/spawnling)
|
6
|
+
[![Coverage Status](https://coveralls.io/repos/tra/spawnling/badge.png)](https://coveralls.io/r/tra/spawnling)
|
7
|
+
[![Dependency Status](https://gemnasium.com/tra/spawnling.png)](https://gemnasium.com/tra/spawnling)
|
8
|
+
[![Code Climate](https://codeclimate.com/github/tra/spawnling.png)](https://codeclimate.com/github/tra/spawnling)
|
9
|
+
|
1
10
|
#News
|
2
11
|
|
3
12
|
2013-4-15 gem renamed from "spawn-block" (lame) to "spawnling" (awesome). Sadly the
|
@@ -21,14 +30,14 @@ threads if you prefer using the threaded model over forking.
|
|
21
30
|
|
22
31
|
## Installation
|
23
32
|
|
24
|
-
The name of the gem is "
|
33
|
+
The name of the gem is "spawnling" (unfortunately somebody took the name "spawn" before
|
25
34
|
I was able to convert this to a gem).
|
26
35
|
|
27
36
|
### git
|
28
37
|
|
29
38
|
If you want to live on the latest master branch, add this to your Gemfile,
|
30
39
|
|
31
|
-
gem '
|
40
|
+
gem 'spawnling', :git => 'git://github.com/tra/spawnling'
|
32
41
|
|
33
42
|
and use bundler to manage it (bundle install, bundle update).
|
34
43
|
|
@@ -36,7 +45,7 @@ and use bundler to manage it (bundle install, bundle update).
|
|
36
45
|
|
37
46
|
If you'd rather install from the latest gem,
|
38
47
|
|
39
|
-
gem '
|
48
|
+
gem 'spawnling', '~>2.1'
|
40
49
|
|
41
50
|
### configure
|
42
51
|
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
|
3
|
+
desc 'Default: run coverage.'
|
4
|
+
task :default => :coverage
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
desc "Run specs"
|
9
|
+
RSpec::Core::RakeTask.new do |t|
|
10
|
+
t.pattern = "./spec/**/*_spec.rb" # don't need this, it's default.
|
11
|
+
# Put spec opts in a file named .rspec in root
|
12
|
+
end
|
13
|
+
|
14
|
+
desc "Generate SimpleCov test coverage and open in your browser"
|
15
|
+
task :coverage do
|
16
|
+
ENV['COVERAGE'] = 'true'
|
17
|
+
Rake::Task['spec'].invoke
|
18
|
+
end
|
data/lib/patches.rb
CHANGED
@@ -130,6 +130,6 @@ if need_passenger_patch
|
|
130
130
|
end
|
131
131
|
end
|
132
132
|
|
133
|
-
if defined?(::ActiveSupport::Cache::MemCacheStore)
|
133
|
+
if defined?(::ActiveSupport::Cache::MemCacheStore)
|
134
134
|
::ActiveSupport::Cache::MemCacheStore.delegate :reset, :to => :@data
|
135
135
|
end
|
data/lib/spawnling.rb
CHANGED
@@ -91,6 +91,10 @@ class Spawnling
|
|
91
91
|
# :method => :thread or override the default behavior in the environment by setting
|
92
92
|
# 'Spawnling::method :thread'.
|
93
93
|
def initialize(opts = {})
|
94
|
+
self.class.run(opts)
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.run(opts = {})
|
94
98
|
raise "Must give block of code to be spawned" unless block_given?
|
95
99
|
options = @@default_options.merge(symbolize_options(opts))
|
96
100
|
# setting options[:method] will override configured value in default_options[:method]
|
@@ -100,7 +104,7 @@ class Spawnling
|
|
100
104
|
options[:method].call(proc { yield })
|
101
105
|
elsif options[:method] == :thread
|
102
106
|
# for versions before 2.2, check for allow_concurrency
|
103
|
-
if RAILS_2_2 || ActiveRecord::Base.respond_to?(:allow_concurrency) ?
|
107
|
+
if RAILS_2_2 || (defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:allow_concurrency)) ?
|
104
108
|
ActiveRecord::Base.allow_concurrency : Rails.application.config.allow_concurrency
|
105
109
|
@type = :thread
|
106
110
|
@handle = thread_it(options) { yield }
|
@@ -152,7 +156,7 @@ class Spawnling
|
|
152
156
|
Spawnling.close_resources
|
153
157
|
if defined?(Rails)
|
154
158
|
# get a new database connection so the parent can keep the original one
|
155
|
-
ActiveRecord::Base.spawn_reconnect
|
159
|
+
ActiveRecord::Base.spawn_reconnect if defined?(ActiveRecord)
|
156
160
|
# close the memcache connection so the parent can keep the original one
|
157
161
|
Rails.cache.reset if Rails.cache.respond_to?(:reset)
|
158
162
|
end
|
@@ -186,7 +190,7 @@ class Spawnling
|
|
186
190
|
Process.detach(child)
|
187
191
|
|
188
192
|
# remove dead children from the target list to avoid memory leaks
|
189
|
-
@@punks.delete_if {|punk| !alive?(punk)}
|
193
|
+
@@punks.delete_if {|punk| !Spawn.alive?(punk)}
|
190
194
|
|
191
195
|
# mark this child for death when this process dies
|
192
196
|
if options[:kill]
|
@@ -203,14 +207,14 @@ class Spawnling
|
|
203
207
|
ActiveRecord::Base.verify_active_connections!() if defined?(ActiveRecord)
|
204
208
|
thr = Thread.new do
|
205
209
|
# run the long-running code block
|
206
|
-
ActiveRecord::Base.connection_pool.with_connection { yield }
|
210
|
+
ActiveRecord::Base.connection_pool.with_connection { yield } if defined?(ActiveRecord)
|
207
211
|
end
|
208
212
|
thr.priority = -options[:nice] if options[:nice]
|
209
213
|
return thr
|
210
214
|
end
|
211
215
|
|
212
216
|
# In case we don't have rails, can't call opts.symbolize_keys
|
213
|
-
def symbolize_options(hash)
|
217
|
+
def self.symbolize_options(hash)
|
214
218
|
hash.inject({}) do |new_hash, (key, value)|
|
215
219
|
new_hash[key.to_sym] = value
|
216
220
|
new_hash
|
data/spawnling.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'spawnling/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = "spawnling"
|
8
|
+
s.version = Spawnling::VERSION
|
9
|
+
|
10
|
+
s.authors = ['Tom Anderson']
|
11
|
+
s.email = ['tom@squeat.com']
|
12
|
+
|
13
|
+
s.homepage = %q{http://github.com/tra/spawnling}
|
14
|
+
s.license = "MIT"
|
15
|
+
s.summary = %q{Easily fork OR thread long-running sections of code in Ruby}
|
16
|
+
s.description = %q{This plugin provides a 'Spawnling' class to easily fork OR
|
17
|
+
thread long-running sections of code so that your application can return
|
18
|
+
results to your users more quickly. This plugin works by creating new database
|
19
|
+
connections in ActiveRecord::Base for the spawned block.
|
20
|
+
|
21
|
+
The plugin also patches ActiveRecord::Base to handle some known bugs when using
|
22
|
+
threads (see lib/patches.rb).}
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split($/)
|
25
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
26
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
27
|
+
s.require_paths = ["lib"]
|
28
|
+
|
29
|
+
s.add_development_dependency 'bundler'
|
30
|
+
s.add_development_dependency 'rake'
|
31
|
+
s.add_development_dependency 'rspec'
|
32
|
+
s.add_development_dependency 'coveralls'
|
33
|
+
s.add_development_dependency 'rails'
|
34
|
+
end
|
data/spec/spawn/spawn_spec.rb
CHANGED
@@ -1,63 +1,47 @@
|
|
1
1
|
require File.dirname(__FILE__) + '/../spec_helper'
|
2
2
|
|
3
|
-
describe
|
3
|
+
describe Spawnling do
|
4
4
|
|
5
5
|
describe "yields" do
|
6
6
|
before(:each) do
|
7
|
-
|
8
|
-
define_spawned
|
7
|
+
Spawnling::default_options :method => :yield
|
9
8
|
end
|
10
9
|
|
11
10
|
it "should be able to yield directly" do
|
12
|
-
|
11
|
+
spawn!.should == "hello"
|
13
12
|
end
|
14
13
|
end
|
15
14
|
|
16
15
|
describe "override" do
|
17
16
|
before(:each) do
|
18
|
-
|
19
|
-
define_spawned
|
17
|
+
Spawnling::default_options :method => proc{ "foo" }
|
20
18
|
end
|
21
19
|
|
22
20
|
it "should be able to return a proc" do
|
23
|
-
|
21
|
+
spawn!.should == "foo"
|
24
22
|
end
|
25
23
|
|
26
24
|
end
|
27
25
|
|
28
26
|
describe "delegate to a proc" do
|
29
27
|
before(:each) do
|
30
|
-
|
31
|
-
define_spawned
|
28
|
+
Spawnling::default_options :method => proc{ |block| block }
|
32
29
|
end
|
33
30
|
|
34
31
|
it "should be able to return a proc" do
|
35
|
-
|
32
|
+
spawn!.should be_kind_of(Proc)
|
36
33
|
end
|
37
34
|
|
38
35
|
it "should be able to return a proc" do
|
39
|
-
|
36
|
+
spawn!.call.should == "hello"
|
40
37
|
end
|
41
38
|
|
42
39
|
end
|
43
40
|
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
def define_spawned
|
49
|
-
cls = Class.new do
|
50
|
-
extend Spawn
|
51
|
-
|
52
|
-
def self.hello
|
53
|
-
spawn do
|
54
|
-
"hello"
|
55
|
-
end
|
56
|
-
end
|
57
|
-
|
41
|
+
def spawn!
|
42
|
+
Spawnling.run do
|
43
|
+
"hello"
|
58
44
|
end
|
59
|
-
|
60
|
-
Object.const_set :Spawned, cls
|
61
45
|
end
|
62
46
|
|
63
|
-
end
|
47
|
+
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,13 +1,33 @@
|
|
1
1
|
require 'rubygems'
|
2
2
|
gem 'rspec'
|
3
|
-
require '
|
4
|
-
require 'active_record'
|
5
|
-
require 'action_controller'
|
6
|
-
require 'rails/version'
|
3
|
+
require 'rspec'
|
7
4
|
|
8
5
|
$:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
|
9
6
|
|
10
|
-
|
7
|
+
MINIMUM_COVERAGE = 28
|
8
|
+
|
9
|
+
if ENV['COVERAGE']
|
10
|
+
require 'simplecov'
|
11
|
+
require 'coveralls'
|
12
|
+
Coveralls.wear!
|
13
|
+
|
14
|
+
SimpleCov.formatter = Coveralls::SimpleCov::Formatter
|
15
|
+
SimpleCov.start do
|
16
|
+
add_filter '/vendor/'
|
17
|
+
add_filter '/spec/'
|
18
|
+
add_group 'lib', 'lib'
|
19
|
+
end
|
20
|
+
SimpleCov.at_exit do
|
21
|
+
SimpleCov.result.format!
|
22
|
+
percent = SimpleCov.result.covered_percent
|
23
|
+
unless percent >= MINIMUM_COVERAGE
|
24
|
+
puts "Coverage must be above #{MINIMUM_COVERAGE}%. It is #{"%.2f" % percent}%"
|
25
|
+
Kernel.exit(1)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
require 'spawnling'
|
11
31
|
Spec::Runner.configure do |config|
|
12
32
|
|
13
|
-
end
|
33
|
+
end
|
metadata
CHANGED
@@ -1,65 +1,141 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spawnling
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2.1.
|
5
|
-
prerelease:
|
4
|
+
version: 2.1.2
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Tom Anderson
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
13
|
-
dependencies:
|
14
|
-
|
15
|
-
|
11
|
+
date: 2014-06-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
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: coveralls
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
description: |-
|
84
|
+
This plugin provides a 'Spawnling' class to easily fork OR
|
16
85
|
thread long-running sections of code so that your application can return
|
17
|
-
|
18
86
|
results to your users more quickly. This plugin works by creating new database
|
19
|
-
|
20
87
|
connections in ActiveRecord::Base for the spawned block.
|
21
88
|
|
22
|
-
|
23
89
|
The plugin also patches ActiveRecord::Base to handle some known bugs when using
|
24
|
-
|
25
|
-
threads (see lib/patches.rb).'
|
90
|
+
threads (see lib/patches.rb).
|
26
91
|
email:
|
27
92
|
- tom@squeat.com
|
28
93
|
executables: []
|
29
94
|
extensions: []
|
30
95
|
extra_rdoc_files: []
|
31
96
|
files:
|
32
|
-
-
|
33
|
-
-
|
34
|
-
- lib/spawnling.rb
|
35
|
-
- spec/spawn/spawn_spec.rb
|
36
|
-
- spec/spec_helper.rb
|
97
|
+
- ".gitignore"
|
98
|
+
- ".travis.yml"
|
37
99
|
- CHANGELOG
|
100
|
+
- Gemfile
|
38
101
|
- LICENSE
|
39
102
|
- README.md
|
103
|
+
- Rakefile
|
104
|
+
- gemfiles/rails2.gemfile
|
105
|
+
- gemfiles/rails3.gemfile
|
106
|
+
- gemfiles/rails4.gemfile
|
40
107
|
- init.rb
|
108
|
+
- lib/patches.rb
|
109
|
+
- lib/spawnling.rb
|
110
|
+
- lib/spawnling/cucumber.rb
|
111
|
+
- lib/spawnling/version.rb
|
112
|
+
- spawnling.gemspec
|
113
|
+
- spec/spawn/spawn_spec.rb
|
114
|
+
- spec/spec_helper.rb
|
41
115
|
homepage: http://github.com/tra/spawnling
|
42
|
-
licenses:
|
116
|
+
licenses:
|
117
|
+
- MIT
|
118
|
+
metadata: {}
|
43
119
|
post_install_message:
|
44
120
|
rdoc_options: []
|
45
121
|
require_paths:
|
46
122
|
- lib
|
47
123
|
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
-
none: false
|
49
124
|
requirements:
|
50
|
-
- -
|
125
|
+
- - ">="
|
51
126
|
- !ruby/object:Gem::Version
|
52
127
|
version: '0'
|
53
128
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
-
none: false
|
55
129
|
requirements:
|
56
|
-
- -
|
130
|
+
- - ">="
|
57
131
|
- !ruby/object:Gem::Version
|
58
|
-
version:
|
132
|
+
version: '0'
|
59
133
|
requirements: []
|
60
134
|
rubyforge_project:
|
61
|
-
rubygems_version:
|
135
|
+
rubygems_version: 2.2.1
|
62
136
|
signing_key:
|
63
|
-
specification_version:
|
137
|
+
specification_version: 4
|
64
138
|
summary: Easily fork OR thread long-running sections of code in Ruby
|
65
|
-
test_files:
|
139
|
+
test_files:
|
140
|
+
- spec/spawn/spawn_spec.rb
|
141
|
+
- spec/spec_helper.rb
|