allq 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 +11 -0
- data/.rspec +3 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/allq.gemspec +27 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/allq.rb +13 -0
- data/lib/allq/actions/base.rb +59 -0
- data/lib/allq/actions/delete.rb +24 -0
- data/lib/allq/actions/done.rb +23 -0
- data/lib/allq/actions/get.rb +29 -0
- data/lib/allq/actions/put.rb +35 -0
- data/lib/allq/actions/release.rb +29 -0
- data/lib/allq/actions/stats.rb +43 -0
- data/lib/allq/actions/touch.rb +30 -0
- data/lib/allq/client.rb +59 -0
- data/lib/allq/connection.rb +153 -0
- data/lib/allq/job.rb +74 -0
- data/lib/allq/version.rb +3 -0
- metadata +110 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 68fe0f74295aa4a5d7a2fe721ff9b4262c856e5c
|
4
|
+
data.tar.gz: bbb1a2cf6465a6b6ebe984a53d4ae059342b9bce
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 139b588f1544ef694fe935c62701df0eef8e2777735f133f2d0e5897ee305580b4e2e92b6a516e3837aa36d57f63f9d2a5474b960c78537c7397e46d35b21764
|
7
|
+
data.tar.gz: 71686b894c31072183e48fb091a967d3a11a595c255b81cd448b4688af2709ae7d7c61cdcac5979e283d49d0505efcc16010918743a2032ba7fa58378489624e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
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 jaciones@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/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jason
|
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,43 @@
|
|
1
|
+
# Allq
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/allq`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'allq'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install allq
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/allq. 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.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the Allq project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/allq/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/allq.gemspec
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "allq/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "allq"
|
8
|
+
spec.version = Allq::VERSION
|
9
|
+
spec.authors = ["Jason"]
|
10
|
+
spec.email = ["jaciones@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby library for using AllQ}
|
13
|
+
spec.description = %q{Ruby gem for using AllQ}
|
14
|
+
spec.homepage = "https://github.com/blitline-dev/allq_gem"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
18
|
+
f.match(%r{^(test|spec|features)/})
|
19
|
+
end
|
20
|
+
spec.bindir = "exe"
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
25
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
26
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
27
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "allq"
|
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
data/lib/allq.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
|
2
|
+
%w(version client connection job version).each do |f|
|
3
|
+
require_relative "allq/#{f}"
|
4
|
+
end
|
5
|
+
|
6
|
+
%w(base get delete done put release stats touch).each do |f|
|
7
|
+
require_relative "allq/actions/#{f}"
|
8
|
+
end
|
9
|
+
|
10
|
+
module Allq
|
11
|
+
|
12
|
+
# Your code goes here...
|
13
|
+
end
|
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
class AllQ
|
4
|
+
# Base class for handling allq actions
|
5
|
+
class Base
|
6
|
+
@requires_q_server = true
|
7
|
+
|
8
|
+
def initialize(connection)
|
9
|
+
@connection = connection
|
10
|
+
setup
|
11
|
+
end
|
12
|
+
|
13
|
+
def snd(data)
|
14
|
+
send_data = base_send(data)
|
15
|
+
response = send_hash_as_json(send_data)
|
16
|
+
rcv(response)
|
17
|
+
end
|
18
|
+
|
19
|
+
def rcv(data)
|
20
|
+
return nil if data.to_s == '' || data.to_s.strip == '{}'
|
21
|
+
data
|
22
|
+
end
|
23
|
+
|
24
|
+
def send_hash_as_json(data_hash)
|
25
|
+
if @requires_q_server
|
26
|
+
params = data_hash["params"]
|
27
|
+
raise data_hash["action"] + " must have q_server set" unless data_hash["params"]["q_server"]
|
28
|
+
end
|
29
|
+
transmit_data = data_hash.to_json
|
30
|
+
result = nil
|
31
|
+
@connection.transmit(transmit_data) do |response|
|
32
|
+
result = response
|
33
|
+
end
|
34
|
+
result
|
35
|
+
end
|
36
|
+
|
37
|
+
def build_job(result)
|
38
|
+
result_hash = JSON.parse(result)
|
39
|
+
job_info = result_hash["job"]
|
40
|
+
job_id = job_info["job_id"]
|
41
|
+
q_server = job_info["q_server"]
|
42
|
+
|
43
|
+
job = Job.new(job_id, q_server)
|
44
|
+
# -- Optional fields
|
45
|
+
job.body = job_info["body"] if job_info["body"]
|
46
|
+
job.expired_count = job_info["expired_count"] if job_info["expired_count"]
|
47
|
+
return job
|
48
|
+
end
|
49
|
+
|
50
|
+
def setup
|
51
|
+
end
|
52
|
+
|
53
|
+
# -- Abstract
|
54
|
+
def base_send(_data)
|
55
|
+
raise NotImplementedError
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class AllQ
|
2
|
+
class Delete < AllQ::Base
|
3
|
+
|
4
|
+
def snd(data)
|
5
|
+
job_id = data[:job_id]
|
6
|
+
q_server = data[:q_server]
|
7
|
+
|
8
|
+
send_data = base_send(job_id, q_server)
|
9
|
+
response = send_hash_as_json(send_data)
|
10
|
+
result = rcv(response)
|
11
|
+
return JSON.parse(result)
|
12
|
+
end
|
13
|
+
|
14
|
+
def base_send(job_id, q_server)
|
15
|
+
{
|
16
|
+
'action' => 'delete',
|
17
|
+
'params' => {
|
18
|
+
'job_id' => job_id,
|
19
|
+
'q_server' => q_server
|
20
|
+
}
|
21
|
+
}
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class AllQ
|
2
|
+
class Done < AllQ::Base
|
3
|
+
|
4
|
+
def snd(data)
|
5
|
+
job_id = data[:job_id]
|
6
|
+
q_server = data[:q_server]
|
7
|
+
|
8
|
+
send_data = base_send(job_id, q_server)
|
9
|
+
response = send_hash_as_json(send_data)
|
10
|
+
rcv(response)
|
11
|
+
end
|
12
|
+
|
13
|
+
def base_send(job_id, q_server)
|
14
|
+
{
|
15
|
+
'action' => 'done',
|
16
|
+
'params' => {
|
17
|
+
'job_id' => job_id,
|
18
|
+
'q_server' => q_server
|
19
|
+
}
|
20
|
+
}
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class AllQ
|
2
|
+
class Get < AllQ::Base
|
3
|
+
|
4
|
+
def setup
|
5
|
+
@requires_q_server = false
|
6
|
+
end
|
7
|
+
|
8
|
+
def rcv(data)
|
9
|
+
return nil if data.to_s == '' || data.to_s.strip == '{}'
|
10
|
+
|
11
|
+
result = JSON.parse(data)
|
12
|
+
if result['job']
|
13
|
+
return nil if result['job'].to_s == "{}"
|
14
|
+
job = Job.new_from_hash(result['job'])
|
15
|
+
return job
|
16
|
+
end
|
17
|
+
nil
|
18
|
+
end
|
19
|
+
|
20
|
+
def base_send(tube)
|
21
|
+
{
|
22
|
+
'action' => 'get',
|
23
|
+
'params' => {
|
24
|
+
'tube' => tube
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
class AllQ
|
2
|
+
class Put < AllQ::Base
|
3
|
+
|
4
|
+
def setup
|
5
|
+
@requires_q_server = false
|
6
|
+
end
|
7
|
+
|
8
|
+
def snd(data)
|
9
|
+
result = nil
|
10
|
+
tube = data.delete('tube')
|
11
|
+
body = data.delete('body')
|
12
|
+
|
13
|
+
send_data = base_send(tube, body, data)
|
14
|
+
response = send_hash_as_json(send_data)
|
15
|
+
result = rcv(response)
|
16
|
+
build_job(result)
|
17
|
+
end
|
18
|
+
|
19
|
+
def base_send(tube, body, options = {})
|
20
|
+
raise 'Must have tube name and body' unless tube && body
|
21
|
+
base = {
|
22
|
+
'action' => 'put',
|
23
|
+
'params' => {
|
24
|
+
'tube' => tube,
|
25
|
+
'body' => body
|
26
|
+
}
|
27
|
+
}
|
28
|
+
base['params']['ttl'] = options['ttl'] if options['ttl']
|
29
|
+
base['params']['delay'] = options['delay'] if options['delay']
|
30
|
+
base['params']['parent_id'] = options['parent_id'] if options['parent_id']
|
31
|
+
return base
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
class AllQ
|
2
|
+
class Release < AllQ::Base
|
3
|
+
|
4
|
+
def snd(data)
|
5
|
+
job_id = data[:job_id]
|
6
|
+
q_server = data[:q_server]
|
7
|
+
|
8
|
+
send_data = base_send(job_id, q_server)
|
9
|
+
response = send_hash_as_json(send_data)
|
10
|
+
result = rcv(response)
|
11
|
+
return result["release"] && result["release"]["job_id"]
|
12
|
+
end
|
13
|
+
|
14
|
+
def rcv(data)
|
15
|
+
return nil if data.to_s == '' || data.to_s.strip == '{}'
|
16
|
+
JSON.parse(data)
|
17
|
+
end
|
18
|
+
|
19
|
+
def base_send(job_id, q_server)
|
20
|
+
{
|
21
|
+
'action' => 'release',
|
22
|
+
'params' => {
|
23
|
+
'job_id' => job_id,
|
24
|
+
'q_server' => q_server
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
class AllQ
|
2
|
+
class Stats < AllQ::Base
|
3
|
+
|
4
|
+
def setup
|
5
|
+
@requires_q_server = false
|
6
|
+
end
|
7
|
+
|
8
|
+
def rcv(data)
|
9
|
+
return nil if data.to_s == '' || data.to_s.strip == '{}'
|
10
|
+
results = JSON.parse(data)
|
11
|
+
stats = {}
|
12
|
+
results.each do |server, data|
|
13
|
+
data.each do |tube, tube_data|
|
14
|
+
stats[tube] = merge_tube_data(stats[tube], tube_data)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
stats
|
18
|
+
end
|
19
|
+
|
20
|
+
def merge_tube_data(original_hash, new_hash)
|
21
|
+
if original_hash.nil?
|
22
|
+
original_hash = {}
|
23
|
+
end
|
24
|
+
interize(new_hash)
|
25
|
+
new_hash.each do |k, v|
|
26
|
+
original_hash[k] = original_hash[k].to_i + v
|
27
|
+
end
|
28
|
+
original_hash
|
29
|
+
end
|
30
|
+
|
31
|
+
def interize(hash)
|
32
|
+
return {} if hash.nil? || hash.empty?
|
33
|
+
hash.update(hash){ |_, v| v.to_i }
|
34
|
+
end
|
35
|
+
|
36
|
+
def base_send(_data)
|
37
|
+
{
|
38
|
+
'action' => 'stats',
|
39
|
+
'params' => {}
|
40
|
+
}
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class AllQ
|
2
|
+
class Touch < AllQ::Base
|
3
|
+
|
4
|
+
def snd(data)
|
5
|
+
job_id = data[:job_id]
|
6
|
+
q_server = data[:q_server]
|
7
|
+
|
8
|
+
send_data = base_send(job_id, q_server)
|
9
|
+
response = send_hash_as_json(send_data)
|
10
|
+
result = rcv(response)
|
11
|
+
return result["touch"] && result["touch"]["job_id"]
|
12
|
+
end
|
13
|
+
|
14
|
+
def rcv(data)
|
15
|
+
return nil if data.to_s == '' || data.to_s.strip == '{}'
|
16
|
+
JSON.parse(data)
|
17
|
+
end
|
18
|
+
|
19
|
+
def base_send(job_id, q_server)
|
20
|
+
{
|
21
|
+
'action' => 'touch',
|
22
|
+
'params' => {
|
23
|
+
'q_server' => q_server,
|
24
|
+
'job_id' => job_id
|
25
|
+
}
|
26
|
+
}
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
end
|
data/lib/allq/client.rb
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'byebug'
|
3
|
+
|
4
|
+
class AllQ
|
5
|
+
# Represents the client singleton
|
6
|
+
class Client
|
7
|
+
include Singleton
|
8
|
+
|
9
|
+
URL = ENV['ALLQ_CLIENT_URL'] || '127.0.0.1:7766'
|
10
|
+
def initialize
|
11
|
+
@connection = AllQ::Connection.new(URL)
|
12
|
+
@get_action = AllQ::Get.new(@connection)
|
13
|
+
@put_action = AllQ::Put.new(@connection)
|
14
|
+
@done_action = AllQ::Done.new(@connection)
|
15
|
+
@stats_action = AllQ::Stats.new(@connection)
|
16
|
+
@release_action = AllQ::Release.new(@connection)
|
17
|
+
@touch_action = AllQ::Touch.new(@connection)
|
18
|
+
end
|
19
|
+
|
20
|
+
def get(tube_name)
|
21
|
+
@get_action.snd(tube_name)
|
22
|
+
end
|
23
|
+
|
24
|
+
def put(body, tube, delay = 0, ttl = 3600, priority = 5)
|
25
|
+
data = {
|
26
|
+
'body' => body,
|
27
|
+
'tube' => tube,
|
28
|
+
'delay' => delay,
|
29
|
+
'ttl' => ttl,
|
30
|
+
'priority' => priority
|
31
|
+
}
|
32
|
+
@put_action.snd(data)
|
33
|
+
end
|
34
|
+
|
35
|
+
def done(job)
|
36
|
+
raise "Can't set 'done' on a Job that is nil. Please check for Nil job before setting done." unless job
|
37
|
+
@done_action.snd(job_id: job.id, q_server: job.q_server)
|
38
|
+
end
|
39
|
+
|
40
|
+
def touch(job)
|
41
|
+
@touch_action.snd(job_id: job.id, q_server: job.q_server)
|
42
|
+
end
|
43
|
+
|
44
|
+
def stats
|
45
|
+
@stats_action.snd(nil)
|
46
|
+
end
|
47
|
+
|
48
|
+
def release(job)
|
49
|
+
@release_action.snd(job_id: job.id, q_server: job.q_server)
|
50
|
+
end
|
51
|
+
|
52
|
+
def delete(job)
|
53
|
+
end
|
54
|
+
|
55
|
+
def kick(job)
|
56
|
+
end
|
57
|
+
|
58
|
+
end
|
59
|
+
end
|
@@ -0,0 +1,153 @@
|
|
1
|
+
require 'yaml'
|
2
|
+
require 'socket'
|
3
|
+
|
4
|
+
class AllQ
|
5
|
+
# Represents a connection to a allq instance.
|
6
|
+
class Connection
|
7
|
+
|
8
|
+
# Default number of retries to send a command to a connection
|
9
|
+
MAX_RETRIES = 3
|
10
|
+
|
11
|
+
# Default retry interval
|
12
|
+
DEFAULT_RETRY_INTERVAL = 1
|
13
|
+
|
14
|
+
# Default port value for beanstalk connection
|
15
|
+
DEFAULT_PORT = 11300
|
16
|
+
|
17
|
+
attr_reader :address, :host, :port, :connection
|
18
|
+
|
19
|
+
# Initializes new connection.
|
20
|
+
#
|
21
|
+
# @param [String] address allq instance address.
|
22
|
+
# @example
|
23
|
+
# AllQ::Connection.new('127.0.0.1')
|
24
|
+
# AllQ::Connection.new('127.0.0.1:11300')
|
25
|
+
#
|
26
|
+
# ENV['ALLQ_CLIENT_URL'] = '127.0.0.1:11300'
|
27
|
+
# @b = AllQ.new
|
28
|
+
# @b.connection.host # => '127.0.0.1'
|
29
|
+
# @b.connection.port # => '11300'
|
30
|
+
#
|
31
|
+
def initialize(address = '')
|
32
|
+
@address = address || _host_from_env
|
33
|
+
@mutex = Mutex.new
|
34
|
+
establish_connection
|
35
|
+
rescue
|
36
|
+
_raise_not_connected!
|
37
|
+
end
|
38
|
+
|
39
|
+
# Send commands to allq server via connection.
|
40
|
+
#
|
41
|
+
# @param [String] command AllQ command
|
42
|
+
# @return [Array<Hash{String => String, Number}>] AllQ command response
|
43
|
+
# @example
|
44
|
+
# @conn = AllQ::Connection.new
|
45
|
+
# @conn.transmit('bury 123')
|
46
|
+
# @conn.transmit('stats')
|
47
|
+
#
|
48
|
+
def transmit(command, options={}, &block)
|
49
|
+
_with_retry(options[:retry_interval], options[:init]) do
|
50
|
+
@mutex.synchronize do
|
51
|
+
_raise_not_connected! unless @connection
|
52
|
+
command = command.force_encoding('ASCII-8BIT') if command.respond_to?(:force_encoding)
|
53
|
+
@connection.puts(command.to_s)
|
54
|
+
res = @connection.readline
|
55
|
+
yield block.call(res)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
# Close connection with allq server.
|
61
|
+
#
|
62
|
+
# @example
|
63
|
+
# @conn.close
|
64
|
+
#
|
65
|
+
def close
|
66
|
+
if @connection
|
67
|
+
@connection.close
|
68
|
+
@connection = nil
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns string representation of job.
|
73
|
+
#
|
74
|
+
# @example
|
75
|
+
# @conn.inspect
|
76
|
+
#
|
77
|
+
def to_s
|
78
|
+
"#<AllQ::Connection host=#{host.inspect} port=#{port.inspect}>"
|
79
|
+
end
|
80
|
+
alias :inspect :to_s
|
81
|
+
|
82
|
+
protected
|
83
|
+
|
84
|
+
# Establish a connection based on beanstalk address.
|
85
|
+
#
|
86
|
+
# @return [Net::TCPSocket] connection for specified address.
|
87
|
+
# @raise [AllQ::NotConnected] Could not connect to specified allq instance.
|
88
|
+
# @example
|
89
|
+
# establish_connection('localhost:3005')
|
90
|
+
#
|
91
|
+
def establish_connection
|
92
|
+
@address = address.first if address.is_a?(Array)
|
93
|
+
match = address.split(':')
|
94
|
+
@host, @port = match[0], Integer(match[1] || DEFAULT_PORT)
|
95
|
+
|
96
|
+
@connection = TCPSocket.new @host, @port
|
97
|
+
end
|
98
|
+
|
99
|
+
private
|
100
|
+
|
101
|
+
# Wrapper method for capturing certain failures and retry the payload block
|
102
|
+
#
|
103
|
+
# @param [Proc] block The command to execute.
|
104
|
+
# @param [Integer] retry_interval The time to wait before the next retry
|
105
|
+
# @param [Integer] tries The maximum number of tries in draining mode
|
106
|
+
# @return [Object] Result of the block passed
|
107
|
+
#
|
108
|
+
def _with_retry(retry_interval, init=true, tries=MAX_RETRIES, &block)
|
109
|
+
yield
|
110
|
+
rescue EOFError, Errno::ECONNRESET, Errno::EPIPE,
|
111
|
+
Errno::ECONNREFUSED => ex
|
112
|
+
|
113
|
+
_reconnect(ex, retry_interval)
|
114
|
+
_initialize_tubes if init
|
115
|
+
retry
|
116
|
+
end
|
117
|
+
|
118
|
+
# Tries to re-establish connection to the allq
|
119
|
+
#
|
120
|
+
# @param [Exception] original_exception The exception caused the retry
|
121
|
+
# @param [Integer] retry_interval The time to wait before the next reconnect
|
122
|
+
# @param [Integer] tries The maximum number of attempts to reconnect
|
123
|
+
def _reconnect(original_exception, retry_interval, tries=MAX_RETRIES)
|
124
|
+
close
|
125
|
+
establish_connection
|
126
|
+
rescue Errno::ECONNREFUSED
|
127
|
+
tries -= 1
|
128
|
+
if tries.zero?
|
129
|
+
_raise_not_connected!
|
130
|
+
end
|
131
|
+
sleep(retry_interval || DEFAULT_RETRY_INTERVAL)
|
132
|
+
retry
|
133
|
+
end
|
134
|
+
|
135
|
+
# The host provided by ALLQ_CLIENT_URL environment variable, if available.
|
136
|
+
#
|
137
|
+
# @return [String] A allq host address
|
138
|
+
# @example
|
139
|
+
# ENV['ALLQ_CLIENT_URL'] = "localhost:1212"
|
140
|
+
# # => 'localhost:1212'
|
141
|
+
#
|
142
|
+
def _host_from_env
|
143
|
+
ENV['ALLQ_CLIENT_URL'].respond_to?(:length) && ENV['ALLQ_CLIENT_URL'].length > 0 && ENV['ALLQ_CLIENT_URL'].strip
|
144
|
+
end
|
145
|
+
|
146
|
+
# Raises an error to be triggered when the connection has failed
|
147
|
+
# @raise [AllQ::NotConnected] AllQ is no longer connected
|
148
|
+
def _raise_not_connected!
|
149
|
+
raise AllQ::NotConnected, "Connection to allq '#{@host}:#{@port}' is closed!"
|
150
|
+
end
|
151
|
+
|
152
|
+
end # Connection
|
153
|
+
end # AllQ
|
data/lib/allq/job.rb
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
class AllQ
|
2
|
+
|
3
|
+
class Job
|
4
|
+
attr_accessor :id, :q_server, :body, :expired_count
|
5
|
+
def initialize(id, q_server, tube = nil, body = nil, expired_count = nil)
|
6
|
+
@body = body
|
7
|
+
@q_server = q_server
|
8
|
+
@id = id
|
9
|
+
@tube = tube
|
10
|
+
@expired_count = expired_count
|
11
|
+
end
|
12
|
+
|
13
|
+
def to_hash
|
14
|
+
{
|
15
|
+
'id' => @id,
|
16
|
+
'q_server' => @q_server,
|
17
|
+
'body' => @body,
|
18
|
+
'tube' => @tube,
|
19
|
+
'expired_count' => @expired_count
|
20
|
+
}
|
21
|
+
end
|
22
|
+
|
23
|
+
def done
|
24
|
+
AllQ::Client.instance.done(self)
|
25
|
+
end
|
26
|
+
|
27
|
+
def delete
|
28
|
+
AllQ::Client.instance.delete(self)
|
29
|
+
end
|
30
|
+
|
31
|
+
def touch
|
32
|
+
AllQ::Client.instance.touch(self)
|
33
|
+
end
|
34
|
+
|
35
|
+
def release
|
36
|
+
AllQ::Client.instance.release(self)
|
37
|
+
end
|
38
|
+
|
39
|
+
def bury
|
40
|
+
AllQ::Client.instance.bury(self)
|
41
|
+
end
|
42
|
+
|
43
|
+
def to_json
|
44
|
+
{
|
45
|
+
id: @id,
|
46
|
+
q_server: @q_server,
|
47
|
+
body: @body
|
48
|
+
}
|
49
|
+
end
|
50
|
+
|
51
|
+
def to_s
|
52
|
+
to_json.to_json
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.new_from_hash(hash)
|
56
|
+
puts hash.inspect
|
57
|
+
begin
|
58
|
+
id = hash.fetch('id')
|
59
|
+
body = hash.fetch('body')
|
60
|
+
tube = hash.fetch('tube')
|
61
|
+
expired_count = hash.fetch('expired_count')
|
62
|
+
q_server = hash.fetch('q_server')
|
63
|
+
puts hash.inspect
|
64
|
+
job = Job.new(id, q_server, tube, body, expired_count)
|
65
|
+
return job
|
66
|
+
rescue => ex
|
67
|
+
puts 'Invalid job data i'
|
68
|
+
end
|
69
|
+
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
data/lib/allq/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: allq
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jason
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-04-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
description: Ruby gem for using AllQ
|
56
|
+
email:
|
57
|
+
- jaciones@gmail.com
|
58
|
+
executables: []
|
59
|
+
extensions: []
|
60
|
+
extra_rdoc_files: []
|
61
|
+
files:
|
62
|
+
- ".gitignore"
|
63
|
+
- ".rspec"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- allq.gemspec
|
71
|
+
- bin/console
|
72
|
+
- bin/setup
|
73
|
+
- lib/allq.rb
|
74
|
+
- lib/allq/actions/base.rb
|
75
|
+
- lib/allq/actions/delete.rb
|
76
|
+
- lib/allq/actions/done.rb
|
77
|
+
- lib/allq/actions/get.rb
|
78
|
+
- lib/allq/actions/put.rb
|
79
|
+
- lib/allq/actions/release.rb
|
80
|
+
- lib/allq/actions/stats.rb
|
81
|
+
- lib/allq/actions/touch.rb
|
82
|
+
- lib/allq/client.rb
|
83
|
+
- lib/allq/connection.rb
|
84
|
+
- lib/allq/job.rb
|
85
|
+
- lib/allq/version.rb
|
86
|
+
homepage: https://github.com/blitline-dev/allq_gem
|
87
|
+
licenses:
|
88
|
+
- MIT
|
89
|
+
metadata: {}
|
90
|
+
post_install_message:
|
91
|
+
rdoc_options: []
|
92
|
+
require_paths:
|
93
|
+
- lib
|
94
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
95
|
+
requirements:
|
96
|
+
- - ">="
|
97
|
+
- !ruby/object:Gem::Version
|
98
|
+
version: '0'
|
99
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project:
|
106
|
+
rubygems_version: 2.6.12
|
107
|
+
signing_key:
|
108
|
+
specification_version: 4
|
109
|
+
summary: Ruby library for using AllQ
|
110
|
+
test_files: []
|