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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f1bd582f2fffa08dd029c8d11c77552868322cc8f71bd92f18880966362d70ab
4
- data.tar.gz: 0deb992d109d947d598de595b50e3e1f09fcee10e178ac6921ef3d1761aaec9c
3
+ metadata.gz: d269f2a6ff5d6ce6614c5d54ddff5b23a5c007d84e8a4ec4b2a57cda302bcf4a
4
+ data.tar.gz: b5f71fbdcf2865d0680f10206654a5cd34d1532eed991a422986bd71ecdbc4d1
5
5
  SHA512:
6
- metadata.gz: 54f94fdc1deee3e4c2a92eea73ddf8e406989210f95989e19ced9f3f95101d3265fd310a5ebc882cefdc89abcadca079e0a6b5e0a60c5a0d0d5d051100fd76f3
7
- data.tar.gz: ebc2e7d9c63fb256b2c7572c60fc284a63016e96327ae93c52a7f45447c761d5c714d8fef1fa0752e7b44ba2f763dc58c4d162ccf2e7980d4622a1a55eb62693
6
+ metadata.gz: ce032ca8e828afc6ad7a7ad787c0398a34fe390d9ba7b199bfd7814bac3ca3c8c03ca11e53b7f708c531d88ce75019111582103b2793be2c846f658c7c26d51a
7
+ data.tar.gz: 646da7438886bf68ca7b41b4e30bdca329a1c42198f231ef6643da2a05a08191ea34eead2abf34275f08035153c67547ac80d7f9069dcc29ccf3210355467471
data/Gemfile CHANGED
@@ -1,6 +1,8 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
2
 
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
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
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -1,7 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
2
3
 
3
- require "bundler/setup"
4
- require "totoro"
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 "irb"
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 = Totoro::Config.get_worker worker_name
19
+ worker = queue_class.get_worker worker_name
17
20
  worker_queue = worker.class::QUEUE
18
- Totoro::Queue.subscribe(worker_queue) do |delivery_info, metadata, payload|
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
- Totoro::Queue.channel.work_pool.join
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 "Generate totoro config file"
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
- connect:
3
- host: rabbitmq
4
- port: 5672
5
- user: app
6
- pass: app
7
-
8
- queue:
9
- # example_queue:
10
- # name: real.queue.name
11
- # durable: true
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 "Generate totoro worker file"
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', File.join('app/models/worker', "#{name.underscore}.rb")
12
+ template 'worker.rb',
13
+ File.join('app/models/worker', "#{name.underscore}.rb")
11
14
  end
12
15
  end
13
16
  end
@@ -1,4 +1,6 @@
1
- require "totoro/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'totoro/version'
2
4
  require 'totoro/config'
3
5
  require 'totoro/queue'
4
6
 
@@ -1,8 +1,14 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Totoro
2
- class Queue
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(Totoro::Config.connect).tap(&:start)
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(*Totoro::Config.queue(id))
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(*Totoro::Config.queue(id))
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
@@ -1,27 +1,28 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Totoro
2
4
  class Config
3
- class <<self
4
- def data
5
- @data ||= Rails.application.config_for(:totoro).with_indifferent_access
6
- end
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
- def reset_data
9
- @data = nil
10
- end
10
+ def reset_data
11
+ @data = nil
12
+ end
11
13
 
12
- def connect
13
- data[:connect]
14
- end
14
+ def connect
15
+ @data[:connect]
16
+ end
15
17
 
16
- def queue(id)
17
- name = data[:queue][id][:name]
18
- settings = { durable: data[:queue][id][:durable] }
19
- [name, settings]
20
- end
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
- def get_worker(worker_class)
23
- ::Worker.const_get(worker_class.to_s.camelize).new
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
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Totoro
2
- VERSION = "0.1.3"
4
+ VERSION = '0.1.4'
3
5
  end
Binary file
@@ -1,9 +1,9 @@
1
- require "bundler/setup"
2
- require "totoro"
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 = ".rspec_status"
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!
@@ -1,9 +1,9 @@
1
1
  RSpec.describe Totoro do
2
- it "has a version number" do
2
+ it 'has a version number' do
3
3
  expect(Totoro::VERSION).not_to be nil
4
4
  end
5
5
 
6
- it "does something useful" do
6
+ it 'does something useful' do
7
7
  expect(false).to eq(true)
8
8
  end
9
9
  end
@@ -1,24 +1,28 @@
1
1
 
2
- lib = File.expand_path("../lib", __FILE__)
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 "totoro/version"
6
+ require 'totoro/version'
5
7
 
6
8
  Gem::Specification.new do |spec|
7
- spec.name = "totoro"
9
+ spec.name = 'totoro'
8
10
  spec.version = Totoro::VERSION
9
- spec.authors = ["ShuHui18"]
10
- spec.email = ["shu.hui@blockchaintech.net.au"]
11
+ spec.authors = ['ShuHui18']
12
+ spec.email = ['shu.hui@blockchaintech.net.au']
11
13
 
12
- spec.summary = %q{tool for rabbitmq}
13
- spec.description = %q{Enpower rabbitmq to make it configrable}
14
- spec.homepage = "https://github.com/blockchaintech-au/totoro"
15
- spec.license = "MIT"
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 = ["totoro"]
19
- spec.require_paths = ["lib"]
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 "bundler", "~> 1.16"
22
- spec.add_development_dependency "rake", "~> 10.0"
23
- spec.add_development_dependency "rspec", "~> 3.0"
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.3
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-04-30 00:00:00.000000000 Z
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/queue.rb
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