backgrounded 0.4.1 → 0.5.0

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.
data/.gitignore CHANGED
@@ -1,3 +1,4 @@
1
+ .document
1
2
  *.sw?
2
3
  .DS_Store
3
4
  coverage
data/README.rdoc CHANGED
@@ -1,11 +1,13 @@
1
1
  = Backgrounded
2
2
 
3
- Background processing done right.
3
+ The cleanest way to integrate background processing into your application.
4
+
5
+ Backgrounded provides a thin wrapper around any background processing framework that implements the Backgrounded handler API which makes it trivial to swap out processing frameworks with no impact to your code.
4
6
 
5
7
  = Features
6
- * clean and concise API
7
- * testable
8
+ * clean and concise API which removes any dependency on external "worker" jobs and allows you to execute any model method in the background
8
9
  * integrates with any background processing framework (DelayedJob, Resque, JobFu, Workling, etc)
10
+ * background methods can be actually unit tested by using an 'in process' runner
9
11
 
10
12
  = Usage
11
13
 
@@ -31,43 +33,37 @@ Rails environment.rb configuration
31
33
 
32
34
  config.gem 'backgrounded'
33
35
 
34
- Gemfile configuration
36
+ Bundler Gemfile configuration
35
37
 
36
38
  gem 'backgrounded'
37
39
 
38
40
  = Configuration
39
41
 
40
- Backgrounded provides a thin wrapper around any background processing framework that implements the Backgrounded handler API. This makes it trivial to swap out processing frameworks with no impact on your code.
41
-
42
- = DelayedJob
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!
43
45
 
44
- An implementation for DelayedJob is packaged directly with Backgrounded for a slick out of the box experience.
46
+ == DelayedJob
45
47
  see http://github.com/tobi/delayed_job/tree/master
46
48
 
47
49
  # config/initializers/backgrounded.rb
48
50
  require 'backgrounded/handler/delayed_job_handler'
49
51
  Backgrounded.handler = Backgrounded::Handler::DelayedJobHandler.new
50
52
 
51
- = Resque
52
-
53
- An implementation for Resque is packaged directly with Backgrounded for a slick out of the box experience.
53
+ == Resque
54
54
  see http://github.com/defunkt/resque/
55
55
 
56
56
  # config/initializers/backgrounded.rb
57
57
  require 'backgrounded/handler/resque_handler'
58
58
  Backgrounded.handler = Backgrounded::Handler::ResqueHandler.new
59
59
 
60
- = JobFu
61
-
62
- Developers using the JobFu library have it easy as well!
60
+ == JobFu
63
61
  see http://github.com/jnstq/job_fu/tree
64
62
 
65
63
  # config/initializers/backgrounded.rb
66
64
  Backgrounded.handler = JobFu::Backgrounded::Handler.new
67
65
 
68
- = Custom Handlers
69
-
70
- It's trivial to write your own plugin for processing Backgrounded events!
66
+ == Custom Handlers
71
67
 
72
68
  # config/initializers/backgrounded.rb
73
69
  class MyHandler
data/VERSION.yml CHANGED
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :major: 0
3
- :minor: 4
3
+ :minor: 5
4
4
  :build:
5
- :patch: 1
5
+ :patch: 0
data/backgrounded.gemspec CHANGED
@@ -5,19 +5,18 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{backgrounded}
8
- s.version = "0.4.1"
8
+ s.version = "0.5.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ryan Sonnek"]
12
- s.date = %q{2010-09-27}
12
+ s.date = %q{2010-10-01}
13
13
  s.email = %q{ryan.sonnek@gmail.com}
14
14
  s.extra_rdoc_files = [
15
15
  "LICENSE",
16
16
  "README.rdoc"
17
17
  ]
18
18
  s.files = [
19
- ".document",
20
- ".gitignore",
19
+ ".gitignore",
21
20
  "LICENSE",
22
21
  "README.rdoc",
23
22
  "Rakefile",
@@ -1,12 +1,17 @@
1
1
  module Backgrounded
2
2
  module ClassMethods
3
- def backgrounded(*methods)
4
- methods.each do |method|
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|
5
8
  method_basename, punctuation = method.to_s.sub(/([?!=])$/, ''), $1
6
9
  define_method "#{method_basename}_backgrounded#{punctuation}" do |*args|
7
10
  Backgrounded.handler.request(self, method, *args)
8
11
  end
9
12
  end
13
+ cattr_accessor :backgrounded_options
14
+ self.backgrounded_options = methods_with_options
10
15
  end
11
16
  end
12
17
  end
@@ -4,13 +4,16 @@ module Backgrounded
4
4
  module Handler
5
5
  #enque requests in resque
6
6
  class ResqueHandler
7
+ DEFAULT_QUEUE = 'backgrounded'
8
+ @@queue = DEFAULT_QUEUE
9
+
7
10
  def request(object, method, *args)
11
+ @@queue = object.backgrounded_options[method.to_sym][:queue] || DEFAULT_QUEUE
8
12
  Resque.enqueue(ResqueHandler, object.class.name, object.id, method, *args)
9
13
  end
10
14
  def self.queue
11
- 'backgrounded'
15
+ @@queue
12
16
  end
13
-
14
17
  def self.perform(clazz, id, method, *args)
15
18
  clazz.constantize.find(id).send(method, *args)
16
19
  end
@@ -5,6 +5,10 @@ ActiveRecord::Schema.define(:version => 1) do
5
5
  create_table :users, :force => true do |t|
6
6
  t.column :name, :string
7
7
  end
8
+
9
+ create_table :posts, :force => true do |t|
10
+ t.column :title, :string
11
+ end
8
12
  end
9
13
 
10
14
  class User < ActiveRecord::Base
@@ -14,6 +18,13 @@ class User < ActiveRecord::Base
14
18
  end
15
19
  end
16
20
 
21
+ class Post < ActiveRecord::Base
22
+ backgrounded :do_stuff => {:queue => 'important'}
23
+
24
+ def do_stuff
25
+ end
26
+ end
27
+
17
28
  class ResqueHandlerTest < Test::Unit::TestCase
18
29
  context 'when backgrounded is configured with resque' do
19
30
  setup do
@@ -32,6 +43,7 @@ class ResqueHandlerTest < Test::Unit::TestCase
32
43
  end
33
44
  should "enqueue job to resque" do
34
45
  assert_queued Backgrounded::Handler::ResqueHandler
46
+ assert_equal Backgrounded::Handler::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
35
47
  end
36
48
  context "running background job" do
37
49
  should "invoke method on user object" do
@@ -40,6 +52,21 @@ class ResqueHandlerTest < Test::Unit::TestCase
40
52
  end
41
53
  end
42
54
  end
55
+
56
+ context 'a persisted object with backgrounded method with options' do
57
+ setup do
58
+ @post = Post.create
59
+ end
60
+ context "invoking backgrounded method" do
61
+ setup do
62
+ @post.do_stuff_backgrounded
63
+ end
64
+ should "use configured queue" do
65
+ assert_equal 'important', Backgrounded::Handler::ResqueHandler.queue
66
+ assert_equal 'important', Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
67
+ end
68
+ end
69
+ end
43
70
  end
44
71
  end
45
72
  end
@@ -1,4 +1,4 @@
1
- require 'test_helper'
1
+ require File.join(File.dirname(__FILE__), 'test_helper')
2
2
 
3
3
  class User
4
4
  backgrounded :do_stuff
@@ -30,6 +30,13 @@ class Comment
30
30
  end
31
31
  end
32
32
 
33
+ class Dog
34
+ backgrounded :bark => {:priority => :low}
35
+
36
+ def bark
37
+ end
38
+ end
39
+
33
40
  class BackgroundedTest < Test::Unit::TestCase
34
41
  context 'an object with a single backgrounded method' do
35
42
  setup do
@@ -72,4 +79,13 @@ class BackgroundedTest < Test::Unit::TestCase
72
79
  @post.notify_users_backgrounded
73
80
  end
74
81
  end
82
+
83
+ context 'an object with backgrounded method options' do
84
+ setup do
85
+ @dog = Dog.new
86
+ end
87
+ should 'save method options for future use' do
88
+ assert_equal :low, @dog.backgrounded_options[:bark][:priority]
89
+ end
90
+ end
75
91
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: backgrounded
3
3
  version: !ruby/object:Gem::Version
4
- hash: 13
4
+ hash: 11
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 4
9
- - 1
10
- version: 0.4.1
8
+ - 5
9
+ - 0
10
+ version: 0.5.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Sonnek
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-09-27 00:00:00 -05:00
18
+ date: 2010-10-01 00:00:00 -05:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -88,7 +88,6 @@ extra_rdoc_files:
88
88
  - LICENSE
89
89
  - README.rdoc
90
90
  files:
91
- - .document
92
91
  - .gitignore
93
92
  - LICENSE
94
93
  - README.rdoc
data/.document DELETED
@@ -1,5 +0,0 @@
1
- README.rdoc
2
- lib/**/*.rb
3
- bin/*
4
- features/**/*.feature
5
- LICENSE