sucker_punch-backgroundable 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MGYyZWEyZWM0ZWJhMTFlNmI5MTFmZjRjOWQxYzk5NmY5ZDc2NTMwYQ==
4
+ YTljNmMwNGMyOTc0MjlhZGM0NTg3NDRhMmNlODlkYWI3NmFmM2Y4NQ==
5
5
  data.tar.gz: !binary |-
6
- YjgyMzgyYTZlNzAzMWQwZTgzZjgwNTc4ZjRlMGJkNzcyODFjZTkwZA==
6
+ ODY2NTNhMjNjNDFjYjgwNzQ0ZDY1ZTgyNTkzNWViNTEyNTQwNGJmZQ==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NTAyYzMxZDQxYmFkMzk2NzMwODFiYmI5NmMxYWZlYmU0OWVlNDZmOTQ5Njcz
10
- MDkxZmFjOThjNjNjMDMwNmUyOWU1N2Y5YTY2Mzk0ZTMyZGY3YWI4ODJmMjM2
11
- ZDVmNDk1NzBlMzliNzkxZGRkOTQwMmY1YThiOWI5ODBjMjZiYzU=
9
+ NjIxM2QzN2Y2YWY2NzQ5YjM1Mzk1NjZhYjZlODU1MjA4YzBlMzJiYzUyMWQw
10
+ ZGY3MWYxNGQwMjIyNzZhMDVhMjcwMWJlOWZkNDZlYTUyZjMzZTUzY2RlNGY5
11
+ NDhmNzZkOWM0MmU4OWI3NTVlZmY3Y2RlM2EwNjZhYzBjNzFkYzM=
12
12
  data.tar.gz: !binary |-
13
- ZTY0M2Q3MGMxMTJkZTY4ODQ3ZDc4N2M2NjE0YzNkZGQ4YTcyYzZlNjU3NTRm
14
- MjdiOTBhMGE5ZDNlMDhkY2M4ZjYwMWEzYTliYzU5ZGMwYmNjODEwNzdkY2Uy
15
- NmFiNzAyMGE2ZDRiNmNiNTE3NWZiNjZlOWUxZjQ2MDg3MzIzOWY=
13
+ ODk5OTRkMzZhYmM0MDhlYzVhNjY0ZWM3MTA1ZjkzNzRjMmYzMDNlNzJiNDlm
14
+ YWNhMWM1MmZlZjk5YTRlMzQ2MTFkYmNkM2RkNmU1MDFmZGRhMGNkOGU4NTY3
15
+ ZmIzODk2NzIzZmVmMDY2YzU2YjQ1NjE1MDU3NjQyODJhYThkODU=
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
- ## 0.0.1 (19/5/2014)
1
+ ## 0.2.0 (20/5/2014)
2
+
3
+ * Added 'enabled' configuration option
4
+
5
+ ## 0.1.0 (19/5/2014)
2
6
 
3
7
  * First Release
data/Gemfile CHANGED
@@ -2,7 +2,7 @@ source "http://rubygems.org"
2
2
  # Add dependencies required to use your gem here.
3
3
  # Example:
4
4
  # gem "activesupport", ">= 2.3.5"
5
- gem "sucker_punch", ">= 1.0.0"
5
+ gem "sucker_punch", "~> 1.0"
6
6
 
7
7
  # Add dependencies to develop your gem here.
8
8
  # Include everything needed to run rake, tests, features, etc.
data/Gemfile.lock CHANGED
@@ -98,4 +98,4 @@ DEPENDENCIES
98
98
  rspec (~> 2.8.0)
99
99
  simplecov
100
100
  sqlite3
101
- sucker_punch (>= 1.0.0)
101
+ sucker_punch (~> 1.0)
data/README.md CHANGED
@@ -114,17 +114,21 @@ end
114
114
 
115
115
  ## Configuration
116
116
 
117
- Apart from the `reload` option described above, there is only one other configuration setting:
117
+ Apart from the `reload` option described above, there is two other configuration settings:
118
118
 
119
119
  ```ruby
120
120
  SuckerPunch::Backgroundable.configure do |config|
121
121
  config.reload = true
122
122
  config.workers = 4 # default is 2
123
+ config.enabled = true
123
124
  end
124
125
  ```
125
126
 
126
127
  The number of workers sets the number of background threads that SuckerPunch will use. The default (and minimum) is 2.
127
128
 
129
+ By setting enabled to false, backgrounding is globally disabled, so all methods will be executed synchronously. This
130
+ can be useful in tests.
131
+
128
132
  ## Usage with ActiveRecord
129
133
 
130
134
  When using this gem with ActiveRecord models, it is recommended to set the `reload` option to true (see above).
@@ -134,6 +138,24 @@ If you want sucker_punch-backgroundable to be available in all models you can u
134
138
  ActiveRecord::Base.send(:include, SuckerPunch::Backgroundable)
135
139
  ```
136
140
 
141
+ ## Switching from TorqueBox Backgroundable
142
+
143
+ This gem is completely independent from TorqueBox (or JRuby). But if you are already using TorqueBox Backgroundable, you can switch
144
+ to backgrounding with SuckerPunch by a simple change in an initializer:
145
+
146
+ ```ruby
147
+ # In config/initializers/active_record_backgroundable.rb, replace
148
+ if defined?(TorqueBox::Messaging::Backgroundable) && defined?(ActiveRecord::Base)
149
+ ActiveRecord::Base.send(:include, TorqueBox::Messaging::Backgroundable)
150
+ end
151
+
152
+ # by this:
153
+ ActiveRecord::Base.send(:include, SuckerPunch::Backgroundable)
154
+ ```
155
+
156
+ Switching from TorqueBox Backgroundable to SuckerPunch will save you RAM (since TorqueBox uses a separate application instance
157
+ for Backgroundable), but remember that SuckerPunch jobs are not persisted, so you might lose jobs on a server restart.
158
+
137
159
  ## Contributing to sucker_punch-backgroundable
138
160
 
139
161
  * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -110,7 +110,8 @@ module SuckerPunch
110
110
  (singleton_method ? singleton : self).class_eval do
111
111
  define_method async_method do |*args|
112
112
  # run sucker punch job asynchronously
113
- Job.new.async.perform(self, sync_method, args, options)
113
+ # Job.new.async.perform(self, sync_method, args, options)
114
+ JobRunner.new(self, sync_method, args, options).run
114
115
  end
115
116
  end
116
117
 
@@ -142,11 +143,7 @@ module SuckerPunch
142
143
  def method_missing(method, *args, &block)
143
144
  @receiver.method_missing(method, *args, &block) unless @receiver.respond_to?(method)
144
145
  raise ArgumentError.new("Backgrounding a method with a block argument is not supported.") if block_given?
145
- if @seconds > 0
146
- Job.new.async.later(@seconds, @receiver, method, args, @options)
147
- else
148
- Job.new.async.perform(@receiver, method, args, @options)
149
- end
146
+ JobRunner.new(@receiver, method, args, @options).run(@seconds)
150
147
  end
151
148
  end
152
149
 
@@ -11,10 +11,12 @@ module SuckerPunch
11
11
  end
12
12
 
13
13
  class Configuration
14
+ attr_accessor :enabled
14
15
  attr_accessor :workers
15
16
  attr_accessor :reload
16
17
 
17
18
  def initialize
19
+ @enabled = true
18
20
  @workers = 2
19
21
  @reload = false
20
22
  end
@@ -1,35 +1,69 @@
1
1
  module SuckerPunch
2
2
  module Backgroundable
3
3
 
4
+ module CallMethod
5
+ private
6
+
7
+ def instantiate?(options)
8
+ return true if SuckerPunch::Backgroundable.configuration.reload && !(!options[:reload].nil? && options[:reload] == false)
9
+ options[:reload]
10
+ end
11
+
12
+ def load(receiver)
13
+ receiver.respond_to?(:id) ? receiver.class.find(receiver.id) : receiver
14
+ end
15
+
16
+ def call(receiver, method, *args)
17
+ if defined?(ActiveRecord)
18
+ begin
19
+ ActiveRecord::Base.connection_pool.with_connection do
20
+ receiver.send(method, *args)
21
+ end
22
+ ensure
23
+ ActiveRecord::Base.connection_handler.clear_active_connections!
24
+ end
25
+ else
26
+ receiver.send(method, *args)
27
+ end
28
+ end
29
+ end
30
+
4
31
  class Job
5
32
  include SuckerPunch::Job
6
33
  workers SuckerPunch::Backgroundable.configuration.workers
34
+ include CallMethod
7
35
 
8
36
  def perform(receiver, method, args, options)
9
37
  receiver = load(receiver) if instantiate?(options)
10
- if defined?(ActiveRecord)
11
- ActiveRecord::Base.connection_pool.with_connection do
12
- receiver.send(method, *args)
13
- end
14
- else
15
- receiver.send(method, *args)
16
- end
38
+ call(receiver, method, *args)
17
39
  end
18
40
 
19
41
  def later(sec, *args)
20
42
  after(sec) { perform(*args) }
21
43
  end
44
+ end
45
+
46
+ class JobRunner
47
+ include CallMethod
22
48
 
23
- private
49
+ def initialize(receiver, method, args, options)
50
+ @receiver, @method, @args, @options = receiver, method, args, options
51
+ end
24
52
 
25
- def instantiate?(options)
26
- return true if SuckerPunch::Backgroundable.configuration.reload && !(!options[:reload].nil? && options[:reload] == false)
27
- options[:reload]
28
- end
29
-
30
- def load(receiver)
31
- receiver.respond_to?(:id) ? receiver.class.find(receiver.id) : receiver
53
+ def run(seconds = 0)
54
+ if SuckerPunch::Backgroundable.configuration.enabled
55
+ # run as SuckerPunch Job
56
+ if seconds > 0
57
+ Job.new.async.later(seconds, @receiver, @method, @args, @options)
58
+ else
59
+ Job.new.async.perform(@receiver, @method, @args, @options)
60
+ end
61
+ else
62
+ # run without SuckerPunch or Celluloid
63
+ @receiver = load(@receiver) if instantiate?(@options)
64
+ call(@receiver, @method, *@args)
32
65
  end
66
+ end
33
67
  end
34
68
 
35
69
  end
@@ -28,7 +28,7 @@ describe "sucker_punch-backgroundable" do
28
28
  check_queue(200, 2.2)
29
29
  end
30
30
 
31
- it "allows running instance methods with a specified delay, even when they are declared as :always_bacground" do
31
+ it "allows running instance methods with a specified delay, even when they are declared as :always_background" do
32
32
  @obj.later(2).always1 # run after 2 seconds
33
33
  check_queue(1, 2.2)
34
34
  end
@@ -86,7 +86,7 @@ describe "sucker_punch-backgroundable" do
86
86
  TestClass.queue.length.should eq(1)
87
87
  TestClass.queue.pop.should eq(value)
88
88
  end
89
-
89
+
90
90
  context 'reloading' do
91
91
  before do
92
92
  require File.expand_path(File.dirname(__FILE__) + '/load_active_record')
@@ -123,10 +123,19 @@ describe "sucker_punch-backgroundable" do
123
123
  end
124
124
 
125
125
  context 'global reload option' do
126
- it "honors when global reload is set to true" do
126
+ before do
127
127
  SuckerPunch::Backgroundable.configure do |config|
128
128
  config.reload = true
129
129
  end
130
+ end
131
+
132
+ after do
133
+ SuckerPunch::Backgroundable.configure do |config|
134
+ config.reload = false
135
+ end
136
+ end
137
+
138
+ it "honors when global reload is set to true" do
130
139
  @model.later(0.4).copy_value
131
140
  @model.value += 1 # change object
132
141
  sleep(0.5)
@@ -134,9 +143,6 @@ describe "sucker_punch-backgroundable" do
134
143
  end
135
144
 
136
145
  it "honors :reload => false even when when global reload is set to true" do
137
- SuckerPunch::Backgroundable.configure do |config|
138
- config.reload = true
139
- end
140
146
  @model.later(0.4, :reload => false).copy_value
141
147
  @model.value += 1 # change object
142
148
  sleep(0.5)
@@ -144,9 +150,6 @@ describe "sucker_punch-backgroundable" do
144
150
  end
145
151
 
146
152
  it "ignores :reload for class methods" do
147
- SuckerPunch::Backgroundable.configure do |config|
148
- config.reload = true
149
- end
150
153
  TestModel.class_always
151
154
  sleep(0.5)
152
155
  TestModel.queue.pop.should eq(7)
@@ -155,4 +158,41 @@ describe "sucker_punch-backgroundable" do
155
158
  end
156
159
  end
157
160
 
161
+ context 'enable / disable' do
162
+ before do
163
+ SuckerPunch::Backgroundable.configure do |config|
164
+ config.enabled = false
165
+ end
166
+ @obj = TestClass.new
167
+ TestClass.clear
168
+ end
169
+
170
+ it "respects enabled => false for methods marked as :always_background" do
171
+ @obj.always1
172
+ TestClass.queue.length.should eq(1)
173
+ TestClass.queue.pop.should eq(1)
174
+ end
175
+
176
+ it "respects enabled => false for calls to 'background'" do
177
+ @obj.background.normal(6)
178
+ TestClass.queue.length.should eq(1)
179
+ TestClass.queue.pop.should eq(6)
180
+ end
181
+
182
+ it "respects enabled => false for calls to 'later'" do
183
+ @obj.later(10).normal(6)
184
+ TestClass.queue.length.should eq(1)
185
+ TestClass.queue.pop.should eq(6)
186
+ end
187
+
188
+ it "respects enabled => false for methods marked as :always_background (with reloading)" do
189
+ require File.expand_path(File.dirname(__FILE__) + '/load_active_record')
190
+ require File.expand_path(File.dirname(__FILE__) + '/test_model')
191
+ @model = TestModel.create(:value => 5)
192
+ @model.copy_value_in_background
193
+ TestModel.queue.pop.should eq(5)
194
+ end
195
+
196
+ end
197
+
158
198
  end
@@ -0,0 +1,82 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
+ # -*- encoding: utf-8 -*-
5
+ # stub: sucker_punch-backgroundable 0.2.0 ruby lib
6
+
7
+ Gem::Specification.new do |s|
8
+ s.name = "sucker_punch-backgroundable"
9
+ s.version = "0.2.0"
10
+
11
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
+ s.require_paths = ["lib"]
13
+ s.authors = ["Micha\u{eb}l Van Damme"]
14
+ s.date = "2014-05-20"
15
+ s.description = "This gem allows you to background any method call without having to write a special job class. Heavily inspired by the Backgroundable module in TorqueBox."
16
+ s.email = "michael.vandamme@vub.ac.be"
17
+ s.extra_rdoc_files = [
18
+ "LICENSE.txt",
19
+ "README.md"
20
+ ]
21
+ s.files = [
22
+ ".document",
23
+ ".rspec",
24
+ ".travis",
25
+ "CHANGELOG.md",
26
+ "Gemfile",
27
+ "Gemfile.lock",
28
+ "LICENSE.txt",
29
+ "README.md",
30
+ "Rakefile",
31
+ "VERSION",
32
+ "lib/sucker_punch-backgroundable.rb",
33
+ "lib/sucker_punch/backgroundable/backgroundable.rb",
34
+ "lib/sucker_punch/backgroundable/config.rb",
35
+ "lib/sucker_punch/backgroundable/job.rb",
36
+ "lib/sucker_punch/backgroundable/util.rb",
37
+ "spec/load_active_record.rb",
38
+ "spec/spec_helper.rb",
39
+ "spec/sucker_punch-backgroundable_spec.rb",
40
+ "spec/test_class.rb",
41
+ "spec/test_model.rb",
42
+ "sucker_punch-backgroundable.gemspec"
43
+ ]
44
+ s.homepage = "http://github.com/mvdamme/sucker_punch-backgroundable"
45
+ s.licenses = ["LGPL-3"]
46
+ s.rubygems_version = "2.2.2"
47
+ s.summary = "Easily execute an object's methods in the background with sucker_punch"
48
+
49
+ if s.respond_to? :specification_version then
50
+ s.specification_version = 4
51
+
52
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
53
+ s.add_runtime_dependency(%q<sucker_punch>, ["~> 1.0"])
54
+ s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
55
+ s.add_development_dependency(%q<rdoc>, ["~> 3.12"])
56
+ s.add_development_dependency(%q<bundler>, ["~> 1.0"])
57
+ s.add_development_dependency(%q<jeweler>, ["~> 2.0.1"])
58
+ s.add_development_dependency(%q<simplecov>, [">= 0"])
59
+ s.add_development_dependency(%q<activerecord>, [">= 0"])
60
+ s.add_development_dependency(%q<sqlite3>, [">= 0"])
61
+ else
62
+ s.add_dependency(%q<sucker_punch>, ["~> 1.0"])
63
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
64
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
65
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
66
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
67
+ s.add_dependency(%q<simplecov>, [">= 0"])
68
+ s.add_dependency(%q<activerecord>, [">= 0"])
69
+ s.add_dependency(%q<sqlite3>, [">= 0"])
70
+ end
71
+ else
72
+ s.add_dependency(%q<sucker_punch>, ["~> 1.0"])
73
+ s.add_dependency(%q<rspec>, ["~> 2.8.0"])
74
+ s.add_dependency(%q<rdoc>, ["~> 3.12"])
75
+ s.add_dependency(%q<bundler>, ["~> 1.0"])
76
+ s.add_dependency(%q<jeweler>, ["~> 2.0.1"])
77
+ s.add_dependency(%q<simplecov>, [">= 0"])
78
+ s.add_dependency(%q<activerecord>, [">= 0"])
79
+ s.add_dependency(%q<sqlite3>, [">= 0"])
80
+ end
81
+ end
82
+
metadata CHANGED
@@ -1,29 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sucker_punch-backgroundable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michaël Van Damme
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-05-19 00:00:00.000000000 Z
11
+ date: 2014-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sucker_punch
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ~>
18
18
  - !ruby/object:Gem::Version
19
- version: 1.0.0
19
+ version: '1.0'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ~>
25
25
  - !ruby/object:Gem::Version
26
- version: 1.0.0
26
+ version: '1.0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: rspec
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -151,6 +151,7 @@ files:
151
151
  - spec/sucker_punch-backgroundable_spec.rb
152
152
  - spec/test_class.rb
153
153
  - spec/test_model.rb
154
+ - sucker_punch-backgroundable.gemspec
154
155
  homepage: http://github.com/mvdamme/sucker_punch-backgroundable
155
156
  licenses:
156
157
  - LGPL-3