totoro 0.1.5 → 0.1.6
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +53 -6
- data/bin/totoro +2 -14
- data/docker-compose.yml +1 -1
- data/lib/generators/totoro/templates/worker.rb.erb +16 -0
- data/lib/generators/totoro/worker_generator.rb +2 -1
- data/lib/totoro.rb +1 -0
- data/lib/totoro/base_queue.rb +2 -0
- data/lib/totoro/base_worker.rb +43 -0
- data/lib/totoro/config.rb +1 -1
- data/lib/totoro/version.rb +1 -1
- data/pkg/totoro-0.1.5.gem +0 -0
- data/test/totoro_test/Gemfile +0 -1
- data/test/totoro_test/Gemfile.lock +1 -2
- metadata +4 -4
- data/lib/generators/totoro/templates/worker.rb +0 -10
- data/test/totoro_test/config/initializers/totoro.rb +0 -1
- data/test/totoro_test/config/totoro.yml +0 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30ebcadd2cabd53770744a87e313fdf0a2cddb74ab88b4942ab3e7edeb2a7195
|
4
|
+
data.tar.gz: 262090ee10a54d07f003d4af0778e7e04852a879c3e24f154cb1d6dc5fc3cdcb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e09eabec608b85ce18e16540802a2f8a588e75771434e86b365a843e232c3cbccbe7daa0ebc43f982cc5c8ed2f9d6016c60cd831b774caeb469f8ef8408f9fd1
|
7
|
+
data.tar.gz: c052a7fec121be2a8afe402ccdbf02133e25b616cf68f7180ae2d643bf6116212bbfc7f3fca1bc0f663502d595d8894e2cfc0aafbc0b4786a3215cbedf7ec053
|
data/Gemfile.lock
CHANGED
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
|
-
|
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
|
-
####
|
18
|
+
#### Initialize Totoro for Rails app
|
19
19
|
```
|
20
|
-
rails g totoro:
|
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
|
-
|
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
|
-
|
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
|
-
|
28
|
-
queue_class.channel.work_pool.join
|
16
|
+
::Worker.const_get(worker_class.to_s.camelize).new.execute
|
data/docker-compose.yml
CHANGED
@@ -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
data/lib/totoro/base_queue.rb
CHANGED
@@ -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
data/lib/totoro/version.rb
CHANGED
Binary file
|
data/test/totoro_test/Gemfile
CHANGED
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.
|
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 +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
|