emittance-resque 0.1.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.
- checksums.yaml +7 -0
- data/.gitignore +20 -0
- data/.rspec +3 -0
- data/.rubocop.yml +22 -0
- data/.ruby-version +1 -0
- data/.travis.yml +10 -0
- data/.yardopts +4 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +8 -0
- data/Gemfile.lock +97 -0
- data/LICENSE.txt +21 -0
- data/README.md +103 -0
- data/Rakefile +8 -0
- data/bin/console +11 -0
- data/bin/setup +8 -0
- data/emittance-resque.gemspec +35 -0
- data/lib/emittance/resque.rb +34 -0
- data/lib/emittance/resque/broker.rb +20 -0
- data/lib/emittance/resque/dispatcher.rb +88 -0
- data/lib/emittance/resque/dispatcher/job_klass.rb +32 -0
- data/lib/emittance/resque/dispatcher/job_klass_name.rb +46 -0
- data/lib/emittance/resque/errors.rb +8 -0
- data/lib/emittance/resque/event_serializer.rb +28 -0
- data/lib/emittance/resque/event_serializer/active_record.rb +105 -0
- data/lib/emittance/resque/event_serializer/default.rb +31 -0
- data/lib/emittance/resque/job.rb +20 -0
- data/lib/emittance/resque/version.rb +7 -0
- metadata +196 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 3cfabe2cc7c6a9edbf7f9f3485ef79ba3bad5dab
|
4
|
+
data.tar.gz: 77f7164ffa970661f0c835a5e2599d27e88d543b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9a1f452dbb7d6d24960fe20b6a13b4269f29690b50f44c378e66088bfcdeea20ef56806c63845122f11abc20368ca0504b7c2031c48872381d7fbbca680518c0
|
7
|
+
data.tar.gz: 39de826abaa91641a3f910750530f0176bfe327d8c62dbdad21b2f25ba0710558ad7e9daef827527c289f80434e6250523c3f79ef4d6145fd9889d850d52f289
|
data/.gitignore
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/_yardoc/
|
4
|
+
/coverage/
|
5
|
+
/doc/
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/tmp/
|
9
|
+
|
10
|
+
# rspec failure tracking
|
11
|
+
.rspec_status
|
12
|
+
|
13
|
+
.idea
|
14
|
+
.idea/dataSources.xml
|
15
|
+
tags
|
16
|
+
*.sublime-workspace
|
17
|
+
*.sublime-project
|
18
|
+
.project
|
19
|
+
|
20
|
+
.DS_Store
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
AllCops:
|
2
|
+
Exclude:
|
3
|
+
- 'spec/**/*'
|
4
|
+
- 'bin/**/*'
|
5
|
+
|
6
|
+
# Metrics
|
7
|
+
|
8
|
+
Metrics/LineLength:
|
9
|
+
Max: 120
|
10
|
+
|
11
|
+
# Layout
|
12
|
+
|
13
|
+
Layout/MultilineMethodCallIndentation:
|
14
|
+
EnforcedStyle: indented
|
15
|
+
|
16
|
+
Layout/MultilineOperationIndentation:
|
17
|
+
EnforcedStyle: indented
|
18
|
+
|
19
|
+
# Style
|
20
|
+
|
21
|
+
Style/DoubleNegation:
|
22
|
+
Enabled: false
|
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.2
|
data/.travis.yml
ADDED
@@ -0,0 +1,10 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.4.2
|
4
|
+
before_install: gem install bundler -v 1.16.0
|
5
|
+
before_script:
|
6
|
+
- curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./cc-test-reporter
|
7
|
+
- chmod +x ./cc-test-reporter
|
8
|
+
- ./cc-test-reporter before-build
|
9
|
+
after_script:
|
10
|
+
- ./cc-test-reporter after-build --exit-code $TRAVIS_TEST_RESULT
|
data/.yardopts
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at tyguillen@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
emittance-resque (0.1.0)
|
5
|
+
emittance (>= 0.1.1)
|
6
|
+
resque
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
activemodel (5.1.4)
|
12
|
+
activesupport (= 5.1.4)
|
13
|
+
activerecord (5.1.4)
|
14
|
+
activemodel (= 5.1.4)
|
15
|
+
activesupport (= 5.1.4)
|
16
|
+
arel (~> 8.0)
|
17
|
+
activesupport (5.1.4)
|
18
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
19
|
+
i18n (~> 0.7)
|
20
|
+
minitest (~> 5.1)
|
21
|
+
tzinfo (~> 1.1)
|
22
|
+
arel (8.0.0)
|
23
|
+
coderay (1.1.2)
|
24
|
+
concurrent-ruby (1.0.5)
|
25
|
+
diff-lcs (1.3)
|
26
|
+
docile (1.1.5)
|
27
|
+
emittance (0.1.1)
|
28
|
+
i18n (0.9.1)
|
29
|
+
concurrent-ruby (~> 1.0)
|
30
|
+
json (2.1.0)
|
31
|
+
method_source (0.9.0)
|
32
|
+
minitest (5.10.3)
|
33
|
+
mono_logger (1.1.0)
|
34
|
+
multi_json (1.12.2)
|
35
|
+
mustermann (1.0.1)
|
36
|
+
pry (0.11.3)
|
37
|
+
coderay (~> 1.1.0)
|
38
|
+
method_source (~> 0.9.0)
|
39
|
+
rack (2.0.3)
|
40
|
+
rack-protection (2.0.0)
|
41
|
+
rack
|
42
|
+
rake (10.5.0)
|
43
|
+
redis (4.0.1)
|
44
|
+
redis-namespace (1.6.0)
|
45
|
+
redis (>= 3.0.4)
|
46
|
+
resque (1.27.4)
|
47
|
+
mono_logger (~> 1.0)
|
48
|
+
multi_json (~> 1.0)
|
49
|
+
redis-namespace (~> 1.3)
|
50
|
+
sinatra (>= 0.9.2)
|
51
|
+
vegas (~> 0.1.2)
|
52
|
+
rspec (3.7.0)
|
53
|
+
rspec-core (~> 3.7.0)
|
54
|
+
rspec-expectations (~> 3.7.0)
|
55
|
+
rspec-mocks (~> 3.7.0)
|
56
|
+
rspec-core (3.7.0)
|
57
|
+
rspec-support (~> 3.7.0)
|
58
|
+
rspec-expectations (3.7.0)
|
59
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
60
|
+
rspec-support (~> 3.7.0)
|
61
|
+
rspec-mocks (3.7.0)
|
62
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
63
|
+
rspec-support (~> 3.7.0)
|
64
|
+
rspec-support (3.7.0)
|
65
|
+
simplecov (0.15.1)
|
66
|
+
docile (~> 1.1.0)
|
67
|
+
json (>= 1.8, < 3)
|
68
|
+
simplecov-html (~> 0.10.0)
|
69
|
+
simplecov-html (0.10.2)
|
70
|
+
sinatra (2.0.0)
|
71
|
+
mustermann (~> 1.0)
|
72
|
+
rack (~> 2.0)
|
73
|
+
rack-protection (= 2.0.0)
|
74
|
+
tilt (~> 2.0)
|
75
|
+
thread_safe (0.3.6)
|
76
|
+
tilt (2.0.8)
|
77
|
+
tzinfo (1.2.4)
|
78
|
+
thread_safe (~> 0.1)
|
79
|
+
vegas (0.1.11)
|
80
|
+
rack (>= 1.0.0)
|
81
|
+
yard (0.9.12)
|
82
|
+
|
83
|
+
PLATFORMS
|
84
|
+
ruby
|
85
|
+
|
86
|
+
DEPENDENCIES
|
87
|
+
activerecord
|
88
|
+
bundler (~> 1.16)
|
89
|
+
emittance-resque!
|
90
|
+
pry
|
91
|
+
rake (~> 10.0)
|
92
|
+
rspec (~> 3.0)
|
93
|
+
simplecov
|
94
|
+
yard
|
95
|
+
|
96
|
+
BUNDLED WITH
|
97
|
+
1.16.0
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2017 Tyler Guillen
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,103 @@
|
|
1
|
+
# Emittance::Resque
|
2
|
+
|
3
|
+
[](https://travis-ci.org/aastronautss/emittance-resque)
|
4
|
+
[](https://codeclimate.com/github/aastronautss/emittance-resque/maintainability)
|
5
|
+
[](http://inch-ci.org/github/aastronautss/emittance-resque)
|
6
|
+
|
7
|
+
This is the Resque broker for the Emittance gem.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'emittance', '=> 0.0.6'
|
15
|
+
gem 'emittance-resque'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Emittance::Resque requires any version of Emittance from 0.0.6 onward.
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install emittance
|
27
|
+
$ gem install emittance-resque
|
28
|
+
|
29
|
+
## Usage
|
30
|
+
|
31
|
+
Use this as you would the standard Emittance gem. There are some limitations, which we'll get to in the next section.
|
32
|
+
|
33
|
+
First, we need to set the backend:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
Emittance.use_broker :resque
|
37
|
+
```
|
38
|
+
|
39
|
+
Then, we can watch for events:
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
class MyKlass
|
43
|
+
extend Emittance::Watcher
|
44
|
+
|
45
|
+
def self.something_happened(event)
|
46
|
+
puts 'something happened!'
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
MyKlass.watch :happening, :something_happened
|
51
|
+
```
|
52
|
+
|
53
|
+
Per the Emittance core library, this sets `MyKlass` up to call `.something_happened` whenever a `:happening` event (wrapped in an instance of `HappeningEvent`) gets emitted. Since we're using the Resque broker, this will occur in a Resque job.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class MyEmitter
|
57
|
+
extend Emittance::Emitter
|
58
|
+
|
59
|
+
def self.make_something_happen
|
60
|
+
puts 'something will happen'
|
61
|
+
emit :happening
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
MyEmitter.make_something_happen
|
66
|
+
# In the parent process log:
|
67
|
+
# something will happen!
|
68
|
+
#
|
69
|
+
# In the Resque process log:
|
70
|
+
# something happened!
|
71
|
+
```
|
72
|
+
|
73
|
+
## Serialization
|
74
|
+
|
75
|
+
By default, this library will piggyback on Resque's default serialization scheme. When used in a Rails app, it will detect persisted (i.e. `ActiveRecord::Base`) objects (even inside of a hash or enumerable object) and pass on the type and ID, deserializing those values back into the correct objects. The callback method should expect a single argument in the form of the correct `Emittance::Event` object.
|
76
|
+
|
77
|
+
Serialization and deserialization are accomplished with an object (typically a singleton class) that respond to `serialize` and `deserialize` methods, which take an `Emittance::Event` object and the serialized `Emittance::Event` object, respectively. You can write your own serializer if your needs are not met with the built-in schemes--just create a class or an object that meets the criteria discussed earlier in this paragraph. Let Emittance know which serializer to use like so:
|
78
|
+
|
79
|
+
```ruby
|
80
|
+
Emittance::Resque.use_serializer(MyCoolSerializer)
|
81
|
+
```
|
82
|
+
|
83
|
+
## Limitations
|
84
|
+
|
85
|
+
At this time, Emittance::Resque can only register listeners as
|
86
|
+
|
87
|
+
## Development
|
88
|
+
|
89
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
90
|
+
|
91
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
92
|
+
|
93
|
+
## Contributing
|
94
|
+
|
95
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/emittance-resque. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
96
|
+
|
97
|
+
## License
|
98
|
+
|
99
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
100
|
+
|
101
|
+
## Code of Conduct
|
102
|
+
|
103
|
+
Everyone interacting in the Emittance::Resque project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/emittance-resque/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'emittance/resque'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
require 'pry'
|
11
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'emittance/resque/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'emittance-resque'
|
9
|
+
spec.version = Emittance::Resque::VERSION
|
10
|
+
spec.authors = ['Tyler Guillen']
|
11
|
+
spec.email = ['tyguillen@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'The "resque" dispatch strategy for the emittance gem.'
|
14
|
+
spec.description = 'The "resque" dispatch strategy for the emittance gem.'
|
15
|
+
spec.homepage = 'https://github.com/aastronautss/emittance-resque'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_dependency 'emittance', '>= 0.1.1'
|
26
|
+
spec.add_dependency 'resque'
|
27
|
+
|
28
|
+
spec.add_development_dependency 'activerecord'
|
29
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
30
|
+
spec.add_development_dependency 'pry'
|
31
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
32
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
33
|
+
spec.add_development_dependency 'simplecov'
|
34
|
+
spec.add_development_dependency 'yard'
|
35
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'resque'
|
4
|
+
require 'emittance'
|
5
|
+
|
6
|
+
require 'emittance/resque/version'
|
7
|
+
require 'emittance/resque/errors'
|
8
|
+
|
9
|
+
require 'emittance/resque/broker'
|
10
|
+
require 'emittance/resque/dispatcher'
|
11
|
+
|
12
|
+
module Emittance
|
13
|
+
##
|
14
|
+
# Top-level namespace for the Resque emittance broker.
|
15
|
+
#
|
16
|
+
module Resque
|
17
|
+
class << self
|
18
|
+
def use_serializer(serializer)
|
19
|
+
Emittance::Resque::EventSerializer.use_serializer serializer
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
# :nocov:
|
26
|
+
Emittance::Brokerage.register_broker Emittance::Resque::Broker, :resque
|
27
|
+
|
28
|
+
if defined?(ActiveRecord)
|
29
|
+
require 'emittance/resque/event_serializer/active_record'
|
30
|
+
Emittance::Resque.use_serializer Emittance::Resque::EventSerializer::ActiveRecord
|
31
|
+
else
|
32
|
+
Emittance::Resque.use_serializer Emittance::Resque::EventSerializer::Default
|
33
|
+
end
|
34
|
+
# :nocov:
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Emittance
|
4
|
+
module Resque
|
5
|
+
##
|
6
|
+
# The Resque broker for Emittance.
|
7
|
+
#
|
8
|
+
class Broker < Emittance::Broker
|
9
|
+
class << self
|
10
|
+
def process_event(event)
|
11
|
+
dispatcher.process_event event
|
12
|
+
end
|
13
|
+
|
14
|
+
def dispatcher
|
15
|
+
Emittance::Resque::Dispatcher
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module Emittance
|
6
|
+
module Resque
|
7
|
+
class Dispatcher < Emittance::Dispatcher; end
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
require 'emittance/resque/job'
|
12
|
+
require 'emittance/resque/dispatcher/job_klass_name'
|
13
|
+
require 'emittance/resque/dispatcher/job_klass'
|
14
|
+
require 'emittance/resque/event_serializer'
|
15
|
+
require 'emittance/resque/event_serializer/default'
|
16
|
+
|
17
|
+
module Emittance
|
18
|
+
module Resque
|
19
|
+
##
|
20
|
+
# The Resque dispatcher for Emittance.
|
21
|
+
#
|
22
|
+
class Dispatcher
|
23
|
+
Jobs = Module.new
|
24
|
+
|
25
|
+
class << self
|
26
|
+
include Emittance::Helpers::ConstantHelpers
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
def _process_event(event)
|
31
|
+
jobs = registrations_for(event.class)
|
32
|
+
serialized_event = serialize_event(event)
|
33
|
+
|
34
|
+
jobs.each { |job| enqueue_job job, serialized_event }
|
35
|
+
end
|
36
|
+
|
37
|
+
def _register(_identifier, &_callback)
|
38
|
+
raise InvalidCallbackError, 'Emittance::Resque cannot accept closures as callbacks at this time'
|
39
|
+
end
|
40
|
+
|
41
|
+
def _register_method_call(identifier, object, method_name)
|
42
|
+
validate_method_call object, method_name
|
43
|
+
|
44
|
+
event_klass = find_event_klass(identifier)
|
45
|
+
klass_name = method_call_job_klass_name(event_klass, object, method_name)
|
46
|
+
klass = method_call_job_klass(object, method_name)
|
47
|
+
|
48
|
+
set_namespaced_constant_by_name("#{Jobs.name}::#{klass_name}", klass) unless Jobs.const_defined?(klass_name)
|
49
|
+
registrations_for(event_klass) << klass
|
50
|
+
end
|
51
|
+
|
52
|
+
def enqueue_job(job, event)
|
53
|
+
::Resque.enqueue job, event
|
54
|
+
end
|
55
|
+
|
56
|
+
def find_event_klass(event)
|
57
|
+
Emittance::EventLookup.find_event_klass(event)
|
58
|
+
end
|
59
|
+
|
60
|
+
def serialize_event(event)
|
61
|
+
Emittance::Resque::EventSerializer.serialize(event)
|
62
|
+
end
|
63
|
+
|
64
|
+
def method_call_job_klass_name(event_klass, object, method_name)
|
65
|
+
JobKlassName.new(event_klass, object, method_name).generate
|
66
|
+
end
|
67
|
+
|
68
|
+
def job_klass(callback)
|
69
|
+
JobKlass.new(callback).generate
|
70
|
+
end
|
71
|
+
|
72
|
+
def method_call_job_klass(object, method_name)
|
73
|
+
callback = lambda_for_method_call(object, method_name)
|
74
|
+
job_klass callback
|
75
|
+
end
|
76
|
+
|
77
|
+
def validate_method_call(object, _method_name)
|
78
|
+
error_msg = 'Emittance::Resque can only call methods on classes and modules'
|
79
|
+
raise InvalidCallbackError, error_msg unless object.is_a?(Module)
|
80
|
+
end
|
81
|
+
|
82
|
+
def lambda_for_method_call(object, method_name)
|
83
|
+
->(event) { object.send method_name, event }
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Emittance
|
4
|
+
module Resque
|
5
|
+
class Dispatcher
|
6
|
+
##
|
7
|
+
# Use this to build a job class from a callback block/proc/lambda.
|
8
|
+
#
|
9
|
+
class JobKlass
|
10
|
+
# The name of the method used by the background job library to perform the job.
|
11
|
+
PERFORM_METHOD_NAME = :perform
|
12
|
+
|
13
|
+
def initialize(callback, queue: :default)
|
14
|
+
@callback = callback
|
15
|
+
@queue = queue
|
16
|
+
end
|
17
|
+
|
18
|
+
def generate
|
19
|
+
klass = Class.new(Emittance::Resque::Job)
|
20
|
+
klass.send(:define_method, PERFORM_METHOD_NAME, callback)
|
21
|
+
klass.instance_variable_set '@queue', queue
|
22
|
+
|
23
|
+
klass
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
attr_reader :callback, :queue
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Emittance
|
4
|
+
module Resque
|
5
|
+
class Dispatcher
|
6
|
+
##
|
7
|
+
# Use this to build job class names.
|
8
|
+
#
|
9
|
+
class JobKlassName
|
10
|
+
include Emittance::Helpers::StringHelpers
|
11
|
+
|
12
|
+
SUFFIX = 'Job'
|
13
|
+
|
14
|
+
def initialize(event_klass, object, method_name)
|
15
|
+
@event_klass = event_klass
|
16
|
+
@object = object
|
17
|
+
@method_name = method_name
|
18
|
+
end
|
19
|
+
|
20
|
+
def generate
|
21
|
+
"#{prefix}::#{base_name}#{suffix}"
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
attr_reader :event_klass, :object, :method_name
|
27
|
+
|
28
|
+
def prefix
|
29
|
+
event_klass.name
|
30
|
+
end
|
31
|
+
|
32
|
+
def base_name
|
33
|
+
"#{object}::#{formatted_method_name}"
|
34
|
+
end
|
35
|
+
|
36
|
+
def suffix
|
37
|
+
SUFFIX
|
38
|
+
end
|
39
|
+
|
40
|
+
def formatted_method_name
|
41
|
+
camel_case(method_name.to_s)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Emittance
|
4
|
+
module Resque
|
5
|
+
##
|
6
|
+
# Entry point for serialization.
|
7
|
+
#
|
8
|
+
module EventSerializer
|
9
|
+
class << self
|
10
|
+
def use_serializer(serializer)
|
11
|
+
self.serializer = serializer
|
12
|
+
end
|
13
|
+
|
14
|
+
def serialize(event)
|
15
|
+
serializer.serialize event
|
16
|
+
end
|
17
|
+
|
18
|
+
def deserialize(event_hash)
|
19
|
+
serializer.deserialize event_hash
|
20
|
+
end
|
21
|
+
|
22
|
+
private
|
23
|
+
|
24
|
+
attr_accessor :serializer
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,105 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Emittance
|
4
|
+
module Resque
|
5
|
+
module EventSerializer
|
6
|
+
##
|
7
|
+
# Essentially the same as the default serializer, with the added ability to store the metadata for +ActiveRecord+
|
8
|
+
# objects, so that it will re-fetch the record when the job is dequeued. Will serialize +ActiveRecord+ objects
|
9
|
+
# even if inside of an array (or nested array), or if added as a value in a hash.
|
10
|
+
#
|
11
|
+
module ActiveRecord
|
12
|
+
class << self
|
13
|
+
def serialize(event)
|
14
|
+
{
|
15
|
+
identifier: event.identifiers.first,
|
16
|
+
emitter: serialize_object(event.emitter),
|
17
|
+
timestamp: serialize_timestamp(event.timestamp),
|
18
|
+
payload: serialize_object(event.payload)
|
19
|
+
}
|
20
|
+
end
|
21
|
+
|
22
|
+
def deserialize(event_hash)
|
23
|
+
identifier = event_hash['identifier']
|
24
|
+
event_klass = Emittance::EventLookup.find_event_klass(identifier)
|
25
|
+
|
26
|
+
emitter = deserialize_object(event_hash['emitter'])
|
27
|
+
timestamp = deserialize_timestamp(event_hash['timestamp'])
|
28
|
+
payload = deserialize_object(event_hash['payload'])
|
29
|
+
|
30
|
+
event_klass.new(emitter, timestamp, payload)
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
|
35
|
+
def serialize_object(obj)
|
36
|
+
if obj.is_a? Hash
|
37
|
+
serialize_hash(obj)
|
38
|
+
elsif obj.is_a? Enumerable
|
39
|
+
serialize_enum(obj)
|
40
|
+
elsif obj.is_a? ::ActiveRecord::Base
|
41
|
+
serialize_persisted(obj)
|
42
|
+
else
|
43
|
+
obj
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
def serialize_timestamp(time)
|
48
|
+
time.to_i
|
49
|
+
end
|
50
|
+
|
51
|
+
def serialize_hash(obj)
|
52
|
+
Hash[
|
53
|
+
obj.map { |k, v| [k, serialize_object(v)] }
|
54
|
+
]
|
55
|
+
end
|
56
|
+
|
57
|
+
def serialize_enum(obj)
|
58
|
+
obj.map { |ele| serialize_object(ele) }
|
59
|
+
end
|
60
|
+
|
61
|
+
def serialize_persisted(obj)
|
62
|
+
{
|
63
|
+
_persisted: true,
|
64
|
+
persisted_type: obj.class.name,
|
65
|
+
persisted_id: obj.id
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
def deserialize_object(obj)
|
70
|
+
if obj.is_a? Hash
|
71
|
+
deserialize_hash(obj)
|
72
|
+
elsif obj.is_a? Enumerable
|
73
|
+
deserialize_enum(obj)
|
74
|
+
else
|
75
|
+
obj
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def deserialize_timestamp(time_i)
|
80
|
+
Time.at(time_i)
|
81
|
+
end
|
82
|
+
|
83
|
+
def deserialize_hash(obj)
|
84
|
+
if obj['_persisted']
|
85
|
+
deserialize_persisted(obj)
|
86
|
+
else
|
87
|
+
Hash[
|
88
|
+
obj.map { |k, v| [k, deserialize_object(v)] }
|
89
|
+
]
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def deserialize_enum(obj)
|
94
|
+
obj.map { |ele| deserialize_object(ele) }
|
95
|
+
end
|
96
|
+
|
97
|
+
def deserialize_persisted(obj)
|
98
|
+
klass = Object.const_get obj['persisted_type']
|
99
|
+
klass.find obj['persisted_id']
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Emittance
|
4
|
+
module Resque
|
5
|
+
module EventSerializer
|
6
|
+
##
|
7
|
+
# The default serializer for Emittance::Resque. Converts the event with its properties into a hash, and
|
8
|
+
# deserializes that hash by initializing a new event with those properties.
|
9
|
+
#
|
10
|
+
module Default
|
11
|
+
class << self
|
12
|
+
def serialize(event)
|
13
|
+
{
|
14
|
+
identifier: event.identifiers.first,
|
15
|
+
emitter: event.emitter,
|
16
|
+
timestamp: event.timestamp,
|
17
|
+
payload: event.payload
|
18
|
+
}
|
19
|
+
end
|
20
|
+
|
21
|
+
def deserialize(event_hash)
|
22
|
+
identifier = event_hash[:identifier]
|
23
|
+
event_klass = Emittance::EventLookup.find_event_klass(identifier)
|
24
|
+
|
25
|
+
event_klass.new(event_hash[:emitter], event_hash[:timestamp], event_hash[:payload])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'emittance/resque/event_serializer'
|
4
|
+
|
5
|
+
module Emittance
|
6
|
+
module Resque
|
7
|
+
##
|
8
|
+
# Wrapper calls for resque jobs to dispatch.
|
9
|
+
#
|
10
|
+
class Job
|
11
|
+
class << self
|
12
|
+
def perform(event)
|
13
|
+
deserialized_event = Emittance::Resque::EventSerializer.deserialize(event)
|
14
|
+
|
15
|
+
new.perform(deserialized_event)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
metadata
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: emittance-resque
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tyler Guillen
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-12-29 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: emittance
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 0.1.1
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 0.1.1
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: resque
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: activerecord
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.16'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.16'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: pry
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '3.0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '3.0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: simplecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: yard
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: The "resque" dispatch strategy for the emittance gem.
|
140
|
+
email:
|
141
|
+
- tyguillen@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".rubocop.yml"
|
149
|
+
- ".ruby-version"
|
150
|
+
- ".travis.yml"
|
151
|
+
- ".yardopts"
|
152
|
+
- CODE_OF_CONDUCT.md
|
153
|
+
- Gemfile
|
154
|
+
- Gemfile.lock
|
155
|
+
- LICENSE.txt
|
156
|
+
- README.md
|
157
|
+
- Rakefile
|
158
|
+
- bin/console
|
159
|
+
- bin/setup
|
160
|
+
- emittance-resque.gemspec
|
161
|
+
- lib/emittance/resque.rb
|
162
|
+
- lib/emittance/resque/broker.rb
|
163
|
+
- lib/emittance/resque/dispatcher.rb
|
164
|
+
- lib/emittance/resque/dispatcher/job_klass.rb
|
165
|
+
- lib/emittance/resque/dispatcher/job_klass_name.rb
|
166
|
+
- lib/emittance/resque/errors.rb
|
167
|
+
- lib/emittance/resque/event_serializer.rb
|
168
|
+
- lib/emittance/resque/event_serializer/active_record.rb
|
169
|
+
- lib/emittance/resque/event_serializer/default.rb
|
170
|
+
- lib/emittance/resque/job.rb
|
171
|
+
- lib/emittance/resque/version.rb
|
172
|
+
homepage: https://github.com/aastronautss/emittance-resque
|
173
|
+
licenses:
|
174
|
+
- MIT
|
175
|
+
metadata: {}
|
176
|
+
post_install_message:
|
177
|
+
rdoc_options: []
|
178
|
+
require_paths:
|
179
|
+
- lib
|
180
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
181
|
+
requirements:
|
182
|
+
- - ">="
|
183
|
+
- !ruby/object:Gem::Version
|
184
|
+
version: '0'
|
185
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
186
|
+
requirements:
|
187
|
+
- - ">="
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: '0'
|
190
|
+
requirements: []
|
191
|
+
rubyforge_project:
|
192
|
+
rubygems_version: 2.6.13
|
193
|
+
signing_key:
|
194
|
+
specification_version: 4
|
195
|
+
summary: The "resque" dispatch strategy for the emittance gem.
|
196
|
+
test_files: []
|