sneakers-laces 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b0c52511ab7dd40a16bebd3315581e6543205829d3473689d8d05bf546bc4b4b
4
+ data.tar.gz: d7a643d5a140b97ed083d01d59ad81beb88f35a6c1c735c00a3b63d689426e18
5
+ SHA512:
6
+ metadata.gz: e561b8eb24290121f9201c80fddf2bca90e7ca28bbba0ae64f6ef4bd0474417e9961f97c0143795f55c20cfa24a35a4c9e83b2398e19e38e498fef1904b6d056
7
+ data.tar.gz: 345fad59c3b609c618568ec4fc6450a09343440d73d413b38c7c99e5152b350f2c1873f10b329943d271ad778c9d5638ecacc4f12d137bda519030f6e8f729db
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+ /.ruby-gemset
10
+ /.ruby-version
11
+ /Gemfile.lock
12
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --require spec_helper
@@ -0,0 +1,23 @@
1
+ require: rubocop-rspec
2
+
3
+ Documentation:
4
+ Enabled: false
5
+
6
+ Metrics/LineLength:
7
+ Max: 120
8
+
9
+ Metrics/BlockLength:
10
+ Exclude:
11
+ - 'spec/**/*'
12
+
13
+ Naming/ConstantName:
14
+ Exclude:
15
+ - 'lib/sneakers/laces/worker.rb'
16
+
17
+ Style/MutableConstant:
18
+ Exclude:
19
+ - 'lib/sneakers/laces/worker.rb'
20
+
21
+ Metrics/ParameterLists:
22
+ Exclude:
23
+ - 'lib/sneakers/laces/queue_manager.rb'
@@ -0,0 +1,14 @@
1
+ language: ruby
2
+
3
+ sudo: required
4
+
5
+ services:
6
+ - rabbitmq
7
+
8
+ rvm:
9
+ - "2.5.1"
10
+
11
+ cache:
12
+ bundler: true
13
+
14
+ script: 'bundle exec rspec'
data/Gemfile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
6
+
7
+ # Specify your gem's dependencies in sneakers-laces.gemspec
8
+ gemspec
9
+
10
+ gem 'pry-byebug'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Rustam Sharshenov
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.
@@ -0,0 +1,77 @@
1
+ # Laces for [sneakers](https://github.com/jondot/sneakers) [![Build Status](https://travis-ci.org/sharshenov/sneakers-laces.svg?branch=master)](https://travis-ci.org/sharshenov/sneakers-laces) [![Gem Version](https://badge.fury.io/rb/sneakers-laces.svg)](https://badge.fury.io/rb/sneakers-laces)
2
+
3
+ The project allows you to manage Sneakers queue processing dynamically. It uses RabbitMQ HTTP API to manage queues and utilizes [live restart](https://github.com/treasure-data/serverengine#live-restart) to reload workers.
4
+
5
+ It can be helpful for processing IO-jobs grouped by some criteria without waiting for jobs with other criterias to be executed. Imagine requesting some remote API on user basis. Requests of particular user can be executed in parallel and should not be postponed by requests of other users.
6
+
7
+ With help of the Sneakers::Laces you can declare worker classes with multiple queues per worker class. When you declare or delete a queue the worker is gracefully reloads itself without main process restart(Side effect: It is a docker-friendly solution).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'sneakers-laces'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install sneakers-laces
24
+
25
+ ## Usage on consumer-side
26
+
27
+ Declare your workers and run instance
28
+ ```ruby
29
+ require 'sneakers/laces'
30
+
31
+ # Be sure to configure Sneakers same way as on producer side
32
+ Sneakers.configure
33
+
34
+ class RequestAPIWorker
35
+ include Sneakers::Laces::Worker
36
+
37
+ from_tag :api_requester
38
+
39
+ def work(message)
40
+ logger.info("Doing some API request for #{message}")
41
+ ack!
42
+ end
43
+ end
44
+
45
+ require 'sneakers/laces/runner'
46
+
47
+ Sneakers::Laces::Runner.new.run
48
+ ```
49
+
50
+ ## Usage on producer-side
51
+
52
+ ```ruby
53
+ require 'sneakers/laces'
54
+
55
+ # Be sure to configure Sneakers same way as on consumer side
56
+ Sneakers.configure
57
+
58
+ queue_manager = Sneakers::Laces::QueueManager.new
59
+
60
+ # This will declare queue, bindings, and send message to reload workers
61
+ queue_manager.declare_queue name: 'user_1', worker_tag: 'api_requester'
62
+ queue_manager.declare_queue name: 'user_2', worker_tag: 'api_requester'
63
+
64
+ Sneakers.publish('user 1', to_queue: 'user_1')
65
+ Sneakers.publish('user 2', to_queue: 'user_2')
66
+
67
+ # This will delete queue and send message to reload workers
68
+ queue_manager.delete_queue name: 'user_2', worker_tag: 'api_requester'
69
+ ```
70
+
71
+ ## Contributing
72
+
73
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/sharshenov/sneakers-laces).
74
+
75
+ ## License
76
+
77
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ task default: :spec
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rabbitmq/http/client'
4
+ require 'active_support'
5
+ require 'active_support/core_ext'
6
+
7
+ require 'sneakers/laces/version'
8
+ require 'sneakers/laces/configuration'
9
+ require 'sneakers/laces/queue_manager'
10
+ require 'sneakers/laces/worker_manager'
11
+ require 'sneakers/laces/worker'
12
+ require 'sneakers/laces/reload_worker'
13
+ require 'sneakers'
14
+
15
+ module Sneakers
16
+ module Laces
17
+ class << self
18
+ def config
19
+ @config ||= Configuration.new
20
+ end
21
+
22
+ def configure
23
+ yield(config)
24
+ @api_client = nil # ensure api client uses actual endpoint
25
+ end
26
+
27
+ def api_client
28
+ @api_client ||= RabbitMQ::HTTP::Client.new(config.rabbitmq_api_endpoint)
29
+ end
30
+
31
+ def reload(worker_tag:)
32
+ Sneakers.publish('reload, please', routing_key: ReloadWorker.routing_key(worker_tag: worker_tag))
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sneakers
4
+ module Laces
5
+ class Configuration
6
+ attr_writer :rabbitmq_api_endpoint,
7
+ :vhost
8
+
9
+ def vhost
10
+ @vhost || Sneakers::CONFIG.fetch(:vhost)
11
+ end
12
+
13
+ def rabbitmq_api_endpoint
14
+ @rabbitmq_api_endpoint || ENV.fetch('RABBITMQ_API_ENDPOINT') { 'http://guest:guest@localhost:15672/' }
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,70 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sneakers
4
+ module Laces
5
+ class QueueManager
6
+ WORKER_TAG_ARGUMENT = 'x-worker-tag'
7
+
8
+ def declare_queue(name:, worker_tag:, routing_key: nil, binding_arguments: [], params: {}, reload: true)
9
+ api_client.declare_queue vhost,
10
+ name,
11
+ params_with_worker_tag(worker_tag, params)
12
+
13
+ api_client.bind_queue(vhost, name, exchange, (routing_key || name), binding_arguments)
14
+
15
+ reload_worker(worker_tag) if reload
16
+ end
17
+
18
+ def grouped_queues
19
+ filtered_queues.group_by { |q| q.arguments[WORKER_TAG_ARGUMENT] }
20
+ end
21
+
22
+ def list_queues
23
+ api_client.list_queues(vhost)
24
+ rescue Faraday::ResourceNotFound
25
+ Sneakers.logger.error("Vhost '#{vhost}' does not exist.")
26
+ []
27
+ end
28
+
29
+ def delete_queue(name:, worker_tag:, reload: true)
30
+ api_client.delete_queue vhost, name
31
+
32
+ reload_worker(worker_tag) if reload
33
+ rescue Faraday::ResourceNotFound
34
+ false
35
+ end
36
+
37
+ def queue_info(name:)
38
+ api_client.queue_info vhost, name
39
+ end
40
+
41
+ private
42
+
43
+ def reload_worker(worker_tag)
44
+ Sneakers::Laces.reload(worker_tag: worker_tag)
45
+ end
46
+
47
+ def filtered_queues
48
+ list_queues.select { |q| q.arguments[WORKER_TAG_ARGUMENT].present? }
49
+ end
50
+
51
+ def params_with_worker_tag(worker_tag, params = {})
52
+ Sneakers::CONFIG.fetch(:queue_options).merge(params.deep_symbolize_keys).tap do |p|
53
+ p[:arguments][WORKER_TAG_ARGUMENT] = worker_tag
54
+ end
55
+ end
56
+
57
+ def api_client
58
+ Sneakers::Laces.api_client
59
+ end
60
+
61
+ def vhost
62
+ Sneakers::Laces.config.vhost
63
+ end
64
+
65
+ def exchange
66
+ Sneakers::CONFIG.fetch(:exchange)
67
+ end
68
+ end
69
+ end
70
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sneakers
4
+ module Laces
5
+ class ReloadWorker
6
+ DEFAULT_OPTS = {
7
+ threads: 1,
8
+ queue_options: {
9
+ exclusive: true,
10
+ auto_delete: true,
11
+ durable: false
12
+ }
13
+ }.freeze
14
+
15
+ class << self
16
+ def configure(worker_tags:)
17
+ routing_keys = worker_tags.map { |tag| routing_key(worker_tag: tag) }
18
+
19
+ return if routing_keys.blank?
20
+
21
+ include Sneakers::Worker
22
+
23
+ # empty queue name makes consumer use dynamic one
24
+ from_queue '', DEFAULT_OPTS.merge(routing_key: routing_keys)
25
+ end
26
+
27
+ def routing_key(worker_tag:)
28
+ ['reload', worker_tag].join(':')
29
+ end
30
+ end
31
+
32
+ def work(_msg)
33
+ # USR1 signal forces serverengine to gracefully restart child processes
34
+ # https://github.com/treasure-data/serverengine#live-restart
35
+ Process.kill('USR1', Process.pid)
36
+ ensure
37
+ ack!
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'sneakers/runner'
4
+
5
+ module Sneakers
6
+ module Laces
7
+ class Runner
8
+ def run
9
+ Sneakers::Runner.new(runner_config).run
10
+ end
11
+
12
+ def runner_config
13
+ -> { WorkerManager.new.workers }
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sneakers
4
+ module Laces
5
+ VERSION = '0.1.1'
6
+ end
7
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sneakers
4
+ module Laces
5
+ module Worker
6
+ Classes = []
7
+
8
+ def self.included(base)
9
+ base.extend ClassMethods
10
+ Classes << base if base.is_a? Class
11
+ end
12
+
13
+ module ClassMethods
14
+ attr_reader :worker_opts
15
+ attr_reader :worker_tag
16
+
17
+ def from_tag(tag, opts = { routing_key: [] })
18
+ @worker_tag = tag.to_s
19
+ @worker_opts = opts
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Sneakers
4
+ module Laces
5
+ class WorkerManager
6
+ cattr_accessor :workers_by_queues do
7
+ {}
8
+ end
9
+
10
+ def initialize(worker_classes = [])
11
+ @worker_classes = worker_classes
12
+ configure_reload_worker
13
+ end
14
+
15
+ def workers
16
+ workers = [ReloadWorker]
17
+
18
+ worker_classes.map do |worker_class|
19
+ grouped_queues.fetch(worker_class.worker_tag, []).map do |queue|
20
+ workers << workers_by_queues[queue.name] ||= build_worker_class(worker_class, queue)
21
+ end
22
+ end
23
+
24
+ workers
25
+ end
26
+
27
+ private
28
+
29
+ def configure_reload_worker
30
+ ReloadWorker.configure(worker_tags: worker_classes.map(&:worker_tag))
31
+ end
32
+
33
+ def worker_classes
34
+ @worker_classes.any? ? @worker_classes : Sneakers::Laces::Worker::Classes
35
+ end
36
+
37
+ def build_worker_class(worker_class, queue)
38
+ Class.new(worker_class) do
39
+ include Sneakers::Worker
40
+
41
+ from_queue queue.fetch(:name),
42
+ worker_class.worker_opts.deep_merge(arguments: queue.fetch(:arguments))
43
+ end
44
+ end
45
+
46
+ def grouped_queues
47
+ @grouped_queues ||= QueueManager.new.grouped_queues
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,33 @@
1
+ # frozen_string_literal: true
2
+
3
+ lib = File.expand_path('lib', __dir__)
4
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
5
+ require 'sneakers/laces/version'
6
+
7
+ Gem::Specification.new do |spec|
8
+ spec.name = 'sneakers-laces'
9
+ spec.version = Sneakers::Laces::VERSION
10
+ spec.authors = ['Rustam Sharshenov']
11
+ spec.email = ['rustam@sharshenov.com']
12
+
13
+ spec.summary = 'Configure your Sneakers worker on-the-fly'
14
+ spec.description = 'Subscribe your Sneakers workers to queues by name regexes without process restart'
15
+ spec.homepage = 'https://github.com/sharshenov/sneakers-laces'
16
+ spec.license = 'MIT'
17
+
18
+ # Specify which files should be added to the gem when it is released.
19
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
20
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
21
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
22
+ end
23
+
24
+ spec.require_paths = ['lib']
25
+
26
+ spec.add_dependency 'activesupport'
27
+ spec.add_dependency 'rabbitmq_http_api_client'
28
+ spec.add_dependency 'sneakers'
29
+
30
+ spec.add_development_dependency 'rspec'
31
+ spec.add_development_dependency 'rubocop'
32
+ spec.add_development_dependency 'rubocop-rspec'
33
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sneakers-laces
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Rustam Sharshenov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2018-09-22 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
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: rabbitmq_http_api_client
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: sneakers
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
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: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
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: rubocop-rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Subscribe your Sneakers workers to queues by name regexes without process
98
+ restart
99
+ email:
100
+ - rustam@sharshenov.com
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - lib/sneakers/laces.rb
114
+ - lib/sneakers/laces/configuration.rb
115
+ - lib/sneakers/laces/queue_manager.rb
116
+ - lib/sneakers/laces/reload_worker.rb
117
+ - lib/sneakers/laces/runner.rb
118
+ - lib/sneakers/laces/version.rb
119
+ - lib/sneakers/laces/worker.rb
120
+ - lib/sneakers/laces/worker_manager.rb
121
+ - sneakers-laces.gemspec
122
+ homepage: https://github.com/sharshenov/sneakers-laces
123
+ licenses:
124
+ - MIT
125
+ metadata: {}
126
+ post_install_message:
127
+ rdoc_options: []
128
+ require_paths:
129
+ - lib
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ version: '0'
135
+ required_rubygems_version: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ version: '0'
140
+ requirements: []
141
+ rubyforge_project:
142
+ rubygems_version: 2.7.7
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Configure your Sneakers worker on-the-fly
146
+ test_files: []