totoro 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +4 -2
- data/Rakefile +5 -3
- data/bin/console +4 -3
- data/bin/totoro +6 -3
- data/lib/generators/totoro/config_generator.rb +4 -2
- data/lib/generators/totoro/templates/totoro.yml +21 -10
- data/lib/generators/totoro/worker_generator.rb +6 -3
- data/lib/totoro.rb +3 -1
- data/lib/totoro/{queue.rb → base_queue.rb} +14 -4
- data/lib/totoro/config.rb +19 -18
- data/lib/totoro/initializer.rb +39 -0
- data/lib/totoro/version.rb +3 -1
- data/pkg/totoro-0.1.3.gem +0 -0
- data/spec/spec_helper.rb +3 -3
- data/spec/totoro_spec.rb +2 -2
- data/totoro.gemspec +18 -14
- metadata +18 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d269f2a6ff5d6ce6614c5d54ddff5b23a5c007d84e8a4ec4b2a57cda302bcf4a
|
4
|
+
data.tar.gz: b5f71fbdcf2865d0680f10206654a5cd34d1532eed991a422986bd71ecdbc4d1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ce032ca8e828afc6ad7a7ad787c0398a34fe390d9ba7b199bfd7814bac3ca3c8c03ca11e53b7f708c531d88ce75019111582103b2793be2c846f658c7c26d51a
|
7
|
+
data.tar.gz: 646da7438886bf68ca7b41b4e30bdca329a1c42198f231ef6643da2a05a08191ea34eead2abf34275f08035153c67547ac80d7f9069dcc29ccf3210355467471
|
data/Gemfile
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
|
1
|
+
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
3
|
+
source 'https://rubygems.org'
|
4
|
+
|
5
|
+
git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
|
4
6
|
|
5
7
|
# Specify your gem's dependencies in totoro.gemspec
|
6
8
|
gemspec
|
data/Rakefile
CHANGED
data/bin/console
CHANGED
@@ -1,7 +1,8 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
|
-
require
|
4
|
-
require
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'totoro'
|
5
6
|
|
6
7
|
# You can add fixtures and/or initialization code here to make experimenting
|
7
8
|
# with your gem easier. You can also use a different console, if you like.
|
@@ -10,5 +11,5 @@ require "totoro"
|
|
10
11
|
# require "pry"
|
11
12
|
# Pry.start
|
12
13
|
|
13
|
-
require
|
14
|
+
require 'irb'
|
14
15
|
IRB.start(__FILE__)
|
data/bin/totoro
CHANGED
@@ -1,4 +1,6 @@
|
|
1
1
|
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
2
4
|
require 'totoro'
|
3
5
|
# You might want to change this
|
4
6
|
ENV['RAILS_ENV'] ||= 'development'
|
@@ -12,14 +14,15 @@ Rails.logger = logger = Logger.new STDOUT
|
|
12
14
|
logger.level = Logger.const_get(Rails.configuration.log_level.to_s.upcase)
|
13
15
|
|
14
16
|
worker_name = ARGV[0]
|
17
|
+
queue_class = Totoro::Queue
|
15
18
|
logger.info 'Start to subscribe to Rabbitmq'
|
16
|
-
worker =
|
19
|
+
worker = queue_class.get_worker worker_name
|
17
20
|
worker_queue = worker.class::QUEUE
|
18
|
-
|
21
|
+
queue_class.subscribe(worker_queue) do |delivery_info, metadata, payload|
|
19
22
|
logger.info "#{worker_queue} Received: #{payload}"
|
20
23
|
payload_hash = JSON.parse(payload).with_indifferent_access
|
21
24
|
worker.process payload_hash, metadata, delivery_info
|
22
25
|
end
|
23
26
|
|
24
27
|
logger.info 'Listening to the Rabbitmq'
|
25
|
-
|
28
|
+
queue_class.channel.work_pool.join
|
@@ -1,8 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Totoro
|
2
4
|
class ConfigGenerator < Rails::Generators::Base
|
3
5
|
source_root File.expand_path('../templates', __FILE__)
|
4
|
-
desc
|
5
|
-
|
6
|
+
desc 'Generate totoro config file'
|
7
|
+
|
6
8
|
def copy_config_file
|
7
9
|
template 'totoro.yml', File.join('config', 'totoro.yml')
|
8
10
|
end
|
@@ -1,14 +1,25 @@
|
|
1
1
|
default: &default
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
2
|
+
default:
|
3
|
+
connect:
|
4
|
+
host: rabbitmq
|
5
|
+
port: 5672
|
6
|
+
user: app
|
7
|
+
pass: app
|
8
|
+
queue:
|
9
|
+
# example_queue:
|
10
|
+
# name: real.queue.name
|
11
|
+
# durable: true
|
12
|
+
|
13
|
+
custom:
|
14
|
+
connect:
|
15
|
+
host: rabbitmq2
|
16
|
+
port: 5672
|
17
|
+
user: app
|
18
|
+
pass: app
|
19
|
+
queue:
|
20
|
+
# example_queue:
|
21
|
+
# name: real.queue.name
|
22
|
+
# durable: true
|
12
23
|
|
13
24
|
|
14
25
|
development:
|
@@ -1,13 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Totoro
|
2
4
|
class WorkerGenerator < Rails::Generators::Base
|
3
|
-
desc
|
5
|
+
desc 'Generate totoro worker file'
|
4
6
|
|
5
7
|
source_root File.expand_path('../templates', __FILE__)
|
6
8
|
argument :name, type: :string
|
7
9
|
argument :queue, type: :string, required: false
|
8
|
-
|
10
|
+
|
9
11
|
def copy_config_file
|
10
|
-
template 'worker.rb',
|
12
|
+
template 'worker.rb',
|
13
|
+
File.join('app/models/worker', "#{name.underscore}.rb")
|
11
14
|
end
|
12
15
|
end
|
13
16
|
end
|
data/lib/totoro.rb
CHANGED
@@ -1,8 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Totoro
|
2
|
-
class
|
4
|
+
class BaseQueue
|
3
5
|
class <<self
|
6
|
+
def config
|
7
|
+
@config ||= Totoro::Config.new
|
8
|
+
end
|
9
|
+
|
4
10
|
def connection
|
5
|
-
@connection ||= Bunny.new(
|
11
|
+
@connection ||= Bunny.new(config.connect).tap(&:start)
|
6
12
|
end
|
7
13
|
|
8
14
|
def channel
|
@@ -15,17 +21,21 @@ module Totoro
|
|
15
21
|
|
16
22
|
# enqueue = publish to direct exchange
|
17
23
|
def enqueue(id, payload)
|
18
|
-
queue = channel.queue(*
|
24
|
+
queue = channel.queue(*config.queue(id))
|
19
25
|
payload = JSON.dump payload
|
20
26
|
exchange.publish(payload, routing_key: queue.name)
|
21
27
|
end
|
22
28
|
|
23
29
|
def subscribe(id)
|
24
|
-
queue = channel.queue(*
|
30
|
+
queue = channel.queue(*config.queue(id))
|
25
31
|
queue.subscribe do |delivery_info, metadata, payload|
|
26
32
|
yield(delivery_info, metadata, payload)
|
27
33
|
end
|
28
34
|
end
|
35
|
+
|
36
|
+
def get_worker(worker_class)
|
37
|
+
::Worker.const_get(worker_class.to_s.camelize).new
|
38
|
+
end
|
29
39
|
end
|
30
40
|
end
|
31
41
|
end
|
data/lib/totoro/config.rb
CHANGED
@@ -1,27 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
module Totoro
|
2
4
|
class Config
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
5
|
+
def initialize(prefix)
|
6
|
+
@data = Rails.application.config_for(:totoro).with_indifferent_access
|
7
|
+
@data = @data[prefix] if prefix.present?
|
8
|
+
end
|
7
9
|
|
8
|
-
|
9
|
-
|
10
|
-
|
10
|
+
def reset_data
|
11
|
+
@data = nil
|
12
|
+
end
|
11
13
|
|
12
|
-
|
13
|
-
|
14
|
-
|
14
|
+
def connect
|
15
|
+
@data[:connect]
|
16
|
+
end
|
15
17
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
def queue(id)
|
19
|
+
name = data[:queue][id][:name]
|
20
|
+
settings = { durable: data[:queue][id][:durable] }
|
21
|
+
[name, settings]
|
22
|
+
end
|
21
23
|
|
22
|
-
|
23
|
-
|
24
|
-
end
|
24
|
+
def get_worker(worker_class)
|
25
|
+
::Worker.const_get(worker_class.to_s.camelize).new
|
25
26
|
end
|
26
27
|
end
|
27
28
|
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Totoro
|
4
|
+
class Initializer
|
5
|
+
DEFAULT_CONFIG = %i[default connect queue].freeze
|
6
|
+
def excute
|
7
|
+
config = Rails.application.config_for(:totoro).with_indifferent_access
|
8
|
+
# set default queue class
|
9
|
+
Totoro.const_set('Queue', default_queue_class(config))
|
10
|
+
# set custom queue class
|
11
|
+
(config.symbolize_keys.keys - DEFAULT_CONFIG).each do |prefix|
|
12
|
+
prefix_module(prefix, queue_class(prefix))
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def default_queue_class(config)
|
19
|
+
if config.key?(:default)
|
20
|
+
queue_class(:default)
|
21
|
+
else
|
22
|
+
Class.new(Totoro::BaseQueue)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def queue_class(prefix)
|
27
|
+
custom_queue_class = Class.new(Totoro::BaseQueue)
|
28
|
+
custom_queue_class.define_singleton_method('config') do
|
29
|
+
@config ||= Totoro::Config.new(prefix)
|
30
|
+
end
|
31
|
+
custom_queue_class
|
32
|
+
end
|
33
|
+
|
34
|
+
def prefix_module(prefix, custom_queue_class)
|
35
|
+
prefix_module = Totoro.const_set(prefix.camelize, Module.new)
|
36
|
+
prefix_module.const_set('Queue', custom_queue_class)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
data/lib/totoro/version.rb
CHANGED
data/pkg/totoro-0.1.3.gem
CHANGED
Binary file
|
data/spec/spec_helper.rb
CHANGED
@@ -1,9 +1,9 @@
|
|
1
|
-
require
|
2
|
-
require
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'totoro'
|
3
3
|
|
4
4
|
RSpec.configure do |config|
|
5
5
|
# Enable flags like --only-failures and --next-failure
|
6
|
-
config.example_status_persistence_file_path =
|
6
|
+
config.example_status_persistence_file_path = '.rspec_status'
|
7
7
|
|
8
8
|
# Disable RSpec exposing methods globally on `Module` and `main`
|
9
9
|
config.disable_monkey_patching!
|
data/spec/totoro_spec.rb
CHANGED
data/totoro.gemspec
CHANGED
@@ -1,24 +1,28 @@
|
|
1
1
|
|
2
|
-
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
5
|
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
-
require
|
6
|
+
require 'totoro/version'
|
5
7
|
|
6
8
|
Gem::Specification.new do |spec|
|
7
|
-
spec.name =
|
9
|
+
spec.name = 'totoro'
|
8
10
|
spec.version = Totoro::VERSION
|
9
|
-
spec.authors = [
|
10
|
-
spec.email = [
|
11
|
+
spec.authors = ['ShuHui18']
|
12
|
+
spec.email = ['shu.hui@blockchaintech.net.au']
|
11
13
|
|
12
|
-
spec.summary =
|
13
|
-
spec.description =
|
14
|
-
spec.homepage =
|
15
|
-
spec.license =
|
14
|
+
spec.summary = 'tool for rabbitmq'
|
15
|
+
spec.description = 'Enpower rabbitmq to make it configrable'
|
16
|
+
spec.homepage = 'https://github.com/blockchaintech-au/totoro'
|
17
|
+
spec.license = 'MIT'
|
16
18
|
|
17
19
|
spec.files = Dir['**/*'].keep_if { |file| File.file?(file) }
|
18
|
-
spec.executables = [
|
19
|
-
spec.require_paths = [
|
20
|
+
spec.executables = ['totoro']
|
21
|
+
spec.require_paths = ['lib']
|
22
|
+
|
23
|
+
spec.add_runtime_dependency 'bunny', '~> 2.10'
|
20
24
|
|
21
|
-
spec.add_development_dependency
|
22
|
-
spec.add_development_dependency
|
23
|
-
spec.add_development_dependency
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
24
28
|
end
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: totoro
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ShuHui18
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-06-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bunny
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.10'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.10'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: bundler
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -73,8 +87,9 @@ files:
|
|
73
87
|
- lib/generators/totoro/templates/worker.rb
|
74
88
|
- lib/generators/totoro/worker_generator.rb
|
75
89
|
- lib/totoro.rb
|
90
|
+
- lib/totoro/base_queue.rb
|
76
91
|
- lib/totoro/config.rb
|
77
|
-
- lib/totoro/
|
92
|
+
- lib/totoro/initializer.rb
|
78
93
|
- lib/totoro/version.rb
|
79
94
|
- pkg/totoro-0.1.1.gem
|
80
95
|
- pkg/totoro-0.1.2.gem
|