totoro 0.1.5 → 0.1.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c0ce1f9b6e844375ba38c9bbb6cdd3f0f844854e076e2f67844d33f8c13ec73f
4
- data.tar.gz: 297c15584c20d54012e67e1fa8fd564c969a76109ba9479ddb260508426a6f3f
3
+ metadata.gz: 30ebcadd2cabd53770744a87e313fdf0a2cddb74ab88b4942ab3e7edeb2a7195
4
+ data.tar.gz: 262090ee10a54d07f003d4af0778e7e04852a879c3e24f154cb1d6dc5fc3cdcb
5
5
  SHA512:
6
- metadata.gz: f4c25c90c0e08e68af2a67b11035aeddfdfdad66cf291161adcb3734636c3dc76e16ce10b674a90a0d2b05531dcdc335b19e16223db6a8e2c83c7503f3ab3045
7
- data.tar.gz: b7f891ebbba5a1bd1151cb93be5c73794908a54b16df68d3818bf0cab16f82682b0c239455f8541e1c9cd956d7d8ed2d25a076ce43195fd4f04910f063bf5d1c
6
+ metadata.gz: e09eabec608b85ce18e16540802a2f8a588e75771434e86b365a843e232c3cbccbe7daa0ebc43f982cc5c8ed2f9d6016c60cd831b774caeb469f8ef8408f9fd1
7
+ data.tar.gz: c052a7fec121be2a8afe402ccdbf02133e25b616cf68f7180ae2d643bf6116212bbfc7f3fca1bc0f663502d595d8894e2cfc0aafbc0b4786a3215cbedf7ec053
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- totoro (0.1.5)
4
+ totoro (0.1.6)
5
5
  bunny (~> 2.10)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -4,7 +4,7 @@ Totoro is a RabbitMQ util that focuses on samplify queue operation.
4
4
 
5
5
  ## Installation
6
6
 
7
- #### Install gem
7
+ ### Install gem
8
8
  Add this line to your application's Gemfile:
9
9
 
10
10
  ```ruby
@@ -15,14 +15,22 @@ And then execute:
15
15
 
16
16
  $ bundle
17
17
 
18
- #### Generate configuration file
18
+ #### Initialize Totoro for Rails app
19
19
  ```
20
- rails g totoro:config
20
+ rails g totoro:init
21
21
  ```
22
22
 
23
+ This command will generate two files
24
+
25
+ 1. `totoro.yml` (Rabbitmq configuration file)
26
+ 2. `initilizers/totoro.rb` (Rails initializer)
27
+
23
28
  ## Quick Start
24
29
 
30
+ ### Default rabbitmq server
31
+
25
32
  #### Enqueue
33
+
26
34
  ```
27
35
  Totoro::Queue.enqueue('queue', payload)
28
36
  ```
@@ -35,11 +43,16 @@ rails g totoro:wroker <worker_name> <queue_name>
35
43
  after that, add business logic in the process method
36
44
  ```
37
45
  module Worker
38
- class WorkerClass
39
- QUEUE = 'queue_name'
40
- def process(payload, _metadata, _delivery_info)
46
+ class WorkerClass < Totoro::BaseWorker
47
+ def process(payload, metadata, delivery_info)
41
48
  # worker process
42
49
  end
50
+
51
+ private
52
+
53
+ def setup
54
+ @queue_name = <queue_name>
55
+ end
43
56
  end
44
57
  end
45
58
  ```
@@ -48,6 +61,40 @@ finally, run the background deamon
48
61
  bundle exec totoro worker_class
49
62
  ```
50
63
 
64
+ ### Custom rabbitmq server
65
+
66
+ #### Enqueue
67
+
68
+ ```
69
+ Totoro::<ServerName>::Queue.enqueue('queue', payload)
70
+ ```
71
+
72
+ #### Dequeue
73
+ To create a dequeue daemon, first you need to create a worker
74
+ ```
75
+ rails g totoro:wroker <worker_name> <queue_name> <prefix>
76
+ ```
77
+ after that, add business logic in the process method
78
+ ```
79
+ module Worker
80
+ class WorkerClass < Totoro::BaseWorker
81
+ def process(payload, metadata, delivery_info)
82
+ # worker process
83
+ end
84
+
85
+ private
86
+
87
+ def setup
88
+ @prefix = :<prefix>
89
+ @queue_name = <queue_name>
90
+ end
91
+ end
92
+ end
93
+ ```
94
+ finally, run the background deamon
95
+ ```
96
+ bundle exec totoro worker_class
97
+ ```
51
98
 
52
99
  ## Contributing
53
100
 
data/bin/totoro CHANGED
@@ -10,19 +10,7 @@ root = File.dirname(root) until File.exist?(File.join(root, 'config'))
10
10
  Dir.chdir(root)
11
11
 
12
12
  require File.join(root, 'config', 'environment')
13
- Rails.logger = logger = Logger.new STDOUT
14
- logger.level = Logger.const_get(Rails.configuration.log_level.to_s.upcase)
15
13
 
16
- worker_name = ARGV[0]
17
- queue_class = Totoro::Queue
18
- logger.info 'Start to subscribe to Rabbitmq'
19
- worker = queue_class.get_worker worker_name
20
- worker_queue = worker.class::QUEUE
21
- queue_class.subscribe(worker_queue) do |delivery_info, metadata, payload|
22
- logger.info "#{worker_queue} Received: #{payload}"
23
- payload_hash = JSON.parse(payload).with_indifferent_access
24
- worker.process payload_hash, metadata, delivery_info
25
- end
14
+ worker_class = ARGV[0]
26
15
 
27
- logger.info 'Listening to the Rabbitmq'
28
- queue_class.channel.work_pool.join
16
+ ::Worker.const_get(worker_class.to_s.camelize).new.execute
data/docker-compose.yml CHANGED
@@ -25,7 +25,7 @@ services:
25
25
  ports:
26
26
  - '3000:3000'
27
27
  volumes:
28
- - ./test/totoro_test:/app
28
+ # - ./test/totoro_test:/app
29
29
  - .:/totoro
30
30
  command: ./bin/rails c
31
31
  environment:
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Worker
4
+ class <%= name.camelcase %> < Totoro::BaseWorker
5
+ def process(payload, metadata, delivery_info)
6
+ # worker process
7
+ end
8
+
9
+ private
10
+
11
+ def setup<% if prefix.present? %>
12
+ @prefix = :<%= prefix %><% end %>
13
+ @queue_name = '<%= queue || name.underscore %>'
14
+ end
15
+ end
16
+ end
@@ -7,9 +7,10 @@ module Totoro
7
7
  source_root File.expand_path('../templates', __FILE__)
8
8
  argument :name, type: :string
9
9
  argument :queue, type: :string, required: false
10
+ argument :prefix, type: :string, required: false
10
11
 
11
12
  def copy_config_file
12
- template 'worker.rb',
13
+ template 'worker.rb.erb',
13
14
  File.join('app/models/worker', "#{name.underscore}.rb")
14
15
  end
15
16
  end
data/lib/totoro.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'totoro/version'
4
4
  require 'totoro/config'
5
5
  require 'totoro/base_queue'
6
+ require 'totoro/base_worker'
6
7
  require 'totoro/initializer'
7
8
 
8
9
  module Totoro
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'bunny'
4
+
3
5
  module Totoro
4
6
  class BaseQueue
5
7
  class <<self
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Totoro
4
+ class BaseWorker
5
+ def initialize
6
+ @prefix = :default
7
+ Rails.logger = @logger = Logger.new STDOUT
8
+ @logger.level = Logger.const_get(
9
+ Rails.configuration.log_level.to_s.upcase
10
+ )
11
+ setup
12
+ @queue_class = queue_class
13
+ end
14
+
15
+ def execute
16
+ @queue_class.subscribe(@queue_name) do |delivery_info, metadata, payload|
17
+ @logger.info "#{@queue_name} Received: #{payload}"
18
+ payload_hash = JSON.parse(payload).with_indifferent_access
19
+ process(payload_hash, metadata, delivery_info)
20
+ end
21
+ @logger.info 'Listening to the Rabbitmq'
22
+ @queue_class.channel.work_pool.join
23
+ end
24
+
25
+ def process; end
26
+
27
+ private
28
+
29
+ def setup
30
+ raise(Totoro::NeedQueueNameError, 'Need setup @queue_name')
31
+ end
32
+
33
+ def queue_class
34
+ if @prefix == :default
35
+ Totoro::Queue
36
+ else
37
+ "Totoro::#{@prefix.to_s.camelize}::Queue".constantize
38
+ end
39
+ end
40
+ end
41
+
42
+ class NeedQueueNameError < RuntimeError; end
43
+ end
data/lib/totoro/config.rb CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  module Totoro
4
4
  class Config
5
- def initialize(prefix)
5
+ def initialize(prefix = nil)
6
6
  @data = Rails.application.config_for(:totoro).with_indifferent_access
7
7
  @data = @data[prefix] if prefix.present?
8
8
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Totoro
4
- VERSION = '0.1.5'
4
+ VERSION = '0.1.6'
5
5
  end
Binary file
@@ -5,7 +5,6 @@ git_source(:github) do |repo_name|
5
5
  "https://github.com/#{repo_name}.git"
6
6
  end
7
7
 
8
- gem 'bunny'
9
8
  gem 'totoro', path: '/totoro'
10
9
  # Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
11
10
  gem 'rails', '~> 5.1.6'
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: /totoro
3
3
  specs:
4
- totoro (0.1.4)
4
+ totoro (0.1.5)
5
5
  bunny (~> 2.10)
6
6
 
7
7
  GEM
@@ -131,7 +131,6 @@ PLATFORMS
131
131
  ruby
132
132
 
133
133
  DEPENDENCIES
134
- bunny
135
134
  byebug
136
135
  listen (>= 3.0.5, < 3.2)
137
136
  puma (~> 3.7)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: totoro
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - ShuHui18
@@ -89,10 +89,11 @@ files:
89
89
  - lib/generators/totoro/init_generator.rb
90
90
  - lib/generators/totoro/templates/initializer.rb
91
91
  - lib/generators/totoro/templates/totoro.yml
92
- - lib/generators/totoro/templates/worker.rb
92
+ - lib/generators/totoro/templates/worker.rb.erb
93
93
  - lib/generators/totoro/worker_generator.rb
94
94
  - lib/totoro.rb
95
95
  - lib/totoro/base_queue.rb
96
+ - lib/totoro/base_worker.rb
96
97
  - lib/totoro/config.rb
97
98
  - lib/totoro/initializer.rb
98
99
  - lib/totoro/version.rb
@@ -100,6 +101,7 @@ files:
100
101
  - pkg/totoro-0.1.2.gem
101
102
  - pkg/totoro-0.1.3.gem
102
103
  - pkg/totoro-0.1.4.gem
104
+ - pkg/totoro-0.1.5.gem
103
105
  - spec/spec_helper.rb
104
106
  - spec/totoro_spec.rb
105
107
  - test/totoro_test/Gemfile
@@ -135,14 +137,12 @@ files:
135
137
  - test/totoro_test/config/initializers/filter_parameter_logging.rb
136
138
  - test/totoro_test/config/initializers/inflections.rb
137
139
  - test/totoro_test/config/initializers/mime_types.rb
138
- - test/totoro_test/config/initializers/totoro.rb
139
140
  - test/totoro_test/config/initializers/wrap_parameters.rb
140
141
  - test/totoro_test/config/locales/en.yml
141
142
  - test/totoro_test/config/puma.rb
142
143
  - test/totoro_test/config/routes.rb
143
144
  - test/totoro_test/config/secrets.yml
144
145
  - test/totoro_test/config/spring.rb
145
- - test/totoro_test/config/totoro.yml
146
146
  - test/totoro_test/db/seeds.rb
147
147
  - test/totoro_test/log/development.log
148
148
  - test/totoro_test/public/robots.txt
@@ -1,10 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Worker
4
- class <%= name.camelcase %>
5
- QUEUE = '<%= queue || name.underscore %>'.freeze
6
- def process(payload, _metadata, _delivery_info)
7
- # worker process
8
- end
9
- end
10
- end
@@ -1 +0,0 @@
1
- Totoro::Initializer.new.execute
@@ -1,31 +0,0 @@
1
- default: &default
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: rabbitmq
16
- port: 5672
17
- user: app
18
- pass: app
19
- queue:
20
- custom_queue:
21
- name: custom.queue.name
22
- durable: true
23
-
24
- development:
25
- <<: *default
26
-
27
- test:
28
- <<: *default
29
-
30
- production:
31
- <<: *default