sucker_punch 0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +95 -0
- data/Rakefile +12 -0
- data/lib/sucker_punch.rb +29 -0
- data/lib/sucker_punch/exceptions.rb +6 -0
- data/lib/sucker_punch/queue.rb +9 -0
- data/lib/sucker_punch/version.rb +3 -0
- data/lib/sucker_punch/worker.rb +7 -0
- data/spec/queue_spec.rb +15 -0
- data/spec/spec_helper.rb +6 -0
- data/spec/sucker_punch_spec.rb +50 -0
- data/spec/worker_spec.rb +15 -0
- data/sucker_punch.gemspec +24 -0
- metadata +113 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Brandon Hilkert
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,95 @@
|
|
1
|
+
# Sucker Punch
|
2
|
+
|
3
|
+
Sucker Punch is a Ruby asynchronous processing using Celluloid, heavily influenced by Sidekiq and girl_friday. With Celluloid's actor pattern, we use do asynchronous processing within a single process. This reduces costs of hosting on a service like Heroku along with the memory footprint of having to maintain additional workers if hosting on a dedicated server.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'sucker_punch'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install sucker_punch
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
Configuration:
|
22
|
+
|
23
|
+
`app/config/sucker_punch.rb`
|
24
|
+
|
25
|
+
```Ruby
|
26
|
+
SuckerPunch.config do
|
27
|
+
queue name: :log_queue, worker: LogWorker, size: 10
|
28
|
+
queue name: :awesome_queue, worker: AwesomeWorker, size: 2
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
Workers:
|
33
|
+
|
34
|
+
`app/workers/log_worker.rb`
|
35
|
+
|
36
|
+
```Ruby
|
37
|
+
class LogWorker
|
38
|
+
include SuckerPunch::Worker
|
39
|
+
|
40
|
+
def perform(event)
|
41
|
+
Log.new(event).track
|
42
|
+
end
|
43
|
+
end
|
44
|
+
```
|
45
|
+
|
46
|
+
All workers should define an instance method `perform`, of which the job being queued will adhere to.
|
47
|
+
|
48
|
+
Workers interacting with `ActiveRecord` should take special precaution not to exhause connections in the pool. This can be done with `ActiveRecord::Base.connection_pool.with_connection`, which ensures the connection is returned back to the pool when completed.
|
49
|
+
|
50
|
+
`app/workers/awesome_worker.rb`
|
51
|
+
|
52
|
+
```Ruby
|
53
|
+
class AwesomeWorker
|
54
|
+
include SuckerPunch::Worker
|
55
|
+
|
56
|
+
def perform(user_id)
|
57
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
58
|
+
user = User.find(user_id)
|
59
|
+
user.update_attributes(is_awesome: true)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
```
|
64
|
+
|
65
|
+
Queues:
|
66
|
+
|
67
|
+
```Ruby
|
68
|
+
SuckerPunch::Queue[:log_queue] # Is just the class LogWorker
|
69
|
+
```
|
70
|
+
|
71
|
+
Synchronous:
|
72
|
+
|
73
|
+
```Ruby
|
74
|
+
SuckerPunch::Queue[:log_queue].perform("login")
|
75
|
+
```
|
76
|
+
|
77
|
+
Asynchronous:
|
78
|
+
|
79
|
+
```Ruby
|
80
|
+
SuckerPunch::Queue[:log_queue].async.perform("login") # => nil
|
81
|
+
```
|
82
|
+
|
83
|
+
## Gem Name
|
84
|
+
|
85
|
+
With all due respect, [@jmazzi](https://twitter.com/jmazzi) is completely responsible for the name, which is totally awesome. If you're looking for a name for something, he is the one to go to.
|
86
|
+
|
87
|
+
## Contributing
|
88
|
+
|
89
|
+
1. Fork it
|
90
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
91
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
92
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
93
|
+
5. Create new Pull Request
|
94
|
+
|
95
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
2
|
+
require 'rspec/core/rake_task'
|
3
|
+
|
4
|
+
RSpec::Core::RakeTask.new('spec')
|
5
|
+
|
6
|
+
# If you want to make this the default task
|
7
|
+
task :default => :spec
|
8
|
+
task :test => :spec
|
9
|
+
|
10
|
+
task :console do
|
11
|
+
exec "irb -r sucker_punch -I ./lib"
|
12
|
+
end
|
data/lib/sucker_punch.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'celluloid'
|
2
|
+
require 'sucker_punch/exceptions'
|
3
|
+
|
4
|
+
module SuckerPunch
|
5
|
+
extend self
|
6
|
+
|
7
|
+
def config(&block)
|
8
|
+
instance_eval &block
|
9
|
+
end
|
10
|
+
|
11
|
+
def queue(options = {})
|
12
|
+
raise SuckerPunch::MissingQueueName unless options[:name]
|
13
|
+
raise SuckerPunch::MissingWorkerName unless options[:worker]
|
14
|
+
|
15
|
+
klass = options.fetch(:worker)
|
16
|
+
registry_name = options.fetch(:name)
|
17
|
+
size = options.fetch(:size, nil)
|
18
|
+
|
19
|
+
Celluloid::Actor[registry_name] = if size
|
20
|
+
klass.send(:pool, size: size)
|
21
|
+
else
|
22
|
+
klass.send(:pool)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'sucker_punch/queue'
|
28
|
+
require 'sucker_punch/worker'
|
29
|
+
require 'sucker_punch/version'
|
data/spec/queue_spec.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class FakeWorker
|
4
|
+
include Celluloid
|
5
|
+
end
|
6
|
+
|
7
|
+
describe SuckerPunch::Queue do
|
8
|
+
describe ".[]" do
|
9
|
+
it "delegates to Celluloid" do
|
10
|
+
Celluloid::Actor[:fake] = FakeWorker.pool
|
11
|
+
Celluloid::Actor.should_receive(:[]).with(:fake)
|
12
|
+
SuckerPunch::Queue[:fake]
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class FakeWorker
|
4
|
+
include Celluloid
|
5
|
+
end
|
6
|
+
|
7
|
+
describe SuckerPunch do
|
8
|
+
context "config" do
|
9
|
+
context "properly configured" do
|
10
|
+
before(:all) do
|
11
|
+
SuckerPunch.config do
|
12
|
+
queue name: :crazy_queue, worker: FakeWorker, size: 7
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
it "turns the class into an actor" do
|
17
|
+
Celluloid::Actor[:crazy_queue].should be_a(FakeWorker)
|
18
|
+
Celluloid::Actor[:crazy_queue].methods.should include(:async)
|
19
|
+
end
|
20
|
+
|
21
|
+
it "allow asynchrounous processing" do
|
22
|
+
end
|
23
|
+
|
24
|
+
it "sets worker size" do
|
25
|
+
Celluloid::Actor[:crazy_queue].size.should == 7
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
context "with no queue name" do
|
30
|
+
it "raises an exception" do
|
31
|
+
expect {
|
32
|
+
SuckerPunch.config do
|
33
|
+
queue worker: FakeWorker
|
34
|
+
end
|
35
|
+
}.to raise_error(SuckerPunch::MissingQueueName)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
context "with no worker name" do
|
40
|
+
it "raises an exception" do
|
41
|
+
expect {
|
42
|
+
SuckerPunch.config do
|
43
|
+
queue name: :fake
|
44
|
+
end
|
45
|
+
}.to raise_error(SuckerPunch::MissingWorkerName)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
end
|
data/spec/worker_spec.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
class FakeWorker
|
4
|
+
include SuckerPunch::Worker
|
5
|
+
|
6
|
+
def perform
|
7
|
+
puts "do stuff"
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe SuckerPunch::Worker do
|
12
|
+
it "should include Celluloid into requesting class when included" do
|
13
|
+
FakeWorker.should respond_to(:pool)
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'sucker_punch/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "sucker_punch"
|
8
|
+
gem.version = SuckerPunch::VERSION
|
9
|
+
gem.authors = ["Brandon Hilkert"]
|
10
|
+
gem.email = ["brandonhilkert@gmail.com"]
|
11
|
+
gem.description = %q{Asynchronous processing library for Ruby}
|
12
|
+
gem.summary = %q{Sucker Punch is a Ruby asynchronous processing using Celluloid, heavily influenced by Sidekiq and girl_friday.}
|
13
|
+
gem.homepage = "https://github.com/brandonhilkert/sucker_punch"
|
14
|
+
|
15
|
+
gem.files = `git ls-files`.split($/)
|
16
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
17
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
18
|
+
gem.require_paths = ["lib"]
|
19
|
+
|
20
|
+
gem.add_development_dependency "rspec"
|
21
|
+
gem.add_development_dependency "rake"
|
22
|
+
|
23
|
+
gem.add_dependency "celluloid", "> 0.11"
|
24
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sucker_punch
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: '0.1'
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Brandon Hilkert
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-01-19 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: rake
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '0'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '0'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: celluloid
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>'
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0.11'
|
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: '0.11'
|
62
|
+
description: Asynchronous processing library for Ruby
|
63
|
+
email:
|
64
|
+
- brandonhilkert@gmail.com
|
65
|
+
executables: []
|
66
|
+
extensions: []
|
67
|
+
extra_rdoc_files: []
|
68
|
+
files:
|
69
|
+
- .gitignore
|
70
|
+
- Gemfile
|
71
|
+
- LICENSE.txt
|
72
|
+
- README.md
|
73
|
+
- Rakefile
|
74
|
+
- lib/sucker_punch.rb
|
75
|
+
- lib/sucker_punch/exceptions.rb
|
76
|
+
- lib/sucker_punch/queue.rb
|
77
|
+
- lib/sucker_punch/version.rb
|
78
|
+
- lib/sucker_punch/worker.rb
|
79
|
+
- spec/queue_spec.rb
|
80
|
+
- spec/spec_helper.rb
|
81
|
+
- spec/sucker_punch_spec.rb
|
82
|
+
- spec/worker_spec.rb
|
83
|
+
- sucker_punch.gemspec
|
84
|
+
homepage: https://github.com/brandonhilkert/sucker_punch
|
85
|
+
licenses: []
|
86
|
+
post_install_message:
|
87
|
+
rdoc_options: []
|
88
|
+
require_paths:
|
89
|
+
- lib
|
90
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
91
|
+
none: false
|
92
|
+
requirements:
|
93
|
+
- - ! '>='
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '0'
|
96
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
requirements: []
|
103
|
+
rubyforge_project:
|
104
|
+
rubygems_version: 1.8.23
|
105
|
+
signing_key:
|
106
|
+
specification_version: 3
|
107
|
+
summary: Sucker Punch is a Ruby asynchronous processing using Celluloid, heavily influenced
|
108
|
+
by Sidekiq and girl_friday.
|
109
|
+
test_files:
|
110
|
+
- spec/queue_spec.rb
|
111
|
+
- spec/spec_helper.rb
|
112
|
+
- spec/sucker_punch_spec.rb
|
113
|
+
- spec/worker_spec.rb
|