backgrounder 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format=d
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in backgrounder.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Zbigniew Humeniuk
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ # Backgrounder
2
+
3
+ Create background jobs using Redis with ease.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'backgrounder'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install backgrounder
18
+
19
+ ## Usage
20
+
21
+ class Dummy < ActiveRecord::Base
22
+ include Backgrounder::Handler
23
+
24
+ @queue = :high
25
+
26
+ def self.foo
27
+ # ...
28
+ end
29
+
30
+ def bar
31
+ # ...
32
+ end
33
+ end
34
+
35
+ dummy = Dummy.create
36
+
37
+ Backgrounder::Placer.new(Dummy, :foo).place
38
+
39
+ Backgrounder::Placer.new(dummy, :bar, queue: :processing).place
40
+
41
+
42
+ class Puppet
43
+ include Backgrounder::Handler
44
+
45
+ def initialize args={}
46
+ # ...
47
+ end
48
+
49
+ def foo
50
+ # ...
51
+ end
52
+ end
53
+
54
+ Backgrounder::Placer.new(Puppet, :foo, args: {
55
+ init_args: {content: 'foo', phone: 'bar'}
56
+ }, queue: :critical).place # Puppet.new('content' => 'foo', 'phone' => 'bar'}).foo
57
+
58
+ ## Contributing
59
+
60
+ 1. Fork it
61
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
62
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
63
+ 4. Push to the branch (`git push origin my-new-feature`)
64
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'backgrounder/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "backgrounder"
8
+ spec.version = Backgrounder::VERSION
9
+ spec.authors = ["Zbigniew Humeniuk"]
10
+ spec.email = ["zbigniew.humeniuk@gmail.com"]
11
+ spec.description = %q{Create background jobs using Redis with ease.}
12
+ spec.summary = %q{Create background jobs using Redis with ease.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency 'activesupport', '>= 3.0.0'
22
+ spec.add_dependency 'resque', '>= 1.24.1'
23
+ spec.add_dependency 'json', '>= 1.7.7'
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec", "~> 2.13.0"
28
+ spec.add_development_dependency "resque_spec", "~> 0.13.0"
29
+ end
@@ -0,0 +1,10 @@
1
+ module Backgrounder
2
+ end
3
+
4
+ require 'active_support/concern' unless defined?(ActiveSupport::Concern)
5
+ require 'resque'
6
+
7
+ require "backgrounder/version"
8
+ require "backgrounder/handler"
9
+ require "backgrounder/placer"
10
+ require "backgrounder/resque/queue"
@@ -0,0 +1,21 @@
1
+ module Backgrounder::Handler
2
+ extend ActiveSupport::Concern
3
+
4
+ module ClassMethods
5
+ def perform(id, action, *args)
6
+ case id
7
+ when 'new'
8
+ obj = if args.any? && args.first.is_a?(Hash) && args.first['init_args']
9
+ new args.first['init_args']
10
+ else
11
+ new
12
+ end
13
+ obj.send action
14
+ when nil
15
+ send action
16
+ else
17
+ find(id).send(action)
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,33 @@
1
+ class Backgrounder::Placer
2
+ def initialize(obj, method_name, opts = {})
3
+ if obj.class == Class
4
+ @klass = obj
5
+ @obj_id = opts[:args] && opts[:args][:init_args] ? 'new' : nil
6
+ else
7
+ @klass = obj.class
8
+ @obj_id = fetch_id obj
9
+ end
10
+ @method_name = method_name
11
+ @queue = opts[:queue] ? opts[:queue] : fetch_queue_name
12
+ @args = opts[:args] ? opts[:args] : nil
13
+ end
14
+
15
+ def place
16
+ Resque::Job.create @queue, @klass, @obj_id, @method_name, @args
17
+ end
18
+
19
+
20
+ private
21
+
22
+ def fetch_queue_name
23
+ @klass.instance_variable_get "@queue"
24
+ end
25
+
26
+ def fetch_id obj
27
+ if obj.respond_to? :id
28
+ obj.id ? obj.id : 'new'
29
+ else
30
+ 'new'
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,8 @@
1
+ # coding: utf-8
2
+ module Backgrounder::Resque
3
+ module Queue
4
+ Critical = :critical
5
+ High = :high
6
+ Low = :low
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module Backgrounder
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,9 @@
1
+ class Backgrounder::Dummy
2
+ include Backgrounder::Handler
3
+
4
+ @queue = Backgrounder::Resque::Queue::High
5
+
6
+ def id
7
+ 123
8
+ end
9
+ end
@@ -0,0 +1,41 @@
1
+ require 'spec_helper'
2
+
3
+ describe Backgrounder::Handler do
4
+ it "includes perform class method" do
5
+ Backgrounder::Dummy.stub(:perform){ |arg1, arg2| true }
6
+ Backgrounder::Dummy.perform(1, :dummy_method).should be_true
7
+ end
8
+
9
+ context "id is set" do
10
+ it "calls method passed as second arg on object with ID passed as first arg" do
11
+ Backgrounder::Dummy.stub(:find).with(1){ Backgrounder::Dummy.new }
12
+ Backgrounder::Dummy.any_instance.stub(:dummy_method){ true }
13
+ Backgrounder::Dummy.should_receive(:find).with(1)
14
+ Backgrounder::Dummy.any_instance.should_receive(:dummy_method)
15
+ Backgrounder::Dummy.perform(1, :dummy_method)
16
+ end
17
+ end
18
+
19
+ context "id is nil" do
20
+ it "calls class method passed as second arg" do
21
+ Backgrounder::Dummy.stub(:dummy_method){ true }
22
+ Backgrounder::Dummy.should_receive(:dummy_method)
23
+ Backgrounder::Dummy.perform(nil, :dummy_method)
24
+ end
25
+ end
26
+
27
+ context "id is 'new'" do
28
+ it "calls method on new object" do
29
+ Backgrounder::Dummy.any_instance.stub(:dummy_method){ true }
30
+ Backgrounder::Dummy.any_instance.should_receive(:dummy_method)
31
+ Backgrounder::Dummy.perform('new', :dummy_method)
32
+ end
33
+
34
+ it "initialize new object with init_args" do
35
+ dummy = double('backgrounder_dummy')
36
+ Backgrounder::Dummy.stub(:new).with({'foo' => 'bar', 'fuz' => 'buz'}).and_return{ dummy }
37
+ dummy.should_receive(:dummy_method).once
38
+ Backgrounder::Dummy.perform 'new', :dummy_method, {'init_args' => {'foo' => 'bar', 'fuz' => 'buz'}}
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe Backgrounder::Placer do
4
+ describe "instance methods" do
5
+ describe "#initialize" do
6
+ it "can get 3 parameters" do
7
+ Backgrounder::Placer.new(Backgrounder::Dummy.new, :dummy_method, queue: :processing).
8
+ should be_instance_of(Backgrounder::Placer)
9
+ end
10
+ end
11
+
12
+ describe "#place" do
13
+ it "adds dummy_method to the processing queue of Backgrounder::Dummy" do
14
+ Backgrounder::Placer.new(Backgrounder::Dummy.new, :dummy_method, queue: :processing).place
15
+ Backgrounder::Dummy.should have_queued(123, :dummy_method, nil).in(:processing)
16
+ end
17
+
18
+ it "adds dummy_method to the default queue of Backgrounder::Dummy" do
19
+ Backgrounder::Placer.new(Backgrounder::Dummy.new, :dummy_method).place
20
+ Backgrounder::Dummy.should have_queued(123, :dummy_method, nil).in(:high)
21
+ end
22
+
23
+ it "adds nil as first arg of queue if placer works on class" do
24
+ Backgrounder::Placer.new(Backgrounder::Dummy, :dummy_method).place
25
+ Backgrounder::Dummy.should have_queued(nil, :dummy_method, nil)
26
+ end
27
+
28
+ it "adds args to queue" do
29
+ Backgrounder::Placer.new(Backgrounder::Dummy.new, :dummy_method, args: {'foo' => {'bar' => 'baz'}}).place
30
+ Backgrounder::Dummy.should have_queued(123, :dummy_method, {'foo' => {'bar' => 'baz'}})
31
+ end
32
+
33
+ it "adds new as first arg if object's ID is nil" do
34
+ obj = Backgrounder::Dummy.new
35
+ obj.stub(:id){ nil }
36
+ Backgrounder::Placer.new(obj, :dummy_method).place
37
+ Backgrounder::Dummy.should have_queued('new', :dummy_method, nil)
38
+ end
39
+
40
+ it "adds new as first arg if object does not respond to id" do
41
+ obj = Backgrounder::Dummy.new
42
+ obj.stub(:id){ raise NoMethodError, 'undefined method id' }
43
+ obj.stub(:respond_to?).with(:id).and_return{ false }
44
+ Backgrounder::Placer.new(obj, :dummy_method).place
45
+ Backgrounder::Dummy.should have_queued('new', :dummy_method, nil)
46
+ end
47
+
48
+ it "adds new as first arg if placer works on class and init_args are passed" do
49
+ Backgrounder::Placer.new(Backgrounder::Dummy, :dummy_method, args: {init_args: {'foo' => 'bar'}}).place
50
+ Backgrounder::Dummy.should have_queued('new', :dummy_method, {init_args: {'foo' => 'bar'}})
51
+ end
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,13 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+
4
+ require 'bundler/setup'
5
+ require 'resque_spec'
6
+
7
+ Bundler.require
8
+
9
+ Dir["#{File.dirname(__FILE__)}/fixtures/**/*.rb"].each{|f| require f}
10
+
11
+ RSpec.configure do |config|
12
+ config.order = "random"
13
+ end
metadata ADDED
@@ -0,0 +1,178 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: backgrounder
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Zbigniew Humeniuk
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-05-09 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.0.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.0.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: resque
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: 1.24.1
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: 1.24.1
46
+ - !ruby/object:Gem::Dependency
47
+ name: json
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: 1.7.7
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: 1.7.7
62
+ - !ruby/object:Gem::Dependency
63
+ name: bundler
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ~>
68
+ - !ruby/object:Gem::Version
69
+ version: '1.3'
70
+ type: :development
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ~>
76
+ - !ruby/object:Gem::Version
77
+ version: '1.3'
78
+ - !ruby/object:Gem::Dependency
79
+ name: rake
80
+ requirement: !ruby/object:Gem::Requirement
81
+ none: false
82
+ requirements:
83
+ - - ! '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ type: :development
87
+ prerelease: false
88
+ version_requirements: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ - !ruby/object:Gem::Dependency
95
+ name: rspec
96
+ requirement: !ruby/object:Gem::Requirement
97
+ none: false
98
+ requirements:
99
+ - - ~>
100
+ - !ruby/object:Gem::Version
101
+ version: 2.13.0
102
+ type: :development
103
+ prerelease: false
104
+ version_requirements: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ~>
108
+ - !ruby/object:Gem::Version
109
+ version: 2.13.0
110
+ - !ruby/object:Gem::Dependency
111
+ name: resque_spec
112
+ requirement: !ruby/object:Gem::Requirement
113
+ none: false
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: 0.13.0
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ~>
124
+ - !ruby/object:Gem::Version
125
+ version: 0.13.0
126
+ description: Create background jobs using Redis with ease.
127
+ email:
128
+ - zbigniew.humeniuk@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - .gitignore
134
+ - .rspec
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - backgrounder.gemspec
140
+ - lib/backgrounder.rb
141
+ - lib/backgrounder/handler.rb
142
+ - lib/backgrounder/placer.rb
143
+ - lib/backgrounder/resque/queue.rb
144
+ - lib/backgrounder/version.rb
145
+ - spec/fixtures/backgrounder/dummy.rb
146
+ - spec/lib/backgrounder/handler_spec.rb
147
+ - spec/lib/backgrounder/placer_spec.rb
148
+ - spec/spec_helper.rb
149
+ homepage: ''
150
+ licenses:
151
+ - MIT
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ none: false
158
+ requirements:
159
+ - - ! '>='
160
+ - !ruby/object:Gem::Version
161
+ version: '0'
162
+ required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ! '>='
166
+ - !ruby/object:Gem::Version
167
+ version: '0'
168
+ requirements: []
169
+ rubyforge_project:
170
+ rubygems_version: 1.8.24
171
+ signing_key:
172
+ specification_version: 3
173
+ summary: Create background jobs using Redis with ease.
174
+ test_files:
175
+ - spec/fixtures/backgrounder/dummy.rb
176
+ - spec/lib/backgrounder/handler_spec.rb
177
+ - spec/lib/backgrounder/placer_spec.rb
178
+ - spec/spec_helper.rb