spawnling 2.1.3 → 2.1.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 1547c27e7ec97c65047ba3205a96e310128950a0
4
- data.tar.gz: 63635512ba55d8f1a6b842c50202ef5a0b1f82a1
3
+ metadata.gz: 26aa7286ca5e508bb9bec2971336151f68d64225
4
+ data.tar.gz: e70bf1b0865d216b5d3a197268eff1af8021e70f
5
5
  SHA512:
6
- metadata.gz: 6ee4fd92e0b597bc3710a3bd0059f9fea5abee7970b802c4b7b105891a357c261ecf139fd3c07a77035c2a93714d9878ddade53ba2678ea2095843bd57abeac7
7
- data.tar.gz: 871dc470a875cf7586d5e286f13028c0eaf800bc97a19e7688ee2bcc2d20d8bbe51c6731d235bac54cdd44977d1d200f0b3ef2bc73991b5692112e22ed30a4cb
6
+ metadata.gz: 01315192fee03920aba467775e9f59801340dce832a3ba6c32b2a3643b7b0f730a8ac58a92b674b11a22ea09ee748688539d43f008cb9da0f6c55bd1a9a15814
7
+ data.tar.gz: e03db92b7c3c476190a89b032160928541c31f15d9068578b19c421bf8669e50d99c40ced3cbe97621270fc4165d021929af381002a878ab739f6122c55747c6
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  Spawnling
2
2
  =========
3
3
 
4
- [![Gem Version](https://badge.fury.io/rb/geokit.png)](http://badge.fury.io/rb/geokit)
4
+ [![Gem Version](https://badge.fury.io/rb/spawnling.png)](http://badge.fury.io/rb/spawnling)
5
5
  [![Build Status](https://travis-ci.org/tra/spawnling.png?branch=master)](https://travis-ci.org/tra/spawnling)
6
6
  [![Coverage Status](https://coveralls.io/repos/tra/spawnling/badge.png)](https://coveralls.io/r/tra/spawnling)
7
7
  [![Dependency Status](https://gemnasium.com/tra/spawnling.png)](https://gemnasium.com/tra/spawnling)
@@ -28,6 +28,10 @@ class Spawnling
28
28
  # in some environments, logger isn't defined
29
29
  @@logger = defined?(::Rails) ? ::Rails.logger : ::Logger.new(STDERR)
30
30
 
31
+ def self.logger=(logger)
32
+ @@logger = logger
33
+ end
34
+
31
35
  attr_accessor :type
32
36
  attr_accessor :handle
33
37
 
@@ -104,8 +108,7 @@ class Spawnling
104
108
  options[:method].call(proc { yield })
105
109
  elsif options[:method] == :thread
106
110
  # for versions before 2.2, check for allow_concurrency
107
- if RAILS_2_2 || (defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:allow_concurrency)) ?
108
- ActiveRecord::Base.allow_concurrency : Rails.application.config.allow_concurrency
111
+ if allow_concurrency?
109
112
  @type = :thread
110
113
  @handle = thread_it(options) { yield }
111
114
  else
@@ -118,6 +121,17 @@ class Spawnling
118
121
  end
119
122
  end
120
123
 
124
+ def self.allow_concurrency?
125
+ return true if RAILS_2_2
126
+ if defined?(ActiveRecord) && ActiveRecord::Base.respond_to?(:allow_concurrency)
127
+ ActiveRecord::Base.allow_concurrency
128
+ elsif defined?(Rails)
129
+ Rails.application.config.allow_concurrency
130
+ else
131
+ true # assume user knows what they are doing
132
+ end
133
+ end
134
+
121
135
  def self.wait(sids = [])
122
136
  # wait for all threads and/or forks (if a single sid passed in, convert to array first)
123
137
  Array(sids).each do |sid|
@@ -137,7 +151,7 @@ class Spawnling
137
151
 
138
152
  protected
139
153
 
140
- def fork_it(options)
154
+ def self.fork_it(options)
141
155
  # The problem with rails is that it only has one connection (per class),
142
156
  # so when we fork a new process, we need to reconnect.
143
157
  @@logger.debug "spawn> parent PID = #{Process.pid}" if @@logger
@@ -202,12 +216,16 @@ class Spawnling
202
216
  return child
203
217
  end
204
218
 
205
- def thread_it(options)
219
+ def self.thread_it(options)
206
220
  # clean up stale connections from previous threads
207
221
  ActiveRecord::Base.verify_active_connections!() if defined?(ActiveRecord)
208
222
  thr = Thread.new do
209
223
  # run the long-running code block
210
- ActiveRecord::Base.connection_pool.with_connection { yield } if defined?(ActiveRecord)
224
+ if defined?(ActiveRecord)
225
+ ActiveRecord::Base.connection_pool.with_connection { yield }
226
+ else
227
+ yield
228
+ end
211
229
  end
212
230
  thr.priority = -options[:nice] if options[:nice]
213
231
  return thr
@@ -1,3 +1,3 @@
1
1
  class Spawnling
2
- VERSION = '2.1.3'
2
+ VERSION = '2.1.4'
3
3
  end
@@ -28,7 +28,7 @@ threads (see lib/patches.rb).}
28
28
 
29
29
  s.add_development_dependency 'bundler'
30
30
  s.add_development_dependency 'rake'
31
- s.add_development_dependency 'rspec'
31
+ s.add_development_dependency 'rspec', '~> 2.0'
32
32
  s.add_development_dependency 'coveralls'
33
33
  s.add_development_dependency 'rails'
34
34
  end
@@ -1,4 +1,4 @@
1
- require File.dirname(__FILE__) + '/../spec_helper'
1
+ require 'spec_helper'
2
2
 
3
3
  describe Spawnling do
4
4
 
@@ -43,13 +43,46 @@ describe Spawnling do
43
43
  it "should be able to return a proc" do
44
44
  spawn!.call.should == "hello"
45
45
  end
46
-
46
+ end
47
+
48
+ describe "thread it" do
49
+ before(:each) do
50
+ Store.reset!
51
+ Spawnling::default_options :method => :thread
52
+ end
53
+
54
+ it "should be able to return a proc" do
55
+ Store.flag.should be_falsey
56
+ spawn_flag!
57
+ sleep(0.1) # wait for file to finish writing
58
+ Store.flag.should be_truthy
59
+ end
47
60
  end
48
61
 
62
+ describe "fork it" do
63
+ before(:each) do
64
+ Store.reset!
65
+ Spawnling::default_options :method => :fork
66
+ end
67
+
68
+ it "should be able to return a proc" do
69
+ Store.flag.should be_falsey
70
+ spawn_flag!
71
+ sleep(0.1) # wait for file to finish writing
72
+ Store.flag.should be_truthy
73
+ end
74
+ end
75
+
49
76
  def spawn!
50
77
  Spawnling.run do
51
78
  "hello"
52
79
  end
53
80
  end
81
+
82
+ def spawn_flag!
83
+ Spawnling.new do
84
+ Store.flag!
85
+ end
86
+ end
54
87
 
55
88
  end
@@ -0,0 +1,12 @@
1
+ require 'spec_helper'
2
+
3
+ describe Store do
4
+ it 'should flag/unflag' do
5
+ Store.reset!
6
+ Store.flag.should be_falsey
7
+ Store.flag!
8
+ Store.flag.should be_truthy
9
+ Store.reset!
10
+ Store.flag.should be_falsey
11
+ end
12
+ end
@@ -4,7 +4,9 @@ require 'rspec'
4
4
 
5
5
  $:.unshift(File.join(File.dirname(__FILE__), %w[.. lib]))
6
6
 
7
- MINIMUM_COVERAGE = 28
7
+ require 'store'
8
+
9
+ MINIMUM_COVERAGE = 40
8
10
 
9
11
  if ENV['COVERAGE']
10
12
  require 'simplecov'
@@ -28,6 +30,5 @@ if ENV['COVERAGE']
28
30
  end
29
31
 
30
32
  require 'spawnling'
31
- Spec::Runner.configure do |config|
32
33
 
33
- end
34
+ Spawnling.logger = nil
@@ -0,0 +1,19 @@
1
+ class Store
2
+ def self.flag!
3
+ write('true')
4
+ end
5
+
6
+ def self.flag
7
+ File.read('foo') == 'true'
8
+ end
9
+
10
+ def self.reset!
11
+ write('')
12
+ end
13
+
14
+ def self.write(message)
15
+ f = File.new('foo', "w+")
16
+ f.write(message)
17
+ f.close
18
+ end
19
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: spawnling
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.3
4
+ version: 2.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tom Anderson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-02 00:00:00.000000000 Z
11
+ date: 2014-06-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -42,16 +42,16 @@ dependencies:
42
42
  name: rspec
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ">="
45
+ - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '0'
47
+ version: '2.0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ">="
52
+ - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '0'
54
+ version: '2.0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: coveralls
57
57
  requirement: !ruby/object:Gem::Requirement
@@ -111,7 +111,9 @@ files:
111
111
  - lib/spawnling/version.rb
112
112
  - spawnling.gemspec
113
113
  - spec/spawn/spawn_spec.rb
114
+ - spec/spawn/store_spec.rb
114
115
  - spec/spec_helper.rb
116
+ - spec/store.rb
115
117
  homepage: http://github.com/tra/spawnling
116
118
  licenses:
117
119
  - MIT
@@ -138,4 +140,6 @@ specification_version: 4
138
140
  summary: Easily fork OR thread long-running sections of code in Ruby
139
141
  test_files:
140
142
  - spec/spawn/spawn_spec.rb
143
+ - spec/spawn/store_spec.rb
141
144
  - spec/spec_helper.rb
145
+ - spec/store.rb