backgrounded 0.7.5 → 2.0.0.pre1

Sign up to get free protection for your applications and to get access to all the features.
data/.rvmrc CHANGED
@@ -1,4 +1,4 @@
1
- rvm use ree-1.8.7@backgrounded --create
1
+ rvm use 1.9.3@backgrounded --create
2
2
  if ! command -v bundle ; then
3
3
  gem install bundler
4
4
  bundle install --local
data/README.rdoc CHANGED
@@ -11,27 +11,21 @@ Backgrounded provides a thin wrapper around any background processing framework
11
11
 
12
12
  = Usage
13
13
 
14
- #declaration
15
14
  class User
16
- backgrounded :do_stuff
17
15
  def do_stuff
18
- # do all your work here
16
+ end
17
+ def self.do_something_else
19
18
  end
20
19
  end
21
20
 
22
- #usage
23
21
  user = User.new
24
- user.do_stuff_backgrounded
25
-
26
- = Installation
27
-
28
- Command line installation
22
+ # execute instance method in background
23
+ user.backgrounded.do_stuff
29
24
 
30
- sudo gem install backgrounded
25
+ # execute class method in background
26
+ User.backgrounded.do_something_else
31
27
 
32
- Rails environment.rb configuration
33
-
34
- config.gem 'backgrounded'
28
+ = Installation
35
29
 
36
30
  Bundler Gemfile configuration
37
31
 
@@ -39,30 +33,14 @@ Bundler Gemfile configuration
39
33
 
40
34
  = Configuration
41
35
 
42
- Backgrounded includes several configurable implementations out of the box for most popular background frameworks.
43
- If your framework isn't included, it's trivial to write your own implementation.
44
- Submit a patch and we may consider it for official distribution!
45
-
46
- == DelayedJob
47
- see http://github.com/tobi/delayed_job/tree/master
48
-
49
- # config/initializers/backgrounded.rb
50
- require 'backgrounded/handler/delayed_job_handler'
51
- Backgrounded.handler = Backgrounded::Handler::DelayedJobHandler.new
36
+ Backgrounded packages handlers for popular frameworks in separate gems. Just drop in the gem for your particular framework or write your own!
52
37
 
53
38
  == Resque
54
- see http://github.com/defunkt/resque/
55
-
56
- # config/initializers/backgrounded.rb
57
- require 'backgrounded/handler/resque_handler'
58
- Backgrounded.handler = Backgrounded::Handler::ResqueHandler.new
39
+ see http://github.com/wireframe/backgrounded-resque
59
40
 
60
41
  == JobFu
61
42
  see http://github.com/jnstq/job_fu/tree
62
43
 
63
- # config/initializers/backgrounded.rb
64
- Backgrounded.handler = JobFu::Backgrounded::Handler.new
65
-
66
44
  == Custom Handlers
67
45
 
68
46
  # config/initializers/backgrounded.rb
@@ -75,4 +53,4 @@ see http://github.com/jnstq/job_fu/tree
75
53
 
76
54
  == Copyright
77
55
 
78
- Copyright (c) 2009 Ryan Sonnek. See LICENSE for details.
56
+ Copyright (c) 2012 Ryan Sonnek. See LICENSE for details.
data/backgrounded.gemspec CHANGED
@@ -9,7 +9,7 @@ Gem::Specification.new do |s|
9
9
  s.authors = ["Ryan Sonnek"]
10
10
  s.email = ["ryan@codecrate.com"]
11
11
  s.homepage = "http://github.com/wireframe/backgrounded"
12
- s.summary = %q{Simple API to run Model methods in the background}
12
+ s.summary = %q{Simple API to perform work in the background}
13
13
  s.description = %q{Execute any ActiveRecord Model method in the background}
14
14
 
15
15
  s.rubyforge_project = "backgrounded"
@@ -17,11 +17,8 @@ Gem::Specification.new do |s|
17
17
  s.add_runtime_dependency(%q<activerecord>, [">= 2.3.10"])
18
18
  s.add_runtime_dependency(%q<activesupport>, [">= 2.3.10"])
19
19
  s.add_development_dependency(%q<shoulda>, [">= 0"])
20
- s.add_development_dependency(%q<resque>, [">= 0.17.1"])
21
- s.add_development_dependency(%q<resque_unit>, [">= 0.3.7"])
22
20
  s.add_development_dependency(%q<mocha>, [">= 0"])
23
21
  s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.2"])
24
- s.add_development_dependency(%q<delayed_job>, [">= 2.1.4"])
25
22
  s.add_development_dependency(%q<rake>, [">= 0.9.2.2"])
26
23
 
27
24
  s.files = `git ls-files`.split("\n")
data/lib/backgrounded.rb CHANGED
@@ -1,20 +1,30 @@
1
1
  require 'active_support/all'
2
2
 
3
- require File.join(File.dirname(__FILE__), 'backgrounded', 'class_methods')
4
3
  require File.join(File.dirname(__FILE__), 'backgrounded', 'handler', 'inprocess_handler')
5
-
6
- Object.send(:include, Backgrounded::ClassMethods)
4
+ require File.join(File.dirname(__FILE__), 'backgrounded', 'proxy')
7
5
 
8
6
  module Backgrounded
7
+ extend ActiveSupport::Concern
8
+
9
9
  class << self
10
10
  attr_accessor :logger, :handler
11
- def method_name_for_backgrounded_options(method_name)
12
- method_basename, punctuation = method_name.to_s.sub(/([?!=])$/, ''), $1
13
- "#{method_basename}_backgrounded_options"
11
+ end
12
+
13
+ module ClassMethods
14
+ def backgrounded(options={})
15
+ Backgrounded::Proxy.new self, options
14
16
  end
15
17
  end
18
+
19
+ # @param options (optional) options to pass into the backgrounded handler
20
+ def backgrounded(options={})
21
+ Backgrounded::Proxy.new self, options
22
+ end
16
23
  end
17
24
 
25
+ # include backgrounded into any ruby object
26
+ Object.send(:include, Backgrounded)
27
+
18
28
  # default handler to the basic in process handler
19
29
  Backgrounded.handler = Backgrounded::Handler::InprocessHandler.new
20
30
 
@@ -3,7 +3,7 @@ module Backgrounded
3
3
  #simple handler to process synchronously and not actually in the background
4
4
  #useful for testing
5
5
  class InprocessHandler
6
- def request(object, method, *args)
6
+ def request(object, method, args, options={})
7
7
  object.send method, *args
8
8
  end
9
9
  end
@@ -3,7 +3,7 @@ module Backgrounded
3
3
  # throw requests out the window
4
4
  # useful for test environment to avoid any background work
5
5
  class NoOpHandler
6
- def request(object, method, *args)
6
+ def request(object, method, args, options={})
7
7
  # do nothing
8
8
  end
9
9
  end
@@ -0,0 +1,14 @@
1
+ module Backgrounded
2
+ class Proxy
3
+ def initialize(delegate, options={})
4
+ @delegate = delegate
5
+ @options = options
6
+ end
7
+
8
+ def method_missing(method_name, *args)
9
+ Backgrounded.logger.debug("Requesting #{Backgrounded.handler} backgrounded method: #{method_name} for instance #{@delegate}")
10
+ Backgrounded.handler.request(@delegate, method_name, args, @options)
11
+ nil
12
+ end
13
+ end
14
+ end
@@ -1,3 +1,3 @@
1
1
  module Backgrounded
2
- VERSION = "0.7.5"
2
+ VERSION = "2.0.0.pre1"
3
3
  end
@@ -1,154 +1,45 @@
1
1
  require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class BackgroundedTest < Test::Unit::TestCase
4
-
5
- class User
6
- backgrounded :do_stuff
7
-
8
- def do_stuff
9
- end
10
- end
11
-
12
- class Person
13
- backgrounded :do_stuff
14
-
15
- def do_stuff(name, place, location)
16
- end
17
- end
18
-
19
- class Post
20
- backgrounded :do_stuff, :notify_users
21
4
 
5
+ class User
22
6
  def do_stuff
23
7
  end
24
- def notify_users
8
+ def self.do_something_else
25
9
  end
26
10
  end
27
11
 
28
- class Comment
29
- backgrounded :delete_spam!
30
-
31
- def delete_spam!
12
+ context '#backgrounded' do
13
+ should 'be defined for class' do
14
+ assert User.respond_to?(:backgrounded)
32
15
  end
33
- end
34
-
35
- class Dog
36
- backgrounded :bark => {:priority => :low}
37
-
38
- def bark
16
+ should 'be defined for instance' do
17
+ assert User.new.respond_to?(:backgrounded)
39
18
  end
40
- end
41
-
42
- class Entry
43
- backgrounded :do_stuff
44
- backgrounded :notify_users
45
-
46
- def do_stuff
47
- end
48
- def notify_users
49
- end
50
- end
51
-
52
- class Blog
53
- class << self
54
- backgrounded :update_info
55
- def update_info
19
+ context 'invoking on class' do
20
+ setup do
21
+ @result = User.backgrounded
22
+ end
23
+ should 'return instance of Backgrounded::Proxy' do
24
+ assert @result.is_a?(Backgrounded::Proxy)
56
25
  end
57
26
  end
58
- end
59
-
60
- context 'an object with a single backgrounded method' do
61
- setup do
62
- @user = User.new
63
- end
64
- should 'define backgrounded method' do
65
- assert @user.respond_to?('do_stuff_backgrounded')
66
- end
67
- should 'define backgrounded_options method' do
68
- assert @user.respond_to?('do_stuff_backgrounded_options')
69
- end
70
- should 'save backgrounded options for method' do
71
- assert_equal({}, @user.do_stuff_backgrounded_options)
72
- end
73
- context 'executing backgrounded method' do
27
+ context 'invoking on an instance' do
74
28
  setup do
75
- @user.expects(:do_stuff).returns(true)
76
- @result = @user.do_stuff_backgrounded
29
+ @user = User.new
30
+ @result = @user.backgrounded
77
31
  end
78
- should "execute method in background" do end #see expectations
79
- should 'return nil for backgrounded method' do
80
- assert_nil @result
32
+ should 'return instance of Backgrounded::Proxy' do
33
+ assert @result.is_a?(Backgrounded::Proxy)
81
34
  end
82
35
  end
83
- end
84
-
85
- context 'an object with a backgrounded method that accepts parameters' do
86
- setup do
87
- @person = Person.new
88
- end
89
- should 'forward them' do
90
- @person.expects(:do_stuff).with('ryan', 2, 3)
91
- @person.do_stuff_backgrounded('ryan', 2, 3)
92
- end
93
- end
94
-
95
- context 'an object with a backgrounded method included punctuation' do
96
- setup do
97
- @comment = Comment.new
98
- end
99
- should 'move punctuation to the end of the new method' do
100
- assert @comment.respond_to?(:'delete_spam_backgrounded!')
101
- end
102
- end
103
-
104
- context 'an object with multiple backgrounded methods' do
105
- setup do
106
- @post = Post.new
107
- end
108
- should "execute method either method in background" do
109
- @post.expects(:do_stuff)
110
- @post.do_stuff_backgrounded
111
-
112
- @post.expects(:notify_users)
113
- @post.notify_users_backgrounded
114
- end
115
- end
116
-
117
- context 'an object with multiple backgrounded invokations' do
118
- setup do
119
- @post = Entry.new
120
- end
121
- should "setup options for both methods" do
122
- assert_equal({}, @post.do_stuff_backgrounded_options)
123
- assert_equal({}, @post.notify_users_backgrounded_options)
124
- end
125
- end
126
-
127
- context 'an object with backgrounded method options' do
128
- setup do
129
- @dog = Dog.new
130
- end
131
- should 'save method options for future use' do
132
- assert_equal({:priority => :low}, @dog.bark_backgrounded_options)
133
- end
134
- end
135
-
136
- context 'a class with backgrounded method' do
137
- should 'define backgrounded method' do
138
- assert Blog.respond_to?('update_info_backgrounded')
139
- end
140
- should 'defined backgrounded_options method' do
141
- assert Blog.respond_to?('update_info_backgrounded_options')
142
- end
143
- should 'save backgrounded options for method' do
144
- assert_equal({}, Blog.update_info_backgrounded_options)
145
- end
146
- context 'invoking backgrounded method' do
36
+ context 'invoking with options' do
147
37
  setup do
148
- Blog.expects(:update_info)
149
- Blog.update_info_backgrounded
38
+ @result = User.backgrounded(:priority => :high)
39
+ end
40
+ should 'initialize proxy with options' do
41
+ assert_equal({:priority => :high}, @result.instance_variable_get(:@options))
150
42
  end
151
- should 'invoke class method' do end #see expectations
152
43
  end
153
44
  end
154
45
  end
@@ -0,0 +1,49 @@
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
+
3
+ class ProxyTest < Test::Unit::TestCase
4
+
5
+ class User
6
+ def do_stuff
7
+ end
8
+ def self.do_something_else
9
+ end
10
+ end
11
+
12
+ class Person
13
+ def do_stuff(name, place, location)
14
+ end
15
+ end
16
+
17
+ context 'invoking delegate method' do
18
+ context 'with arguments' do
19
+ setup do
20
+ @delegate = Person.new
21
+ @delegate.expects(:do_stuff).with('foo', 1, 'bar')
22
+ @proxy = Backgrounded::Proxy.new @delegate
23
+ @result = @proxy.do_stuff 'foo', 1, 'bar'
24
+ end
25
+ should "execute method on delegate" do end #see expectations
26
+ should 'return nil' do
27
+ assert_nil @result
28
+ end
29
+ end
30
+ context 'with no arguments' do
31
+ setup do
32
+ @delegate = User.new
33
+ @delegate.expects(:do_stuff)
34
+ @proxy = Backgrounded::Proxy.new @delegate
35
+ @result = @proxy.do_stuff
36
+ end
37
+ should "execute method on delegate" do end #see expectations
38
+ end
39
+ context 'context when proxy is configured with options' do
40
+ setup do
41
+ Backgrounded.handler.expects(:request).with(anything, anything, anything, {:priority => :high})
42
+ @delegate = User
43
+ @proxy = Backgrounded::Proxy.new @delegate, :priority => :high
44
+ @proxy.do_something_else
45
+ end
46
+ should 'pass options onto Backgrounded.handler' do end # see expectations
47
+ end
48
+ end
49
+ end
metadata CHANGED
@@ -1,19 +1,19 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backgrounded
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.5
5
- prerelease:
4
+ version: 2.0.0.pre1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ryan Sonnek
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-06-04 00:00:00.000000000 Z
12
+ date: 2012-05-22 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &2183008840 !ruby/object:Gem::Requirement
16
+ requirement: &2178080800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: 2.3.10
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *2183008840
24
+ version_requirements: *2178080800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: activesupport
27
- requirement: &2183008320 !ruby/object:Gem::Requirement
27
+ requirement: &2178080260 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: 2.3.10
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *2183008320
35
+ version_requirements: *2178080260
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: shoulda
38
- requirement: &2183007840 !ruby/object:Gem::Requirement
38
+ requirement: &2178079780 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,32 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *2183007840
47
- - !ruby/object:Gem::Dependency
48
- name: resque
49
- requirement: &2183007360 !ruby/object:Gem::Requirement
50
- none: false
51
- requirements:
52
- - - ! '>='
53
- - !ruby/object:Gem::Version
54
- version: 0.17.1
55
- type: :development
56
- prerelease: false
57
- version_requirements: *2183007360
58
- - !ruby/object:Gem::Dependency
59
- name: resque_unit
60
- requirement: &2183006880 !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
63
- - - ! '>='
64
- - !ruby/object:Gem::Version
65
- version: 0.3.7
66
- type: :development
67
- prerelease: false
68
- version_requirements: *2183006880
46
+ version_requirements: *2178079780
69
47
  - !ruby/object:Gem::Dependency
70
48
  name: mocha
71
- requirement: &2183006400 !ruby/object:Gem::Requirement
49
+ requirement: &2178079300 !ruby/object:Gem::Requirement
72
50
  none: false
73
51
  requirements:
74
52
  - - ! '>='
@@ -76,10 +54,10 @@ dependencies:
76
54
  version: '0'
77
55
  type: :development
78
56
  prerelease: false
79
- version_requirements: *2183006400
57
+ version_requirements: *2178079300
80
58
  - !ruby/object:Gem::Dependency
81
59
  name: sqlite3-ruby
82
- requirement: &2183005920 !ruby/object:Gem::Requirement
60
+ requirement: &2178078820 !ruby/object:Gem::Requirement
83
61
  none: false
84
62
  requirements:
85
63
  - - ! '>='
@@ -87,21 +65,10 @@ dependencies:
87
65
  version: 1.3.2
88
66
  type: :development
89
67
  prerelease: false
90
- version_requirements: *2183005920
91
- - !ruby/object:Gem::Dependency
92
- name: delayed_job
93
- requirement: &2183005440 !ruby/object:Gem::Requirement
94
- none: false
95
- requirements:
96
- - - ! '>='
97
- - !ruby/object:Gem::Version
98
- version: 2.1.4
99
- type: :development
100
- prerelease: false
101
- version_requirements: *2183005440
68
+ version_requirements: *2178078820
102
69
  - !ruby/object:Gem::Dependency
103
70
  name: rake
104
- requirement: &2183004960 !ruby/object:Gem::Requirement
71
+ requirement: &2178078340 !ruby/object:Gem::Requirement
105
72
  none: false
106
73
  requirements:
107
74
  - - ! '>='
@@ -109,7 +76,7 @@ dependencies:
109
76
  version: 0.9.2.2
110
77
  type: :development
111
78
  prerelease: false
112
- version_requirements: *2183004960
79
+ version_requirements: *2178078340
113
80
  description: Execute any ActiveRecord Model method in the background
114
81
  email:
115
82
  - ryan@codecrate.com
@@ -125,19 +92,13 @@ files:
125
92
  - Rakefile
126
93
  - backgrounded.gemspec
127
94
  - lib/backgrounded.rb
128
- - lib/backgrounded/class_methods.rb
129
- - lib/backgrounded/handler/delayed_job_handler.rb
130
95
  - lib/backgrounded/handler/inprocess_handler.rb
131
96
  - lib/backgrounded/handler/no_op_handler.rb
132
- - lib/backgrounded/handler/pseudo_resque_handler.rb
133
- - lib/backgrounded/handler/resque_handler.rb
134
- - lib/backgrounded/handler/workling_handler.rb
97
+ - lib/backgrounded/proxy.rb
135
98
  - lib/backgrounded/version.rb
136
- - test/backgrounded/handler/delayed_job_handler_test.rb
137
- - test/backgrounded/handler/resque_handler_test.rb
138
- - test/backgrounded/handler/workling_handler_test.rb
139
99
  - test/backgrounded_test.rb
140
100
  - test/database.yml
101
+ - test/proxy_test.rb
141
102
  - test/test_helper.rb
142
103
  homepage: http://github.com/wireframe/backgrounded
143
104
  licenses: []
@@ -153,26 +114,21 @@ required_ruby_version: !ruby/object:Gem::Requirement
153
114
  version: '0'
154
115
  segments:
155
116
  - 0
156
- hash: -49604688935868797
117
+ hash: -2552094692552709134
157
118
  required_rubygems_version: !ruby/object:Gem::Requirement
158
119
  none: false
159
120
  requirements:
160
- - - ! '>='
121
+ - - ! '>'
161
122
  - !ruby/object:Gem::Version
162
- version: '0'
163
- segments:
164
- - 0
165
- hash: -49604688935868797
123
+ version: 1.3.1
166
124
  requirements: []
167
125
  rubyforge_project: backgrounded
168
126
  rubygems_version: 1.8.15
169
127
  signing_key:
170
128
  specification_version: 3
171
- summary: Simple API to run Model methods in the background
129
+ summary: Simple API to perform work in the background
172
130
  test_files:
173
- - test/backgrounded/handler/delayed_job_handler_test.rb
174
- - test/backgrounded/handler/resque_handler_test.rb
175
- - test/backgrounded/handler/workling_handler_test.rb
176
131
  - test/backgrounded_test.rb
177
132
  - test/database.yml
133
+ - test/proxy_test.rb
178
134
  - test/test_helper.rb
@@ -1,23 +0,0 @@
1
- module Backgrounded
2
- module ClassMethods
3
- def backgrounded(*args)
4
- options = args.extract_options!
5
- methods_with_options = args.inject({}) do |hash, method| hash[method] = {}; hash end
6
- methods_with_options.merge! options
7
- methods_with_options.each_pair do |method, options|
8
- method_basename, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
9
- backgrounded_method = "#{method_basename}_backgrounded#{punctuation}"
10
- class_eval do
11
- define_method backgrounded_method do |*args|
12
- Backgrounded.logger.debug("Requesting #{Backgrounded.handler} backgrounded method: #{method} for instance #{self}")
13
- Backgrounded.handler.request(self, method, *args)
14
- nil
15
- end
16
- define_method Backgrounded.method_name_for_backgrounded_options(method) do
17
- options
18
- end
19
- end
20
- end
21
- end
22
- end
23
- end
@@ -1,13 +0,0 @@
1
- require 'delayed_job'
2
-
3
- module Backgrounded
4
- module Handler
5
- # invoke the operation in the background using delayed job
6
- # see http://github.com/tobi/delayed_job/tree/master
7
- class DelayedJobHandler
8
- def request(object, method, *args)
9
- object.send_later(method.to_sym, *args)
10
- end
11
- end
12
- end
13
- end
@@ -1,14 +0,0 @@
1
- require 'resque'
2
-
3
- # handler that acts like the in process handler but marshalls the arguments
4
- # this simulates how resque encodes/decodes values to/from redis
5
- # useful when passing symbols to arguments and they end up being processed as strings
6
- module Backgrounded
7
- module Handler
8
- class PseudoResqueHandler
9
- def request(object, method, *args)
10
- object.send method, *Resque.decode(Resque.encode(args))
11
- end
12
- end
13
- end
14
- end
@@ -1,38 +0,0 @@
1
- require 'resque'
2
-
3
- module Backgrounded
4
- module Handler
5
- #enque requests in resque
6
- class ResqueHandler
7
- DEFAULT_QUEUE = 'backgrounded'
8
- INVALID_ID = -1
9
- @@queue = DEFAULT_QUEUE
10
-
11
- def request(object, method, *args)
12
- options = object.send Backgrounded.method_name_for_backgrounded_options(method)
13
- @@queue = options[:queue] || DEFAULT_QUEUE
14
- instance, id = instance_identifiers(object)
15
- Resque.enqueue(ResqueHandler, instance, id, method, *args)
16
- end
17
- def self.queue
18
- @@queue
19
- end
20
- def self.perform(clazz, id, method, *args)
21
- find_instance(clazz, id, method).send(method, *args)
22
- end
23
-
24
- private
25
- def self.find_instance(clazz, id, method)
26
- clazz = clazz.constantize
27
- id.to_i == INVALID_ID ? clazz : clazz.find(id)
28
- end
29
- def instance_identifiers(object)
30
- instance, id = if object.kind_of?(ActiveRecord::Base)
31
- [object.class.name, object.id]
32
- else
33
- [object.name, INVALID_ID]
34
- end
35
- end
36
- end
37
- end
38
- end
@@ -1,34 +0,0 @@
1
- class Backgrounded::Handler::WorklingHandler
2
- class BackgroundedWorker < Workling::Base
3
- INVALID_ID = -1
4
- def perform(options = {})
5
- find_instance(options[:class], options[:id], options[:method]).send(options[:method], *options[:params])
6
- end
7
-
8
- private
9
- def find_instance(clazz, id, method)
10
- clazz = clazz.constantize
11
- id.to_i == INVALID_ID ? clazz : clazz.find(id)
12
- end
13
- end
14
-
15
- def request(object, method, *args)
16
- instance, id = instance_identifiers(object)
17
- options = {
18
- :class => instance,
19
- :id => id,
20
- :method => method,
21
- :params => args
22
- }
23
- BackgroundedWorker.async_perform options
24
- end
25
-
26
- private
27
- def instance_identifiers(object)
28
- instance, id = if object.is_a?(Class)
29
- [object.name, INVALID_ID]
30
- else
31
- [object.class.name, object.id]
32
- end
33
- end
34
- end
@@ -1,56 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
- require 'backgrounded/handler/delayed_job_handler'
3
- require 'delayed/backend/active_record'
4
-
5
- ActiveRecord::Schema.define(:version => 1) do
6
- create_table :users, :force => true do |t|
7
- t.column :name, :string
8
- end
9
- create_table :delayed_jobs, :force => true do |table|
10
- table.integer :priority, :default => 0 # Allows some jobs to jump to the front of the queue
11
- table.integer :attempts, :default => 0 # Provides for retries, but still fail eventually.
12
- table.text :handler # YAML-encoded string of the object that will do work
13
- table.string :last_error # reason for last failure (See Note below)
14
- table.datetime :run_at # When to run. Could be Time.now for immediately, or sometime in the future.
15
- table.datetime :locked_at # Set when a client is working on this object
16
- table.datetime :failed_at # Set when all retries have failed (actually, by default, the record is deleted instead)
17
- table.string :locked_by # Who is working on this object (if locked)
18
- table.timestamps
19
- end
20
- end
21
-
22
- class DelayedJobHandlerTest < Test::Unit::TestCase
23
-
24
- class User < ActiveRecord::Base
25
- backgrounded :do_stuff
26
-
27
- def do_stuff
28
- end
29
- end
30
-
31
- context 'when backgrounded is configured with delayed_job' do
32
- setup do
33
- Delayed::Worker.backend = :active_record
34
- @handler = Backgrounded::Handler::DelayedJobHandler.new
35
- Backgrounded.handler = @handler
36
- end
37
-
38
- should 'be configured' do
39
- fail 'delayed job not recognizing activerecord config'
40
- end
41
- # context 'a persisted object with a single backgrounded method' do
42
- # setup do
43
- # @user = User.create
44
- # end
45
- # context "invoking backgrounded method" do
46
- # setup do
47
- # @user.do_stuff_backgrounded
48
- # end
49
- # should_create Delayed::Job
50
- # should 'create delayed job' do
51
- # job = Delayed::Job.last
52
- # end
53
- # end
54
- # end
55
- end
56
- end
@@ -1,154 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
- require 'backgrounded/handler/resque_handler'
3
- require 'resque_unit'
4
-
5
- ActiveRecord::Schema.define(:version => 1) do
6
- create_table :blogs, :force => true do |t|
7
- t.column :name, :string
8
- end
9
-
10
- create_table :users, :force => true do |t|
11
- t.column :name, :string
12
- end
13
-
14
- create_table :posts, :force => true do |t|
15
- t.column :title, :string
16
- end
17
- end
18
-
19
- class ResqueHandlerTest < Test::Unit::TestCase
20
- class User < ActiveRecord::Base
21
- backgrounded :do_stuff
22
-
23
- def do_stuff
24
- end
25
- end
26
-
27
- class Post < ActiveRecord::Base
28
- backgrounded :do_stuff => {:queue => 'important'}
29
-
30
- def do_stuff
31
- end
32
- end
33
-
34
- class Blog < ActiveRecord::Base
35
- class << self
36
- backgrounded :do_stuff
37
-
38
- def do_stuff
39
- end
40
- end
41
- backgrounded :do_stuff
42
- def do_stuff
43
- end
44
- end
45
-
46
- module Foo
47
- class << self
48
- backgrounded :bar
49
- def bar
50
- end
51
- end
52
- end
53
-
54
- context 'when backgrounded is configured with resque' do
55
- setup do
56
- Resque.reset!
57
- @handler = Backgrounded::Handler::ResqueHandler.new
58
- Backgrounded.handler = @handler
59
- end
60
-
61
- context 'a class level backgrounded method' do
62
- context "invoking backgrounded method" do
63
- setup do
64
- Blog.do_stuff_backgrounded
65
- end
66
- should "enqueue job to resque" do
67
- assert_queued Backgrounded::Handler::ResqueHandler, [Blog.to_s, -1, 'do_stuff']
68
- assert_equal Backgrounded::Handler::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
69
- end
70
- context "running background job" do
71
- setup do
72
- Blog.expects(:do_stuff)
73
- Resque.run!
74
- end
75
- should "invoke method on class" do end #see expectations
76
- end
77
- end
78
- context 'with an instance level backgrounded method of the same name' do
79
- setup do
80
- @blog = Blog.create
81
- @blog.do_stuff_backgrounded
82
- end
83
- should "enqueue instance method job to resque" do
84
- assert_queued Backgrounded::Handler::ResqueHandler, [Blog.to_s, @blog.id, 'do_stuff']
85
- assert_equal Backgrounded::Handler::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
86
- end
87
- context "running background job" do
88
- setup do
89
- Blog.expects(:do_stuff).never
90
- Blog.any_instance.expects(:do_stuff)
91
- Resque.run!
92
- end
93
- should "invoke method on instance" do end #see expectations
94
- end
95
- end
96
- end
97
-
98
- context 'a persisted object with a single backgrounded method' do
99
- setup do
100
- @user = User.create
101
- end
102
- context "invoking backgrounded method" do
103
- setup do
104
- @user.do_stuff_backgrounded
105
- end
106
- should "enqueue job to resque" do
107
- assert_queued Backgrounded::Handler::ResqueHandler, [User.to_s, @user.id, 'do_stuff']
108
- assert_equal Backgrounded::Handler::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
109
- end
110
- context "running background job" do
111
- should "invoke method on user object" do
112
- User.any_instance.expects(:do_stuff)
113
- Resque.run!
114
- end
115
- end
116
- end
117
-
118
- context 'a persisted object with backgrounded method with options' do
119
- setup do
120
- @post = Post.create
121
- end
122
- context "invoking backgrounded method" do
123
- setup do
124
- @post.do_stuff_backgrounded
125
- end
126
- should "use configured queue" do
127
- assert_equal 'important', Backgrounded::Handler::ResqueHandler.queue
128
- assert_equal 'important', Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
129
- assert_equal 1, Resque.queue('important').length
130
- end
131
- end
132
- end
133
- end
134
-
135
- context 'with a module backgrounded class method' do
136
- context 'when invoking class method backgrounded' do
137
- setup do
138
- Foo.bar_backgrounded
139
- end
140
- should "enqueue job to resque" do
141
- assert_queued Backgrounded::Resque::ResqueHandler, [Foo.to_s, -1, 'bar']
142
- assert_equal Backgrounded::Resque::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Resque::ResqueHandler)
143
- end
144
- context 'when processing job' do
145
- setup do
146
- Foo.expects(:bar)
147
- Resque.run!
148
- end
149
- should 'invoke module class method backgrounded' do end # see expectations
150
- end
151
- end
152
- end
153
- end
154
- end
@@ -1,68 +0,0 @@
1
- require File.join(File.dirname(__FILE__), '..', '..', 'test_helper')
2
- # RAILS_DEFAULT_LOGGER = Logger.new STDOUT
3
- # RAILS_ENV = 'test'
4
- # require 'newrelic_rpm'
5
- # require 'memcache'
6
- # require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'socialcast', 'vendor', 'plugins', 'workling', 'lib', 'workling')
7
- # require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'socialcast', 'vendor', 'plugins', 'workling', 'lib', 'workling', 'base')
8
- # require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'socialcast', 'vendor', 'plugins', 'workling', 'lib', 'workling', 'discovery')
9
- # require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'socialcast', 'vendor', 'plugins', 'workling', 'lib', 'workling', 'routing', 'class_and_method_routing')
10
- # require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'socialcast', 'vendor', 'plugins', 'workling', 'lib', 'workling', 'remote', 'invokers', 'threaded_poller')
11
- # require File.join(File.dirname(__FILE__), '..', '..', '..', '..', 'socialcast', 'vendor', 'plugins', 'workling', 'lib', 'workling', 'remote')
12
- # require 'backgrounded/handler/workling_handler'
13
-
14
- ActiveRecord::Schema.define(:version => 1) do
15
- create_table :users, :force => true do |t|
16
- t.column :name, :string
17
- end
18
- end
19
-
20
- class WorklingHandlerTest < Test::Unit::TestCase
21
-
22
- class User < ActiveRecord::Base
23
- backgrounded :do_stuff
24
-
25
- def do_stuff
26
- end
27
- end
28
-
29
- class Post < ActiveRecord::Base
30
- class << self
31
- backgrounded :do_stuff
32
-
33
- def do_stuff
34
- end
35
- end
36
- end
37
-
38
- context 'when backgrounded is configured with workling' do
39
- setup do
40
- fail 'workling not available as a gem'
41
- @handler = Backgrounded::Handler::WorklingHandler.new
42
- Backgrounded.handler = @handler
43
- end
44
-
45
- context 'a persisted object with a single backgrounded method' do
46
- setup do
47
- @user = User.create
48
- end
49
- context "invoking backgrounded method" do
50
- setup do
51
- User.any_instance.expects(:do_stuff).with('a string')
52
- @user.do_stuff_backgrounded 'a string'
53
- end
54
- should 'dispatch through workling back to the object' do end #see expectations
55
- end
56
- end
57
-
58
- context 'a class level backgrounded method' do
59
- context "invoking backgrounded method" do
60
- setup do
61
- Post.expects(:do_stuff).with('a string')
62
- Post.do_stuff_backgrounded 'a string'
63
- end
64
- should 'dispatch through workling back to the object' do end #see expectations
65
- end
66
- end
67
- end
68
- end