sucker_punch 0.2 → 0.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.travis.yml +6 -0
- data/CHANGES.md +0 -0
- data/README.md +50 -11
- data/lib/sucker_punch/queue.rb +4 -0
- data/lib/sucker_punch/testing.rb +8 -0
- data/lib/sucker_punch/version.rb +1 -1
- data/spec/queue_spec.rb +15 -3
- data/spec/testing_spec.rb +20 -0
- metadata +7 -2
data/.travis.yml
ADDED
data/CHANGES.md
ADDED
File without changes
|
data/README.md
CHANGED
@@ -1,6 +1,11 @@
|
|
1
1
|
# Sucker Punch
|
2
2
|
|
3
|
-
|
3
|
+
[![Build Status](https://travis-ci.org/brandonhilkert/sucker_punch.png?branch=master)](https://travis-ci.org/brandonhilkert/sucker_punch)
|
4
|
+
[![Code Climate](https://codeclimate.com/github/brandonhilkert/sucker_punch.png)](https://codeclimate.com/github/brandonhilkert/sucker_punch)
|
5
|
+
|
6
|
+
Sucker Punch is a single-process Ruby asynchronous processing library. It's [girl_friday](https://github.com/mperham/girl_friday) with syntax from [Sidekiq](https://github.com/mperham/sidekiq) and DSL sugar on top of [Celluloid](https://github.com/celluloid/celluloid/). With Celluloid's actor pattern, we can 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. All queues can run within a single Rails/Sinatra process.
|
7
|
+
|
8
|
+
Sucker Punch is perfect for asynchronous processes like emailing, data crunching, or social platform manipulation. No reason to hold up a user when you can do these things in the background within the same process as your web application...
|
4
9
|
|
5
10
|
## Installation
|
6
11
|
|
@@ -16,11 +21,9 @@ Or install it yourself as:
|
|
16
21
|
|
17
22
|
$ gem install sucker_punch
|
18
23
|
|
19
|
-
##
|
24
|
+
## Configuration
|
20
25
|
|
21
|
-
|
22
|
-
|
23
|
-
`app/config/sucker_punch.rb`
|
26
|
+
`config/initializers/sucker_punch.rb`
|
24
27
|
|
25
28
|
```Ruby
|
26
29
|
SuckerPunch.config do
|
@@ -29,7 +32,7 @@ Configuration:
|
|
29
32
|
end
|
30
33
|
```
|
31
34
|
|
32
|
-
|
35
|
+
## Usage
|
33
36
|
|
34
37
|
`app/workers/log_worker.rb`
|
35
38
|
|
@@ -45,7 +48,7 @@ end
|
|
45
48
|
|
46
49
|
All workers should define an instance method `perform`, of which the job being queued will adhere to.
|
47
50
|
|
48
|
-
Workers interacting with `ActiveRecord` should take special precaution not to
|
51
|
+
Workers interacting with `ActiveRecord` should take special precaution not to exhaust 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
52
|
|
50
53
|
`app/workers/awesome_worker.rb`
|
51
54
|
|
@@ -62,10 +65,27 @@ class AwesomeWorker
|
|
62
65
|
end
|
63
66
|
```
|
64
67
|
|
68
|
+
We can create a job from within another job:
|
69
|
+
|
70
|
+
```Ruby
|
71
|
+
class AwesomeWorker
|
72
|
+
include SuckerPunch::Worker
|
73
|
+
|
74
|
+
def perform(user_id)
|
75
|
+
ActiveRecord::Base.connection_pool.with_connection do
|
76
|
+
user = User.find(user_id)
|
77
|
+
user.update_attributes(is_awesome: true)
|
78
|
+
SuckerPunch::Queue[:log_queue].async.perform("User #{user.id} became awesome!")
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
```
|
83
|
+
|
65
84
|
Queues:
|
66
85
|
|
67
86
|
```Ruby
|
68
|
-
SuckerPunch::Queue[:log_queue] #
|
87
|
+
SuckerPunch::Queue[:log_queue] # Just a wrapper for the LogWorker class
|
88
|
+
SuckerPunch::Queue.new(:log_queue)
|
69
89
|
```
|
70
90
|
|
71
91
|
Synchronous:
|
@@ -80,9 +100,30 @@ Asynchronous:
|
|
80
100
|
SuckerPunch::Queue[:log_queue].async.perform("login") # => nil
|
81
101
|
```
|
82
102
|
|
103
|
+
## Stats
|
104
|
+
|
105
|
+
```Ruby
|
106
|
+
SuckerPunch::Queue[:log_queue].size # => 7
|
107
|
+
SuckerPunch::Queue[:log_queue].busy_size # => 4
|
108
|
+
SuckerPunch::Queue[:log_queue].idle_size # => 3
|
109
|
+
```
|
110
|
+
|
111
|
+
## Testing
|
112
|
+
|
113
|
+
`spec/spec_helper.rb`
|
114
|
+
```Ruby
|
115
|
+
require 'sucker_punch/inline'
|
116
|
+
```
|
117
|
+
|
118
|
+
Requiring this library causes your workers to run everything inline. So a call to the following will actually be SYNCHRONOUS.
|
119
|
+
|
120
|
+
```Ruby
|
121
|
+
SuckerPunch::Queue[:log_queue].async.perform("login")
|
122
|
+
```
|
123
|
+
|
83
124
|
## Gem Name
|
84
125
|
|
85
|
-
|
126
|
+
...is awesome. But I can't take credit for it. Thanks to [@jmazzi](https://twitter.com/jmazzi) for his superior naming skills. If you're looking for a name for something, he is the one to go to.
|
86
127
|
|
87
128
|
## Contributing
|
88
129
|
|
@@ -91,5 +132,3 @@ With all due respect, [@jmazzi](https://twitter.com/jmazzi) is completely respon
|
|
91
132
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
92
133
|
4. Push to the branch (`git push origin my-new-feature`)
|
93
134
|
5. Create new Pull Request
|
94
|
-
|
95
|
-
|
data/lib/sucker_punch/queue.rb
CHANGED
data/lib/sucker_punch/version.rb
CHANGED
data/spec/queue_spec.rb
CHANGED
@@ -3,9 +3,6 @@ require 'spec_helper'
|
|
3
3
|
class FakeWorker
|
4
4
|
include Celluloid
|
5
5
|
end
|
6
|
-
class FakeOtherWorker
|
7
|
-
include Celluloid
|
8
|
-
end
|
9
6
|
|
10
7
|
describe SuckerPunch::Queue do
|
11
8
|
describe ".[]" do
|
@@ -22,6 +19,7 @@ describe SuckerPunch::Queue do
|
|
22
19
|
end
|
23
20
|
|
24
21
|
it "turns the class into an actor" do
|
22
|
+
Celluloid::Actor[:crazy_queue].should be_a(Celluloid)
|
25
23
|
Celluloid::Actor[:crazy_queue].should be_a(FakeWorker)
|
26
24
|
Celluloid::Actor[:crazy_queue].methods.should include(:async)
|
27
25
|
end
|
@@ -30,4 +28,18 @@ describe SuckerPunch::Queue do
|
|
30
28
|
Celluloid::Actor[:crazy_queue].size.should == 7
|
31
29
|
end
|
32
30
|
end
|
31
|
+
|
32
|
+
describe "delegation" do
|
33
|
+
let(:queue) { SuckerPunch::Queue.new(:crazy_queue) }
|
34
|
+
|
35
|
+
before(:each) do
|
36
|
+
SuckerPunch::Queue.new(:crazy_queue).register(FakeWorker, 7)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "sends messages to Actor" do
|
40
|
+
queue.size.should == 7
|
41
|
+
queue.idle_size.should == 7
|
42
|
+
queue.busy_size.should == 0
|
43
|
+
end
|
44
|
+
end
|
33
45
|
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require_relative '../lib/sucker_punch/testing'
|
3
|
+
|
4
|
+
class PatchedWorker
|
5
|
+
include SuckerPunch::Worker
|
6
|
+
|
7
|
+
def perform
|
8
|
+
"do stuff"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
SuckerPunch::Queue.new(:patched_queue).register(PatchedWorker, 2)
|
12
|
+
|
13
|
+
describe "Testing" do
|
14
|
+
let(:queue) { SuckerPunch::Queue.new(:patched_queue) }
|
15
|
+
|
16
|
+
it "processes jobs inline" do
|
17
|
+
job = queue.async.perform
|
18
|
+
expect(job).to eq "do stuff"
|
19
|
+
end
|
20
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sucker_punch
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: '0.
|
4
|
+
version: '0.3'
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-03-19 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec
|
@@ -67,6 +67,8 @@ extensions: []
|
|
67
67
|
extra_rdoc_files: []
|
68
68
|
files:
|
69
69
|
- .gitignore
|
70
|
+
- .travis.yml
|
71
|
+
- CHANGES.md
|
70
72
|
- Gemfile
|
71
73
|
- LICENSE.txt
|
72
74
|
- README.md
|
@@ -74,11 +76,13 @@ files:
|
|
74
76
|
- lib/sucker_punch.rb
|
75
77
|
- lib/sucker_punch/exceptions.rb
|
76
78
|
- lib/sucker_punch/queue.rb
|
79
|
+
- lib/sucker_punch/testing.rb
|
77
80
|
- lib/sucker_punch/version.rb
|
78
81
|
- lib/sucker_punch/worker.rb
|
79
82
|
- spec/queue_spec.rb
|
80
83
|
- spec/spec_helper.rb
|
81
84
|
- spec/sucker_punch_spec.rb
|
85
|
+
- spec/testing_spec.rb
|
82
86
|
- spec/worker_spec.rb
|
83
87
|
- sucker_punch.gemspec
|
84
88
|
homepage: https://github.com/brandonhilkert/sucker_punch
|
@@ -110,4 +114,5 @@ test_files:
|
|
110
114
|
- spec/queue_spec.rb
|
111
115
|
- spec/spec_helper.rb
|
112
116
|
- spec/sucker_punch_spec.rb
|
117
|
+
- spec/testing_spec.rb
|
113
118
|
- spec/worker_spec.rb
|