sidekiq-thrift_arguments 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/Makefile +13 -0
- data/README.md +76 -0
- data/Rakefile +8 -0
- data/lib/sidekiq-thrift_arguments.rb +1 -0
- data/lib/sidekiq/thrift_arguments.rb +48 -0
- data/sidekiq-thrift_arguments.gemspec +22 -0
- data/test.thrift +3 -0
- data/test/acceptance_test.rb +42 -0
- data/test/test_helper.rb +14 -0
- data/vendor/gen-rb/test_constants.rb +9 -0
- data/vendor/gen-rb/test_types.rb +25 -0
- metadata +116 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8bd6d2c719d60decd03a9eb65143dfff7e07169d
|
4
|
+
data.tar.gz: a2b4b0b813b5fc7c39424a1757d307a17f8f62c1
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: ee528c07110634f3c83c43638c6e2cbdcc98917f55dfe96610d8520605b3b2aa06b785d8a9ffa7b34286db89d0803e04c4e1589787f89b79c56deb15929b7dc1
|
7
|
+
data.tar.gz: ed12e8b18aa8de20f69acf0982717500001b603544ce366e1c601c4131211af7cf626b6a904ecbde67633629bbac3f890ba41ed9f89be74e2b8f593554cbd9c6
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 ahawkins
|
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/Makefile
ADDED
data/README.md
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
# Sidekiq::ThriftArguments
|
2
|
+
|
3
|
+
This library transparent handles and serializing & deserializing
|
4
|
+
`Thrift::Struct` instances used as job arguments. This is a tedious
|
5
|
+
process because `Thrift::Struct` instances **cannot** be used
|
6
|
+
transparently with `JSON.dump` and `JSON.load`. The internal JSON
|
7
|
+
serializers must be used. So in pratice there would be code like this:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
class SomeWorker
|
11
|
+
include Sidekiq::Worker
|
12
|
+
|
13
|
+
def perform(json_blob)
|
14
|
+
deserializer = Thrift::Deserializer.new Thrift::JsonProtocolFactory.new
|
15
|
+
struct = deserializer.deserialize(MyThriftStruct.new, json_blob)
|
16
|
+
|
17
|
+
# do the work ...
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
serializer = Thrift::Serializer.new Thrift::JsonProtocolFactory.new
|
22
|
+
SomeWorker.perform_async serializer.serialize(some_thrift_struct)
|
23
|
+
```
|
24
|
+
|
25
|
+
This code functions as you'd expect. First, serialize the thrift
|
26
|
+
struct to a JSON string. The worker class accepts a serialized thrift
|
27
|
+
blob it can deserialize into the required object. This is fine and
|
28
|
+
dandy when you have a few classes. It gets out of hand when you have
|
29
|
+
repeated the same thing in the same codebase and then across multiple
|
30
|
+
codebases. There is also a slight problem in that you must know the
|
31
|
+
class you want to deserialize into. This makes it hard to abstract the
|
32
|
+
code in some cases. This library solves this problem.
|
33
|
+
|
34
|
+
## Installation
|
35
|
+
|
36
|
+
Add this line to your application's Gemfile:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
gem 'sidekiq-thrift_arguments'
|
40
|
+
```
|
41
|
+
|
42
|
+
And then execute:
|
43
|
+
|
44
|
+
$ bundle
|
45
|
+
|
46
|
+
Or install it yourself as:
|
47
|
+
|
48
|
+
$ gem install sidekiq-thrift_arguments
|
49
|
+
|
50
|
+
## Usage
|
51
|
+
|
52
|
+
Usage is simple. `include Sidekiq::ThriftArguments` after including
|
53
|
+
`Sidekiq::Worker`. Here's an example:
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
class ThriftWorker
|
57
|
+
include Sidekiq::Worker
|
58
|
+
# Important! You need to include this after, not before.
|
59
|
+
include Sidekiq::ThriftArguments
|
60
|
+
end
|
61
|
+
```
|
62
|
+
|
63
|
+
Now you can call `perform_async` and `perform` with `Thrift::Struct`
|
64
|
+
instances as you would with any other method.
|
65
|
+
|
66
|
+
## Testing
|
67
|
+
|
68
|
+
$ make test
|
69
|
+
|
70
|
+
## Contributing
|
71
|
+
|
72
|
+
1. Fork it ( https://github.com/saltside/sidekiq-thrift_arguements/fork )
|
73
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
74
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
75
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
76
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'sidekiq/thrift_arguments'
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'thrift'
|
2
|
+
require 'thrift/utf8_json'
|
3
|
+
require 'sidekiq'
|
4
|
+
|
5
|
+
module Sidekiq
|
6
|
+
module ThriftArguments
|
7
|
+
class << self
|
8
|
+
def included(base)
|
9
|
+
base.extend ArgumentEncoder
|
10
|
+
base.prepend ArgumentDecoder
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
module ArgumentEncoder
|
15
|
+
def perform_async(*args)
|
16
|
+
thrift_encoded = args.map do |argument|
|
17
|
+
case argument
|
18
|
+
when Thrift::Struct
|
19
|
+
{
|
20
|
+
'__thrift__' => true,
|
21
|
+
'__thrift_class__' => argument.class.name,
|
22
|
+
'__thrift_blob__' => Thrift::JsonSerializer.new.serialize(argument)
|
23
|
+
}
|
24
|
+
else
|
25
|
+
argument
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
super(*thrift_encoded)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
module ArgumentDecoder
|
35
|
+
def perform(*args)
|
36
|
+
decoded = args.map do |argument|
|
37
|
+
if argument.is_a?(Hash) && argument.key?('__thrift__')
|
38
|
+
klass, blob = argument.fetch('__thrift_class__'), argument.fetch('__thrift_blob__')
|
39
|
+
Thrift::JsonDeserializer.new.deserialize(klass.constantize.new, blob)
|
40
|
+
else
|
41
|
+
argument
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
super(*decoded)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
Gem::Specification.new do |spec|
|
3
|
+
spec.name = "sidekiq-thrift_arguments"
|
4
|
+
spec.version = "0.1.0"
|
5
|
+
spec.authors = ["ahawkins"]
|
6
|
+
spec.email = ["adam@hawkins.io"]
|
7
|
+
spec.summary = %q{Transparently serialize/deserialize Thirft structs used a Sidekiq job arguments}
|
8
|
+
spec.description = %q{}
|
9
|
+
spec.homepage = "https://github.com/saltside/sidekiq-thrift_arguments"
|
10
|
+
spec.license = "MIT"
|
11
|
+
|
12
|
+
spec.files = `git ls-files -z`.split("\x0")
|
13
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
14
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
15
|
+
spec.require_paths = ["lib"]
|
16
|
+
|
17
|
+
spec.add_dependency "sidekiq"
|
18
|
+
spec.add_dependency "thrift-utf8_json"
|
19
|
+
|
20
|
+
spec.add_development_dependency "bundler", "~> 1.7"
|
21
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
22
|
+
end
|
data/test.thrift
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class AcceptanceTest < MiniTest::Unit::TestCase
|
4
|
+
class CountPeople
|
5
|
+
include Sidekiq::Worker
|
6
|
+
include Sidekiq::ThriftArguments
|
7
|
+
|
8
|
+
COUNTED = [ ]
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def counted
|
12
|
+
COUNTED
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
# This method uses Person instance and calls a method. It tests the
|
17
|
+
# job does received a deserialized Person instance. It would be nice
|
18
|
+
# to assert on the result of this method, but perform_async only returns
|
19
|
+
# the job id. So here the counted people are stored in a constant so
|
20
|
+
# its members can be referenced after a job is processed.
|
21
|
+
def perform(person, count = true)
|
22
|
+
return unless count
|
23
|
+
COUNTED << person.name
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def setup
|
28
|
+
CountPeople.counted.clear
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_roundtrips_thrift_classes
|
32
|
+
Sidekiq::Testing.inline! do
|
33
|
+
person = Person.new name: 'adam'
|
34
|
+
|
35
|
+
CountPeople.perform_async Person.new(name: 'adam'), true
|
36
|
+
CountPeople.perform_async Person.new(name: 'terje'), true
|
37
|
+
CountPeople.perform_async Person.new(name: 'foo'), false
|
38
|
+
|
39
|
+
assert_equal 2, CountPeople.counted.size
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
|
3
|
+
root = File.expand_path '../..', __FILE__
|
4
|
+
|
5
|
+
$LOAD_PATH << "#{root}/vendor/gen-rb"
|
6
|
+
|
7
|
+
require 'sidekiq-thrift_arguments'
|
8
|
+
|
9
|
+
Thrift.type_checking = true
|
10
|
+
|
11
|
+
require 'sidekiq/testing'
|
12
|
+
|
13
|
+
require 'test_types'
|
14
|
+
require 'minitest/autorun'
|
@@ -0,0 +1,25 @@
|
|
1
|
+
#
|
2
|
+
# Autogenerated by Thrift Compiler (0.9.2)
|
3
|
+
#
|
4
|
+
# DO NOT EDIT UNLESS YOU ARE SURE THAT YOU KNOW WHAT YOU ARE DOING
|
5
|
+
#
|
6
|
+
|
7
|
+
require 'thrift'
|
8
|
+
|
9
|
+
class Person
|
10
|
+
include ::Thrift::Struct, ::Thrift::Struct_Union
|
11
|
+
NAME = 1
|
12
|
+
|
13
|
+
FIELDS = {
|
14
|
+
NAME => {:type => ::Thrift::Types::STRING, :name => 'name'}
|
15
|
+
}
|
16
|
+
|
17
|
+
def struct_fields; FIELDS; end
|
18
|
+
|
19
|
+
def validate
|
20
|
+
raise ::Thrift::ProtocolException.new(::Thrift::ProtocolException::UNKNOWN, 'Required field name is unset!') unless @name
|
21
|
+
end
|
22
|
+
|
23
|
+
::Thrift::Struct.generate_accessors self
|
24
|
+
end
|
25
|
+
|
metadata
ADDED
@@ -0,0 +1,116 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sidekiq-thrift_arguments
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- ahawkins
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-01-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: sidekiq
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: thrift-utf8_json
|
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: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.7'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.7'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
description: ''
|
70
|
+
email:
|
71
|
+
- adam@hawkins.io
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- Makefile
|
80
|
+
- README.md
|
81
|
+
- Rakefile
|
82
|
+
- lib/sidekiq-thrift_arguments.rb
|
83
|
+
- lib/sidekiq/thrift_arguments.rb
|
84
|
+
- sidekiq-thrift_arguments.gemspec
|
85
|
+
- test.thrift
|
86
|
+
- test/acceptance_test.rb
|
87
|
+
- test/test_helper.rb
|
88
|
+
- vendor/gen-rb/test_constants.rb
|
89
|
+
- vendor/gen-rb/test_types.rb
|
90
|
+
homepage: https://github.com/saltside/sidekiq-thrift_arguments
|
91
|
+
licenses:
|
92
|
+
- MIT
|
93
|
+
metadata: {}
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.2.2
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Transparently serialize/deserialize Thirft structs used a Sidekiq job arguments
|
114
|
+
test_files:
|
115
|
+
- test/acceptance_test.rb
|
116
|
+
- test/test_helper.rb
|