quber 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +8 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +25 -0
- data/LICENSE.txt +21 -0
- data/README.md +72 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/active_job/queue_adapters/quber_adapter.rb +26 -0
- data/lib/quber/client.rb +11 -0
- data/lib/quber/http.rb +66 -0
- data/lib/quber/rack.rb +29 -0
- data/lib/quber/version.rb +3 -0
- data/lib/quber.rb +4 -0
- data/quber.gemspec +28 -0
- metadata +75 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: dad5bc8f43648c045e4b99949bd77bd3c3473f639de55aec60f9de1db86e2c94
|
4
|
+
data.tar.gz: c10ceb5f6d13e63654c598dcc0d147e46ec595a34acf04bfe03b979135ebcb6d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e4ad80705c6e76608ada0a9b423851a6a137c2e033e3413bf80d7e74797f17a09666f62fdee90735afff36d447ab13342e17d30e26cc534c4c78bf1c23b3c5ec
|
7
|
+
data.tar.gz: aeff1641ff99e59d2ebe03f569b790e3dbd6a565e22ee8df58a29be8fc624e4d039b38d33e86a7e0f890a484112a0150782e494cdebee61fbc23dfa97e143c45
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
quber (0.0.5)
|
5
|
+
net-http-persistent (~> 3.1.0)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
connection_pool (2.2.5)
|
11
|
+
minitest (5.14.4)
|
12
|
+
net-http-persistent (3.1.0)
|
13
|
+
connection_pool (~> 2.2)
|
14
|
+
rake (12.3.3)
|
15
|
+
|
16
|
+
PLATFORMS
|
17
|
+
ruby
|
18
|
+
|
19
|
+
DEPENDENCIES
|
20
|
+
minitest (~> 5.0)
|
21
|
+
quber!
|
22
|
+
rake (~> 12.0)
|
23
|
+
|
24
|
+
BUNDLED WITH
|
25
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 creadone
|
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,72 @@
|
|
1
|
+
# Quber
|
2
|
+
|
3
|
+
Rails adapter for the [Qube](https://github.com/tnt-qube/).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'quber'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
```sh
|
15
|
+
$ bundle install
|
16
|
+
```
|
17
|
+
|
18
|
+
Make available enviroment variables:
|
19
|
+
|
20
|
+
* `QUBER_API_URI` — Qube node URI (http://localhost:5672/api/v1/)
|
21
|
+
* `QUBER_API_TOKEN` — Qube node authentication token
|
22
|
+
|
23
|
+
Set in the Rails config Qube adapter:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
# config/application.rb
|
27
|
+
config.active_job.queue_adapter = :quber
|
28
|
+
```
|
29
|
+
|
30
|
+
Expose HTTP-endpoint to receive tasks back:
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
# config/routes.rb
|
34
|
+
# ...
|
35
|
+
mount Quber::Rack.new, at: '/_jobs'
|
36
|
+
# ...
|
37
|
+
```
|
38
|
+
|
39
|
+
## Usage
|
40
|
+
|
41
|
+
Use as usual job:
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
$ bin/rails generate job welcome_message
|
45
|
+
```
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class WelcomeMessageJob < ApplicationJob
|
49
|
+
queue_as :default
|
50
|
+
|
51
|
+
def perform(user)
|
52
|
+
# Do something later
|
53
|
+
end
|
54
|
+
end
|
55
|
+
```
|
56
|
+
|
57
|
+
Note: when you set `queue_as` as `:some_name` the Qube will create tube with `:some_name` and put the task into this tube.
|
58
|
+
|
59
|
+
## Development
|
60
|
+
|
61
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
62
|
+
|
63
|
+
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).
|
64
|
+
|
65
|
+
## Contributing
|
66
|
+
|
67
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/quber.
|
68
|
+
|
69
|
+
|
70
|
+
## License
|
71
|
+
|
72
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "quber"
|
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
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
module ActiveJob
|
2
|
+
module QueueAdapters
|
3
|
+
class QuberAdapter
|
4
|
+
|
5
|
+
def initialize options = {}
|
6
|
+
@client ||= Quber::Client.new options
|
7
|
+
end
|
8
|
+
|
9
|
+
def enqueue(job, attributes = {})
|
10
|
+
task = build_task(job, attributes)
|
11
|
+
@client.put task
|
12
|
+
end
|
13
|
+
|
14
|
+
def enqueue_at(job, timestamp)
|
15
|
+
enqueue job, scheduled_at: timestamp
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def build_task(job, attributes)
|
21
|
+
{ data: job.serialize, **attributes }
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
data/lib/quber/client.rb
ADDED
data/lib/quber/http.rb
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'uri'
|
2
|
+
require 'json'
|
3
|
+
require 'net/http'
|
4
|
+
require 'net/http/persistent'
|
5
|
+
|
6
|
+
module Quber
|
7
|
+
class HTTP
|
8
|
+
Response = Struct.new(:code, :body)
|
9
|
+
|
10
|
+
VERBS = {
|
11
|
+
get: Net::HTTP::Get,
|
12
|
+
post: Net::HTTP::Post,
|
13
|
+
put: Net::HTTP::Put,
|
14
|
+
delete: Net::HTTP::Delete
|
15
|
+
}
|
16
|
+
|
17
|
+
def initialize options = {}
|
18
|
+
@defaults = {
|
19
|
+
connection: Net::HTTP::Persistent.new,
|
20
|
+
api_uri: ENV['QUBER_API_URI'] || 'http://localhost:5672/api/v1/',
|
21
|
+
api_token: ENV['QUBER_API_TOKEN'] || '1234567890'
|
22
|
+
}.merge!(options)
|
23
|
+
end
|
24
|
+
|
25
|
+
def base_headers
|
26
|
+
{
|
27
|
+
'X-Auth-Token' => @defaults[:api_token],
|
28
|
+
'User-Agent' => "Quber/#{Quber::VERSION}",
|
29
|
+
'Content-Type' => 'application/json'
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
def get(path, options = {})
|
34
|
+
execute(path, :get, options)
|
35
|
+
end
|
36
|
+
|
37
|
+
def post(path, options = {})
|
38
|
+
execute(path, :post, options)
|
39
|
+
end
|
40
|
+
|
41
|
+
def put(path, options = {})
|
42
|
+
execute(path, :put, options)
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete(path, options = {})
|
46
|
+
execute(path, :delete, options)
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
def execute(path, method, options = {})
|
51
|
+
uri = URI.join(@defaults[:api_uri], path)
|
52
|
+
req = VERBS[method].new(uri.request_uri)
|
53
|
+
|
54
|
+
# Build headers and body
|
55
|
+
options.transform_keys!(&:to_s) unless options.empty?
|
56
|
+
headers = base_headers.merge(options.dig('headers') || options)
|
57
|
+
headers.each{ |k,v| req[k] = v }
|
58
|
+
req.body = (options.dig('body') || options || {}).to_json
|
59
|
+
|
60
|
+
# Send request and process response
|
61
|
+
resp = @defaults[:connection].request(uri.to_s, req)
|
62
|
+
body = resp.body.empty? ? {} : JSON.parse(resp.body)
|
63
|
+
Response.new(resp.code.to_i, body)
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/lib/quber/rack.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'json'
|
2
|
+
require 'rack'
|
3
|
+
|
4
|
+
module Quber
|
5
|
+
class Rack
|
6
|
+
class PayloadError < StandardError; end
|
7
|
+
|
8
|
+
def call(env)
|
9
|
+
request = ::Rack::Request.new(env)
|
10
|
+
payload = extract_payload(request)
|
11
|
+
|
12
|
+
ActiveJob::Base.execute payload
|
13
|
+
|
14
|
+
[200, {}, ['OK']]
|
15
|
+
rescue PayloadError => e
|
16
|
+
[400, {}, [e.cause.message]]
|
17
|
+
rescue => e
|
18
|
+
[500, {}, [e.message]]
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def extract_payload(request)
|
24
|
+
JSON.parse(request.body.read).fetch('data')
|
25
|
+
rescue JSON::ParserError, KeyError
|
26
|
+
raise PayloadError
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
data/lib/quber.rb
ADDED
data/quber.gemspec
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
require_relative 'lib/quber/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'quber'
|
5
|
+
spec.version = Quber::VERSION
|
6
|
+
spec.authors = ["creadone"]
|
7
|
+
spec.email = ["creadone@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = %q{Rails adapter for Qube}
|
10
|
+
spec.description = %q{Rails adapter for Qube}
|
11
|
+
spec.homepage = "https://github.com/roleus/quber"
|
12
|
+
spec.license = 'MIT'
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.7")
|
14
|
+
|
15
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
16
|
+
spec.metadata["source_code_uri"] = "https://github.com/tnt-qube/quber"
|
17
|
+
|
18
|
+
spec.add_runtime_dependency 'net-http-persistent', '~> 3.1.0'
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = "exe"
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ["lib"]
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,75 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: quber
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.5
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- creadone
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-08-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: net-http-persistent
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.1.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.1.0
|
27
|
+
description: Rails adapter for Qube
|
28
|
+
email:
|
29
|
+
- creadone@gmail.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".travis.yml"
|
36
|
+
- Gemfile
|
37
|
+
- Gemfile.lock
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- bin/console
|
42
|
+
- bin/setup
|
43
|
+
- lib/active_job/queue_adapters/quber_adapter.rb
|
44
|
+
- lib/quber.rb
|
45
|
+
- lib/quber/client.rb
|
46
|
+
- lib/quber/http.rb
|
47
|
+
- lib/quber/rack.rb
|
48
|
+
- lib/quber/version.rb
|
49
|
+
- quber.gemspec
|
50
|
+
homepage: https://github.com/roleus/quber
|
51
|
+
licenses:
|
52
|
+
- MIT
|
53
|
+
metadata:
|
54
|
+
homepage_uri: https://github.com/roleus/quber
|
55
|
+
source_code_uri: https://github.com/tnt-qube/quber
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options: []
|
58
|
+
require_paths:
|
59
|
+
- lib
|
60
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
61
|
+
requirements:
|
62
|
+
- - ">="
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '2.7'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '0'
|
70
|
+
requirements: []
|
71
|
+
rubygems_version: 3.1.4
|
72
|
+
signing_key:
|
73
|
+
specification_version: 4
|
74
|
+
summary: Rails adapter for Qube
|
75
|
+
test_files: []
|