qyu-store-redis 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 +56 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +72 -0
- data/Rakefile +6 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/qyu/store/redis.rb +36 -0
- data/lib/qyu/store/redis/adapter.rb +294 -0
- data/lib/qyu/store/redis/configuration_validator.rb +32 -0
- data/lib/qyu/store/redis/errors.rb +3 -0
- data/lib/qyu/store/redis/errors/task_not_found.rb +14 -0
- data/lib/qyu/store/redis/errors/workflow_not_unique.rb +14 -0
- data/lib/qyu/store/redis/logger.rb +11 -0
- data/lib/qyu/store/redis/version.rb +7 -0
- data/qyu-store-redis.gemspec +33 -0
- metadata +189 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f466d68cd1a2b2edd48dfef7ba4ff4b3875952181d4b55f84eda8c5882cf112e
|
4
|
+
data.tar.gz: aaeccdc3ac905c4c100da97d844ea31db2dc687ba88bb533f275ca8dfed0bb3c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 872cccc3987783f8dc2c992c79b9cdae2b6a55d9dcade23ac82b9de4dbb36e7db9dabb88639e2d04d18bf8d47eb00f08571a1c6fcb76652a9ee5a72b7fb78884
|
7
|
+
data.tar.gz: 3bee4f57f4062f1fd03bd82ad2c9ef0b257a9d455354c7ef61f7eaa0375fa54fe046366c00695aad7cc63c3f5d585038f73c588d0307423b7761fc2395f4bad0
|
data/.gitignore
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
/.config
|
4
|
+
/coverage/
|
5
|
+
/InstalledFiles
|
6
|
+
/pkg/
|
7
|
+
/spec/reports/
|
8
|
+
/spec/examples.txt
|
9
|
+
/test/tmp/
|
10
|
+
/test/version_tmp/
|
11
|
+
/tmp/
|
12
|
+
|
13
|
+
# Used by dotenv library to load environment variables.
|
14
|
+
# .env
|
15
|
+
|
16
|
+
## Specific to RubyMotion:
|
17
|
+
.dat*
|
18
|
+
.repl_history
|
19
|
+
build/
|
20
|
+
*.bridgesupport
|
21
|
+
build-iPhoneOS/
|
22
|
+
build-iPhoneSimulator/
|
23
|
+
|
24
|
+
## Specific to RubyMotion (use of CocoaPods):
|
25
|
+
#
|
26
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
27
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
28
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
29
|
+
#
|
30
|
+
# vendor/Pods/
|
31
|
+
|
32
|
+
## Documentation cache and generated files:
|
33
|
+
/.yardoc/
|
34
|
+
/_yardoc/
|
35
|
+
/doc/
|
36
|
+
/rdoc/
|
37
|
+
|
38
|
+
## Environment normalization:
|
39
|
+
/.bundle/
|
40
|
+
/vendor/bundle
|
41
|
+
/lib/bundler/man/
|
42
|
+
|
43
|
+
# for a library or gem, you might want to ignore these files since the code is
|
44
|
+
# intended to run in multiple environments; otherwise, check them in:
|
45
|
+
Gemfile.lock
|
46
|
+
.ruby-version
|
47
|
+
.ruby-gemset
|
48
|
+
|
49
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
50
|
+
.rvmrc
|
51
|
+
|
52
|
+
# rspec failure tracking
|
53
|
+
.rspec_status
|
54
|
+
|
55
|
+
# byebug history
|
56
|
+
.byebug_history
|
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 me@zzet.org. 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
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 FindHotel
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
# Qyu::Store::Redis
|
2
|
+
|
3
|
+
[![Gem Version](https://img.shields.io/gem/v/qyu-store-redis.svg)](https://rubygems.org/gems/qyu-store-redis)
|
4
|
+
[![Build Status](https://travis-ci.org/FindHotel/qyu-store-redis.svg)](https://travis-ci.org/FindHotel/qyu-store-redis)
|
5
|
+
|
6
|
+
## Requirements:
|
7
|
+
|
8
|
+
* Ruby 2.4.0 or newer
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'qyu-store-redis'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install qyu-store-redis
|
25
|
+
|
26
|
+
## Configuration
|
27
|
+
|
28
|
+
To start using Qyu; you need a queue configuration and a state store configuration. Here's an example:
|
29
|
+
```ruby
|
30
|
+
require 'qyu'
|
31
|
+
require 'qyu/store/redis'
|
32
|
+
|
33
|
+
Qyu.configure(
|
34
|
+
queue: {
|
35
|
+
type: :memory
|
36
|
+
# Or one of the other production-ready queues available
|
37
|
+
# Check https://github.com/FindHotel/qyu/wiki/Message-Queues
|
38
|
+
},
|
39
|
+
store: {
|
40
|
+
type: :redis,
|
41
|
+
host: redis_host,
|
42
|
+
port: redis_port,
|
43
|
+
password: redis_password,
|
44
|
+
db: redis_db_number,
|
45
|
+
lease_period: 60
|
46
|
+
},
|
47
|
+
# optional Defaults to STDOUT
|
48
|
+
logger: Logger.new(STDOUT)
|
49
|
+
)
|
50
|
+
```
|
51
|
+
|
52
|
+
## Usage
|
53
|
+
|
54
|
+
TODO: Write usage instructions here
|
55
|
+
|
56
|
+
## Development
|
57
|
+
|
58
|
+
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.
|
59
|
+
|
60
|
+
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).
|
61
|
+
|
62
|
+
## Contributing
|
63
|
+
|
64
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/FindHotel/qyu-store-redis. 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.
|
65
|
+
|
66
|
+
## License
|
67
|
+
|
68
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
69
|
+
|
70
|
+
## Code of Conduct
|
71
|
+
|
72
|
+
Everyone interacting in the Qyu::Store::Redis project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/FindHotel/qyu-store-redis/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'qyu'
|
5
|
+
require 'qyu/store/redis'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'pry'
|
15
|
+
Pry.start
|
data/bin/setup
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require_relative './redis/version'
|
2
|
+
require_relative './redis/errors'
|
3
|
+
require 'json'
|
4
|
+
require 'redis'
|
5
|
+
require 'redis-namespace'
|
6
|
+
|
7
|
+
module Qyu
|
8
|
+
module Store
|
9
|
+
module Redis
|
10
|
+
autoload :Adapter, 'qyu/store/redis/adapter'
|
11
|
+
autoload :Logger, 'qyu/store/redis/logger'
|
12
|
+
autoload :ConfigurationValidator, 'qyu/store/redis/configuration_validator'
|
13
|
+
|
14
|
+
class << self
|
15
|
+
def interface
|
16
|
+
defined?(Qyu::Store::Base) ? Qyu::Store::Base : Object
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
class << self
|
23
|
+
unless defined?(logger)
|
24
|
+
def logger=(logger)
|
25
|
+
@@__logger = logger
|
26
|
+
end
|
27
|
+
|
28
|
+
def logger
|
29
|
+
@@__logger ||= Qyu::Store::Redis::Logger.new(STDOUT)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Qyu::Config::StoreConfig.register(Qyu::Store::Redis::Adapter) if defined?(Qyu::Config::StoreConfig)
|
36
|
+
Qyu::Factory::StoreFactory.register(Qyu::Store::Redis::Adapter) if defined?(Qyu::Factory::StoreFactory)
|
@@ -0,0 +1,294 @@
|
|
1
|
+
require 'securerandom'
|
2
|
+
|
3
|
+
module Qyu
|
4
|
+
module Store
|
5
|
+
module Redis
|
6
|
+
# Qyu::Store::Redis::Adapter
|
7
|
+
class Adapter < Qyu::Store::Redis.interface
|
8
|
+
TYPE = :redis
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def valid_config?(config)
|
12
|
+
ConfigurationValidator.new(config).valid?
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
def initialize(config)
|
17
|
+
init_client(config)
|
18
|
+
end
|
19
|
+
|
20
|
+
## Workflow
|
21
|
+
def persist_workflow(name, descriptor)
|
22
|
+
id = SecureRandom.uuid
|
23
|
+
validate_workflow!(name)
|
24
|
+
key = "workflow:#{name}:#{id}"
|
25
|
+
@client.hmset(key, :name, name, :id, id, :descriptor, serialize { descriptor })
|
26
|
+
{ 'name' => name, 'id' => id, 'descriptor' => descriptor }
|
27
|
+
end
|
28
|
+
|
29
|
+
def find_workflow(id)
|
30
|
+
workflow_key = @client.keys("workflow:*:#{id}").first
|
31
|
+
return if workflow_key.nil?
|
32
|
+
workflow = @client.hgetall(workflow_key)
|
33
|
+
workflow['descriptor'] = parse { workflow['descriptor'] }
|
34
|
+
workflow
|
35
|
+
end
|
36
|
+
|
37
|
+
def find_workflow_by_name(name)
|
38
|
+
workflow_key = @client.keys("workflow:#{name}:*").first
|
39
|
+
return if workflow_key.nil?
|
40
|
+
workflow = @client.hgetall(workflow_key)
|
41
|
+
workflow['descriptor'] = parse { workflow['descriptor'] }
|
42
|
+
workflow
|
43
|
+
end
|
44
|
+
|
45
|
+
def delete_workflow(id)
|
46
|
+
workflow_key = @client.keys("workflow:*:#{id}").first
|
47
|
+
deleted = @client.del(workflow_key)
|
48
|
+
deleted > 0
|
49
|
+
end
|
50
|
+
|
51
|
+
def delete_workflow_by_name(name)
|
52
|
+
workflow_key = @client.keys("workflow:#{name}:*").first
|
53
|
+
deleted = @client.del(workflow_key)
|
54
|
+
deleted > 0
|
55
|
+
end
|
56
|
+
|
57
|
+
## Job
|
58
|
+
def persist_job(workflow, payload)
|
59
|
+
id = SecureRandom.uuid
|
60
|
+
key = "job:#{id}"
|
61
|
+
@client.hmset(key,
|
62
|
+
:id, id,
|
63
|
+
:workflow, serialize { workflow },
|
64
|
+
:payload, serialize { payload }
|
65
|
+
)
|
66
|
+
{ 'id' => id, 'workflow' => workflow, 'payload' => payload }
|
67
|
+
end
|
68
|
+
|
69
|
+
def find_job(id)
|
70
|
+
job = @client.hgetall("job:#{id}")
|
71
|
+
return if job.eql?({})
|
72
|
+
job['id'] = id
|
73
|
+
job['payload'] = parse { job['payload'] }
|
74
|
+
job['workflow'] = parse { job['workflow'] }
|
75
|
+
job
|
76
|
+
end
|
77
|
+
|
78
|
+
def select_jobs(limit, offset, order = nil)
|
79
|
+
job_keys = @client.keys('job:*')
|
80
|
+
partial_job_keys = job_keys[offset.to_i, limit.to_i]
|
81
|
+
return [] unless partial_job_keys
|
82
|
+
|
83
|
+
partial_job_keys.map do |job_key|
|
84
|
+
job = @client.hgetall(job_key)
|
85
|
+
job['payload'] = parse { job['payload'] }
|
86
|
+
job['workflow'] = parse { job['workflow'] }
|
87
|
+
job
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
91
|
+
def count_jobs
|
92
|
+
@client.keys("job:*").count
|
93
|
+
end
|
94
|
+
|
95
|
+
def delete_job(id)
|
96
|
+
deleted = @client.del("job:#{id}")
|
97
|
+
deleted > 0
|
98
|
+
end
|
99
|
+
|
100
|
+
def clear_completed_jobs
|
101
|
+
# TODO
|
102
|
+
end
|
103
|
+
|
104
|
+
## Task
|
105
|
+
def find_or_persist_task(name, queue_name, payload, job_id, parent_task_id)
|
106
|
+
task_id = nil
|
107
|
+
existent_keys = @client.keys("task:#{name}:#{queue_name}:#{job_id}:#{parent_task_id}:*")
|
108
|
+
|
109
|
+
existent_keys.each do |task_key|
|
110
|
+
task = @client.hgetall(task_key)
|
111
|
+
task_payload = parse { task['payload'] }
|
112
|
+
|
113
|
+
if compare_payloads(task_payload, payload)
|
114
|
+
task_id = task['id']
|
115
|
+
break
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
unless task_id
|
120
|
+
task_id = SecureRandom.uuid
|
121
|
+
key = "task:#{name}:#{queue_name}:#{job_id}:#{parent_task_id}:#{task_id}"
|
122
|
+
@client.hmset(
|
123
|
+
key,
|
124
|
+
:id, task_id,
|
125
|
+
:name, name,
|
126
|
+
:queue_name, queue_name,
|
127
|
+
:payload, serialize { payload },
|
128
|
+
:job_id, job_id,
|
129
|
+
:parent_task_id, parent_task_id
|
130
|
+
)
|
131
|
+
end
|
132
|
+
|
133
|
+
task_id
|
134
|
+
end
|
135
|
+
|
136
|
+
def find_task(id)
|
137
|
+
task_key = @client.keys("task:*:*:*:*:#{id}").first
|
138
|
+
return if task_key.nil?
|
139
|
+
task = @client.hgetall(task_key)
|
140
|
+
task['payload'] = parse { task['payload'] }
|
141
|
+
task
|
142
|
+
end
|
143
|
+
|
144
|
+
def find_task_ids_by_job_id_and_name(job_id, name)
|
145
|
+
task_keys = @client.keys("task:#{name}:*:#{job_id}:*:*")
|
146
|
+
return if task_keys.empty?
|
147
|
+
task_keys.map do |task_key|
|
148
|
+
@client.hget(task_key, 'id')
|
149
|
+
end.uniq
|
150
|
+
end
|
151
|
+
|
152
|
+
def find_task_ids_by_job_id_name_and_parent_task_ids(job_id, name, parent_task_ids)
|
153
|
+
task_keys = parent_task_ids.flat_map do |parent_task_id|
|
154
|
+
@client.keys("task:#{name}:*:#{job_id}:#{parent_task_id}:*")
|
155
|
+
end
|
156
|
+
return if task_keys.empty?
|
157
|
+
task_keys.map do |task_key|
|
158
|
+
@client.hget(task_key, 'id')
|
159
|
+
end.uniq
|
160
|
+
end
|
161
|
+
|
162
|
+
def select_tasks_by_job_id(job_id)
|
163
|
+
task_keys = @client.keys("task:*:*:#{job_id}:*:*")
|
164
|
+
return if task_keys.empty?
|
165
|
+
task_keys.map do |task_key|
|
166
|
+
task = @client.hgetall(task_key)
|
167
|
+
task['payload'] = parse { task['payload'] }
|
168
|
+
task
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def lock_task!(id, lease_time)
|
173
|
+
Qyu.logger.debug '[LOCK] lock_task!'
|
174
|
+
|
175
|
+
task_key = @client.keys("task:*:*:*:*:#{id}").first
|
176
|
+
task = find_task(id)
|
177
|
+
raise Qyu::Store::Redis::Errors::TaskNotFound unless task
|
178
|
+
|
179
|
+
response = nil
|
180
|
+
if task['locked_until'].nil? || DateTime.parse(task['locked_until']) < DateTime.now
|
181
|
+
uuid = SecureRandom.uuid
|
182
|
+
Qyu.logger.debug "[LOCK] uuid = #{uuid}"
|
183
|
+
|
184
|
+
locked_until = seconds_after_time(lease_time)
|
185
|
+
Qyu.logger.debug "[LOCK] locked_until = #{locked_until}"
|
186
|
+
|
187
|
+
response = @client.hmset(task_key, 'locked_by', uuid, 'locked_until', locked_until)
|
188
|
+
end
|
189
|
+
|
190
|
+
response.eql?('OK') ? [uuid, locked_until] : [nil, nil]
|
191
|
+
end
|
192
|
+
|
193
|
+
def unlock_task!(id, lease_token)
|
194
|
+
task_key = @client.keys("task:*:*:*:*:#{id}").first
|
195
|
+
task = find_task(id)
|
196
|
+
raise Qyu::Store::Redis::Errors::TaskNotFound unless task
|
197
|
+
|
198
|
+
if task['locked_by'].eql?(lease_token)
|
199
|
+
@client.hmset(task_key, 'locked_by', nil, 'locked_until', nil).eql?('OK')
|
200
|
+
else
|
201
|
+
false
|
202
|
+
end
|
203
|
+
end
|
204
|
+
|
205
|
+
def renew_lock_lease(id, lease_time, lease_token)
|
206
|
+
Qyu.logger.debug "renew_lock_lease id = #{id}, lease_time = #{lease_time}, lease_token = #{lease_token}"
|
207
|
+
|
208
|
+
task_key = @client.keys("task:*:*:*:*:#{id}").first
|
209
|
+
task = find_task(id)
|
210
|
+
return nil unless task&.fetch('locked_until')
|
211
|
+
|
212
|
+
if task['locked_by'].eql?(lease_token) && DateTime.parse(task['locked_until']) > DateTime.now
|
213
|
+
locked_until = seconds_after_time(lease_time)
|
214
|
+
Qyu.logger.debug "[LOCK] locked_until = #{locked_until}"
|
215
|
+
|
216
|
+
@client.hmset(task_key, 'locked_until', locked_until)
|
217
|
+
locked_until
|
218
|
+
else
|
219
|
+
return nil
|
220
|
+
end
|
221
|
+
end
|
222
|
+
|
223
|
+
def update_status(id, status)
|
224
|
+
task_key = @client.keys("task:*:*:*:*:#{id}").first
|
225
|
+
raise Qyu::Store::Redis::Errors::TaskNotFound unless task_key
|
226
|
+
@client.hmset(task_key, 'status', status).eql?('OK')
|
227
|
+
end
|
228
|
+
|
229
|
+
def with_connection
|
230
|
+
yield
|
231
|
+
end
|
232
|
+
|
233
|
+
def transaction
|
234
|
+
# TODO
|
235
|
+
yield
|
236
|
+
end
|
237
|
+
|
238
|
+
private
|
239
|
+
|
240
|
+
def serialize
|
241
|
+
yield.to_json
|
242
|
+
end
|
243
|
+
|
244
|
+
def parse
|
245
|
+
JSON.parse(yield)
|
246
|
+
end
|
247
|
+
|
248
|
+
def validate_workflow!(name)
|
249
|
+
workflow_keys = @client.keys("workflow:#{name}:*")
|
250
|
+
raise Qyu::Store::Redis::Errors::WorkflowNotUnique if workflow_keys.any?
|
251
|
+
end
|
252
|
+
|
253
|
+
def compare_payloads(payload1, payload2)
|
254
|
+
symbolize_hash(payload1) == symbolize_hash(payload2)
|
255
|
+
end
|
256
|
+
|
257
|
+
def symbolize_hash(hash)
|
258
|
+
hash.map { |key, value| [key.to_sym, value] }.to_h
|
259
|
+
end
|
260
|
+
|
261
|
+
def init_client(config)
|
262
|
+
load_config(config)
|
263
|
+
redis_client = ::Redis.new(@@redis_configuration)
|
264
|
+
@client = ::Redis::Namespace.new(@@redis_configuration[:namespace], :redis => redis_client)
|
265
|
+
end
|
266
|
+
|
267
|
+
def load_config(config)
|
268
|
+
if config[:url]
|
269
|
+
@@redis_configuration = { url: config[:url] }
|
270
|
+
else
|
271
|
+
@@redis_configuration = {
|
272
|
+
host: config[:host],
|
273
|
+
port: config[:port],
|
274
|
+
password: config[:password],
|
275
|
+
db: config.fetch(:db) { 0 }
|
276
|
+
}.compact
|
277
|
+
end
|
278
|
+
|
279
|
+
@@redis_configuration[:ssl_params] = config[:ssl_params] if config[:ssl_params]
|
280
|
+
@@redis_configuration[:timeout] = config[:timeout] if config[:timeout]
|
281
|
+
@@redis_configuration[:connect_timeout] = config[:connect_timeout] if config[:connect_timeout]
|
282
|
+
@@redis_configuration[:read_timeout] = config[:read_timeout] if config[:read_timeout]
|
283
|
+
@@redis_configuration[:write_timeout] = config[:write_timeout] if config[:write_timeout]
|
284
|
+
@@redis_configuration[:namespace] = config.fetch(:namespace) { 'qyu' }
|
285
|
+
true
|
286
|
+
end
|
287
|
+
|
288
|
+
def seconds_after_time(seconds, start_time = Time.now.utc)
|
289
|
+
start_time + seconds
|
290
|
+
end
|
291
|
+
end
|
292
|
+
end
|
293
|
+
end
|
294
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Qyu
|
2
|
+
module Store
|
3
|
+
module Redis
|
4
|
+
# Qyu::Store::Redis::ConfigurationValidator
|
5
|
+
class ConfigurationValidator
|
6
|
+
REQUIRED_ATTRIBUTES = %i[host port].freeze
|
7
|
+
|
8
|
+
attr_reader :errors
|
9
|
+
|
10
|
+
def initialize(config)
|
11
|
+
@config = config
|
12
|
+
@errors = []
|
13
|
+
end
|
14
|
+
|
15
|
+
def valid?
|
16
|
+
validate
|
17
|
+
errors.empty?
|
18
|
+
end
|
19
|
+
|
20
|
+
def validate
|
21
|
+
unless @config[:url]
|
22
|
+
REQUIRED_ATTRIBUTES.each do |attribute|
|
23
|
+
next unless @config[attribute].nil?
|
24
|
+
|
25
|
+
@errors << "#{attribute} must be present."
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'qyu/store/redis/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'qyu-store-redis'
|
8
|
+
spec.version = Qyu::Store::Redis::VERSION
|
9
|
+
spec.authors = ['Mohamed Osama']
|
10
|
+
spec.email = ['mohamed.o.alnagdy@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = 'Redis state store for Qyu https://rubygems.org/gems/qyu'
|
13
|
+
spec.description = 'Redis state store for Qyu https://rubygems.org/gems/qyu'
|
14
|
+
spec.homepage = 'https://github.com/FindHotel/qyu-store-redis'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
18
|
+
end
|
19
|
+
spec.bindir = 'exe'
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
24
|
+
spec.add_development_dependency 'pry', '~> 0.11'
|
25
|
+
spec.add_development_dependency 'pry-byebug', '~> 3.4'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.5'
|
28
|
+
spec.add_development_dependency 'shoulda-matchers', '~> 3.1'
|
29
|
+
spec.add_development_dependency 'simplecov'
|
30
|
+
|
31
|
+
spec.add_runtime_dependency 'redis', '~> 4.0'
|
32
|
+
spec.add_runtime_dependency 'redis-namespace', '~> 1.6.0'
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,189 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: qyu-store-redis
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mohamed Osama
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-02-19 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: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.11'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.11'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry-byebug
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.4'
|
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
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.5'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.5'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: shoulda-matchers
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.1'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.1'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: simplecov
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: redis
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '4.0'
|
118
|
+
type: :runtime
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '4.0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: redis-namespace
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 1.6.0
|
132
|
+
type: :runtime
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 1.6.0
|
139
|
+
description: Redis state store for Qyu https://rubygems.org/gems/qyu
|
140
|
+
email:
|
141
|
+
- mohamed.o.alnagdy@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- ".gitignore"
|
147
|
+
- ".rspec"
|
148
|
+
- ".travis.yml"
|
149
|
+
- CODE_OF_CONDUCT.md
|
150
|
+
- Gemfile
|
151
|
+
- LICENSE
|
152
|
+
- README.md
|
153
|
+
- Rakefile
|
154
|
+
- bin/console
|
155
|
+
- bin/setup
|
156
|
+
- lib/qyu/store/redis.rb
|
157
|
+
- lib/qyu/store/redis/adapter.rb
|
158
|
+
- lib/qyu/store/redis/configuration_validator.rb
|
159
|
+
- lib/qyu/store/redis/errors.rb
|
160
|
+
- lib/qyu/store/redis/errors/task_not_found.rb
|
161
|
+
- lib/qyu/store/redis/errors/workflow_not_unique.rb
|
162
|
+
- lib/qyu/store/redis/logger.rb
|
163
|
+
- lib/qyu/store/redis/version.rb
|
164
|
+
- qyu-store-redis.gemspec
|
165
|
+
homepage: https://github.com/FindHotel/qyu-store-redis
|
166
|
+
licenses:
|
167
|
+
- MIT
|
168
|
+
metadata: {}
|
169
|
+
post_install_message:
|
170
|
+
rdoc_options: []
|
171
|
+
require_paths:
|
172
|
+
- lib
|
173
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
174
|
+
requirements:
|
175
|
+
- - ">="
|
176
|
+
- !ruby/object:Gem::Version
|
177
|
+
version: '0'
|
178
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
179
|
+
requirements:
|
180
|
+
- - ">="
|
181
|
+
- !ruby/object:Gem::Version
|
182
|
+
version: '0'
|
183
|
+
requirements: []
|
184
|
+
rubyforge_project:
|
185
|
+
rubygems_version: 2.7.4
|
186
|
+
signing_key:
|
187
|
+
specification_version: 4
|
188
|
+
summary: Redis state store for Qyu https://rubygems.org/gems/qyu
|
189
|
+
test_files: []
|