backgrounded-resque 2.0.0.pre1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +13 -0
- data/.rvmrc +5 -0
- data/Gemfile +4 -0
- data/LICENSE +20 -0
- data/README.rdoc +37 -0
- data/Rakefile +10 -0
- data/backgrounded.gemspec +30 -0
- data/lib/backgrounded/handler/pseudo_resque_handler.rb +14 -0
- data/lib/backgrounded/handler/resque_handler.rb +37 -0
- data/lib/backgrounded/version.rb +3 -0
- data/test/backgrounded/handler/resque_handler_test.rb +120 -0
- data/test/database.yml +3 -0
- data/test/test_helper.rb +26 -0
- metadata +152 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Ryan Sonnek
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
= Backgrounded
|
2
|
+
|
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.
|
6
|
+
|
7
|
+
This library is an extension which add support for the Resque job processing framework
|
8
|
+
see http://github.com/defunkt/resque/
|
9
|
+
|
10
|
+
= Features
|
11
|
+
* execute class or instance level methods within resque background workers
|
12
|
+
* supports enqueing jobs into custom queues
|
13
|
+
|
14
|
+
= Installation
|
15
|
+
|
16
|
+
Bundler Gemfile configuration
|
17
|
+
|
18
|
+
gem 'backgrounded-resque'
|
19
|
+
|
20
|
+
Backgrounded Configuration
|
21
|
+
|
22
|
+
# config/initializers/backgrounded.rb
|
23
|
+
require 'backgrounded/handler/resque_handler'
|
24
|
+
Backgrounded.handler = Backgrounded::Handler::ResqueHandler.new
|
25
|
+
|
26
|
+
= Usage
|
27
|
+
user = User.new
|
28
|
+
|
29
|
+
# perform work on the default backgrounded queue
|
30
|
+
user.backgrounded.do_something
|
31
|
+
|
32
|
+
# perform work on a custom queue
|
33
|
+
user.backgrounded(:queue => 'custom').do_something_else
|
34
|
+
|
35
|
+
== Copyright
|
36
|
+
|
37
|
+
Copyright (c) 2012 Ryan Sonnek. See LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "backgrounded/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "backgrounded-resque"
|
7
|
+
s.version = Backgrounded::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Ryan Sonnek"]
|
10
|
+
s.email = ["ryan@codecrate.com"]
|
11
|
+
s.homepage = "http://github.com/wireframe/backgrounded"
|
12
|
+
s.summary = %q{Simple API to perform work in the background}
|
13
|
+
s.description = %q{Execute any ActiveRecord Model method in the background}
|
14
|
+
|
15
|
+
s.rubyforge_project = "backgrounded"
|
16
|
+
|
17
|
+
s.add_runtime_dependency(%q<backgrounded>, [">= 2.0.0.pre1"])
|
18
|
+
s.add_runtime_dependency(%q<resque>, [">= 0.17.1"])
|
19
|
+
s.add_development_dependency(%q<shoulda>, [">= 0"])
|
20
|
+
s.add_development_dependency(%q<resque_unit>, [">= 0.3.7"])
|
21
|
+
s.add_development_dependency(%q<resque_unit>, [">= 0.3.7"])
|
22
|
+
s.add_development_dependency(%q<mocha>, [">= 0"])
|
23
|
+
s.add_development_dependency(%q<sqlite3-ruby>, [">= 1.3.2"])
|
24
|
+
s.add_development_dependency(%q<rake>, [">= 0.9.2.2"])
|
25
|
+
|
26
|
+
s.files = `git ls-files`.split("\n")
|
27
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
28
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
29
|
+
s.require_paths = ["lib"]
|
30
|
+
end
|
@@ -0,0 +1,14 @@
|
|
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, options={})
|
10
|
+
object.send method, *Resque.decode(Resque.encode(args))
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,37 @@
|
|
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, options={})
|
12
|
+
@@queue = options[:queue] || DEFAULT_QUEUE
|
13
|
+
instance, id = instance_identifiers(object)
|
14
|
+
Resque.enqueue(ResqueHandler, instance, id, method, *args)
|
15
|
+
end
|
16
|
+
def self.queue
|
17
|
+
@@queue
|
18
|
+
end
|
19
|
+
def self.perform(clazz, id, method, *args)
|
20
|
+
find_instance(clazz, id, method).send(method, *args)
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
def self.find_instance(clazz, id, method)
|
25
|
+
clazz = clazz.constantize
|
26
|
+
id.to_i == INVALID_ID ? clazz : clazz.find(id)
|
27
|
+
end
|
28
|
+
def instance_identifiers(object)
|
29
|
+
instance, id = if object.is_a?(Class)
|
30
|
+
[object.name, INVALID_ID]
|
31
|
+
else
|
32
|
+
[object.class.name, object.id]
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,120 @@
|
|
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
|
+
def do_stuff
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class Post < ActiveRecord::Base
|
26
|
+
def do_stuff
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
class Blog < ActiveRecord::Base
|
31
|
+
class << self
|
32
|
+
def do_stuff
|
33
|
+
end
|
34
|
+
end
|
35
|
+
def do_stuff
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context 'when backgrounded is configured with resque' do
|
40
|
+
setup do
|
41
|
+
Resque.reset!
|
42
|
+
@handler = Backgrounded::Handler::ResqueHandler.new
|
43
|
+
Backgrounded.handler = @handler
|
44
|
+
end
|
45
|
+
|
46
|
+
context 'a class level backgrounded method' do
|
47
|
+
context "invoking backgrounded method" do
|
48
|
+
setup do
|
49
|
+
Blog.backgrounded.do_stuff
|
50
|
+
end
|
51
|
+
should "enqueue job to resque" do
|
52
|
+
assert_queued Backgrounded::Handler::ResqueHandler, [Blog.to_s, -1, 'do_stuff']
|
53
|
+
assert_equal Backgrounded::Handler::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
|
54
|
+
end
|
55
|
+
context "running background job" do
|
56
|
+
setup do
|
57
|
+
Blog.expects(:do_stuff)
|
58
|
+
Resque.run!
|
59
|
+
end
|
60
|
+
should "invoke method on class" do end #see expectations
|
61
|
+
end
|
62
|
+
end
|
63
|
+
context 'with an instance level backgrounded method of the same name' do
|
64
|
+
setup do
|
65
|
+
@blog = Blog.create
|
66
|
+
@blog.backgrounded.do_stuff
|
67
|
+
end
|
68
|
+
should "enqueue instance method job to resque" do
|
69
|
+
assert_queued Backgrounded::Handler::ResqueHandler, [Blog.to_s, @blog.id, 'do_stuff']
|
70
|
+
assert_equal Backgrounded::Handler::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
|
71
|
+
end
|
72
|
+
context "running background job" do
|
73
|
+
setup do
|
74
|
+
Blog.expects(:do_stuff).never
|
75
|
+
Blog.any_instance.expects(:do_stuff)
|
76
|
+
Resque.run!
|
77
|
+
end
|
78
|
+
should "invoke method on instance" do end #see expectations
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'a persisted object with a single backgrounded method' do
|
84
|
+
setup do
|
85
|
+
@user = User.create
|
86
|
+
end
|
87
|
+
context "invoking backgrounded method" do
|
88
|
+
setup do
|
89
|
+
@user.backgrounded.do_stuff
|
90
|
+
end
|
91
|
+
should "enqueue job to resque" do
|
92
|
+
assert_queued Backgrounded::Handler::ResqueHandler, [User.to_s, @user.id, 'do_stuff']
|
93
|
+
assert_equal Backgrounded::Handler::ResqueHandler::DEFAULT_QUEUE, Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
|
94
|
+
end
|
95
|
+
context "running background job" do
|
96
|
+
should "invoke method on user object" do
|
97
|
+
User.any_instance.expects(:do_stuff)
|
98
|
+
Resque.run!
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
context 'a persisted object with backgrounded method with options' do
|
104
|
+
setup do
|
105
|
+
@post = Post.create
|
106
|
+
end
|
107
|
+
context "invoking backgrounded method" do
|
108
|
+
setup do
|
109
|
+
@post.backgrounded(:queue => 'important').do_stuff
|
110
|
+
end
|
111
|
+
should "use configured queue" do
|
112
|
+
assert_equal 'important', Backgrounded::Handler::ResqueHandler.queue
|
113
|
+
assert_equal 'important', Resque.queue_from_class(Backgrounded::Handler::ResqueHandler)
|
114
|
+
assert_equal 1, Resque.queue('important').length
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
data/test/database.yml
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'test/unit'
|
11
|
+
require 'shoulda'
|
12
|
+
require 'mocha'
|
13
|
+
require 'active_record'
|
14
|
+
|
15
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
16
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
17
|
+
require 'backgrounded'
|
18
|
+
|
19
|
+
Backgrounded.logger.level = Logger::DEBUG
|
20
|
+
|
21
|
+
config = YAML::load(IO.read(File.join(File.dirname(__FILE__), 'database.yml')))
|
22
|
+
ActiveRecord::Base.logger = Logger.new(File.join(File.dirname(__FILE__), "debug.log"))
|
23
|
+
ActiveRecord::Base.establish_connection(config[ENV['DB'] || 'sqlite'])
|
24
|
+
|
25
|
+
class Test::Unit::TestCase
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,152 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: backgrounded-resque
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 2.0.0.pre1
|
5
|
+
prerelease: 6
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ryan Sonnek
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-05-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: backgrounded
|
16
|
+
requirement: &2179202580 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 2.0.0.pre1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2179202580
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: resque
|
27
|
+
requirement: &2179202060 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 0.17.1
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *2179202060
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: shoulda
|
38
|
+
requirement: &2179201580 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *2179201580
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: resque_unit
|
49
|
+
requirement: &2179201100 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.3.7
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *2179201100
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: resque_unit
|
60
|
+
requirement: &2179200620 !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: *2179200620
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: mocha
|
71
|
+
requirement: &2179200140 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ! '>='
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *2179200140
|
80
|
+
- !ruby/object:Gem::Dependency
|
81
|
+
name: sqlite3-ruby
|
82
|
+
requirement: &2179199660 !ruby/object:Gem::Requirement
|
83
|
+
none: false
|
84
|
+
requirements:
|
85
|
+
- - ! '>='
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: 1.3.2
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: *2179199660
|
91
|
+
- !ruby/object:Gem::Dependency
|
92
|
+
name: rake
|
93
|
+
requirement: &2179199180 !ruby/object:Gem::Requirement
|
94
|
+
none: false
|
95
|
+
requirements:
|
96
|
+
- - ! '>='
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: 0.9.2.2
|
99
|
+
type: :development
|
100
|
+
prerelease: false
|
101
|
+
version_requirements: *2179199180
|
102
|
+
description: Execute any ActiveRecord Model method in the background
|
103
|
+
email:
|
104
|
+
- ryan@codecrate.com
|
105
|
+
executables: []
|
106
|
+
extensions: []
|
107
|
+
extra_rdoc_files: []
|
108
|
+
files:
|
109
|
+
- .gitignore
|
110
|
+
- .rvmrc
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE
|
113
|
+
- README.rdoc
|
114
|
+
- Rakefile
|
115
|
+
- backgrounded.gemspec
|
116
|
+
- lib/backgrounded/handler/pseudo_resque_handler.rb
|
117
|
+
- lib/backgrounded/handler/resque_handler.rb
|
118
|
+
- lib/backgrounded/version.rb
|
119
|
+
- test/backgrounded/handler/resque_handler_test.rb
|
120
|
+
- test/database.yml
|
121
|
+
- test/test_helper.rb
|
122
|
+
homepage: http://github.com/wireframe/backgrounded
|
123
|
+
licenses: []
|
124
|
+
post_install_message:
|
125
|
+
rdoc_options: []
|
126
|
+
require_paths:
|
127
|
+
- lib
|
128
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
129
|
+
none: false
|
130
|
+
requirements:
|
131
|
+
- - ! '>='
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
segments:
|
135
|
+
- 0
|
136
|
+
hash: 3144533850957425411
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
none: false
|
139
|
+
requirements:
|
140
|
+
- - ! '>'
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: 1.3.1
|
143
|
+
requirements: []
|
144
|
+
rubyforge_project: backgrounded
|
145
|
+
rubygems_version: 1.8.15
|
146
|
+
signing_key:
|
147
|
+
specification_version: 3
|
148
|
+
summary: Simple API to perform work in the background
|
149
|
+
test_files:
|
150
|
+
- test/backgrounded/handler/resque_handler_test.rb
|
151
|
+
- test/database.yml
|
152
|
+
- test/test_helper.rb
|