smart-que 0.1.0 → 0.1.1
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 +4 -4
- data/README.md +80 -2
- data/lib/smart_que.rb +1 -0
- data/lib/smart_que/consumer.rb +20 -0
- data/lib/smart_que/consumers/base.rb +62 -0
- data/lib/smart_que/publishers/base.rb +4 -0
- data/lib/smart_que/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 529f52a47cda3ae2921d73514be3ab68dc162480
|
4
|
+
data.tar.gz: 6bf9acfd151c73aec7e1fb52036f096fe2b3036e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 773828ad29daa876e46611d6be154f6c21f561a7a4849bac9f58305e5b6535032d6854c2a1e540927486734b6a56687dc7914f4cd3c86762da9b9b763f43a4ed
|
7
|
+
data.tar.gz: 8be6f714be2d6344d7ea79861c62ab224dd95b2cd1fe7cf4a34c4501c0d39663baadc0aa59f479a3aad8ddf919a84c8408375743e30fc5b62173785912c3f2cb
|
data/README.md
CHANGED
@@ -24,13 +24,91 @@ Or install it yourself as:
|
|
24
24
|
TODO: Write usage instructions here
|
25
25
|
|
26
26
|
1. Setup [RabbitMq](https://www.rabbitmq.com/#getstarted)
|
27
|
+
2. Add `smart-que` gem to your Gemfile and perform bundle install
|
28
|
+
3. Create Publisher/Consumer classes & start publish/consume messages
|
29
|
+
|
30
|
+
## SmartQue Publisher
|
31
|
+
|
32
|
+
SmartQue publisher is used to publish messages to the previously setup RabbitMq
|
33
|
+
server. All messages are converted to JSON format before publishing to the queue.
|
34
|
+
RabbitMq server details and queue lists can be configured as follows
|
35
|
+
|
36
|
+
```
|
37
|
+
file: config/initializers/smart_que.rb
|
38
|
+
|
39
|
+
SmartQue.configure do |f|
|
40
|
+
f.host = ENV[:rabbit_mq][:host']
|
41
|
+
f.port = ENV[:rabbit_mq][:port']
|
42
|
+
f.username = ENV[:rabbit_mq][:username']
|
43
|
+
f.password = ENV[:rabbit_mq][:password']
|
44
|
+
f.logger = Rails.logger
|
45
|
+
f.queues = [
|
46
|
+
'default',
|
47
|
+
'sms_otp',
|
48
|
+
'fcm_push'
|
49
|
+
]
|
50
|
+
end
|
51
|
+
|
52
|
+
$publisher = SmartQue::Publisher.new
|
53
|
+
|
54
|
+
```
|
55
|
+
|
56
|
+
After initializing SmartQue publisher, it can be accessed anywhere in the rails application
|
57
|
+
to publish messages to the RabbitMq server.
|
58
|
+
|
59
|
+
```
|
60
|
+
$publisher.publish('sms_otp', { number: '+919898123123', message: 'Test Message' })
|
61
|
+
```
|
62
|
+
|
63
|
+
## SmartQue Consumer
|
64
|
+
|
65
|
+
The consumer class should be inherited from SmartQue::Consumer, and each consumer will be
|
66
|
+
listening to a defined Queue. The queue name and `run` method should be defined properly
|
67
|
+
in each consumer class.
|
68
|
+
|
69
|
+
```
|
70
|
+
Class OtpSmsConsumer < SmartQue::Consumer
|
71
|
+
QUEUE_NAME = 'sms.otp'
|
72
|
+
|
73
|
+
def run(payload)
|
74
|
+
# Payload: { number: '+919898123123', message: 'Test Message' }
|
75
|
+
# Method implementation goes here
|
76
|
+
end
|
77
|
+
end
|
78
|
+
```
|
79
|
+
|
80
|
+
Note that, queue name words are separated by `.` while defining it with consumer class.
|
81
|
+
Consumer can be started by calling `start` method available for consumer instance as follows
|
82
|
+
|
83
|
+
```
|
84
|
+
c = OtpSmsConsumer.new
|
85
|
+
c.start
|
86
|
+
```
|
87
|
+
|
88
|
+
All consumer processes can be placed in a rake file, so that it can be started individually
|
89
|
+
with rake tasks.
|
90
|
+
|
91
|
+
```
|
92
|
+
File : lib/tasks/consumer.rake
|
93
|
+
|
94
|
+
# Tasks which related to rabbitmq broker.
|
95
|
+
namespace :rabbitmq do
|
96
|
+
|
97
|
+
desc "Run OTP sms worker"
|
98
|
+
task :send_otp_sms => [:environment] do
|
99
|
+
c = OtpSmsConsumer.new
|
100
|
+
c.start
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Task can be initiated by rake
|
105
|
+
RAILS_ENV=staging bundle exec rake rabbitmq:send_otp_sms
|
106
|
+
```
|
27
107
|
|
28
108
|
## Development
|
29
109
|
|
30
110
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
31
111
|
|
32
|
-
Under Development !!!
|
33
|
-
|
34
112
|
## Contributing
|
35
113
|
|
36
114
|
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/smart_que. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
data/lib/smart_que.rb
CHANGED
@@ -0,0 +1,20 @@
|
|
1
|
+
require_relative "consumers/base"
|
2
|
+
|
3
|
+
module SmartQue
|
4
|
+
class Consumer < Consumers::Base
|
5
|
+
|
6
|
+
# Initialize
|
7
|
+
def initialize(queue_name = nil)
|
8
|
+
@queue_name = ( queue_name || self.class::QUEUE_NAME )
|
9
|
+
end
|
10
|
+
|
11
|
+
# Instance methods
|
12
|
+
|
13
|
+
# Consume message and perform tasks
|
14
|
+
def run(payload)
|
15
|
+
# Implement logic in the corresponding consumer
|
16
|
+
Rails.logger.info "Not Implemented, Please define run method for the consumer class."
|
17
|
+
:ok
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module Consumers
|
2
|
+
class Base
|
3
|
+
# Methods which are related to implement a base consumer.
|
4
|
+
|
5
|
+
# The queue name should be defined here.
|
6
|
+
QUEUE_NAME = 'smart_que.default'
|
7
|
+
|
8
|
+
attr_accessor :queue_name
|
9
|
+
|
10
|
+
def queue_name
|
11
|
+
@queue_name
|
12
|
+
end
|
13
|
+
|
14
|
+
# This method will return the default queue which present
|
15
|
+
# in the message queues. Consumer specific queue should be
|
16
|
+
# defined and implemented in the consumer sub classes.
|
17
|
+
def queue
|
18
|
+
@queue ||= channel.queue(queue_name)
|
19
|
+
end
|
20
|
+
|
21
|
+
# Establish connection to Message Queues.
|
22
|
+
def connection
|
23
|
+
::SmartQue.establish_connection
|
24
|
+
end
|
25
|
+
|
26
|
+
# Create channel with the established connection.
|
27
|
+
def channel
|
28
|
+
@channel ||= connection.create_channel
|
29
|
+
end
|
30
|
+
|
31
|
+
def config
|
32
|
+
::SmartQue.config
|
33
|
+
end
|
34
|
+
|
35
|
+
# Method which kick start the consumer process thread
|
36
|
+
def start
|
37
|
+
channel.prefetch(10)
|
38
|
+
queue.subscribe(manual_ack: true, exclusive: false) do |delivery_info, metadata, payload|
|
39
|
+
begin
|
40
|
+
body = JSON.parse(payload).with_indifferent_access
|
41
|
+
status = run(body)
|
42
|
+
rescue => e
|
43
|
+
status = :error
|
44
|
+
end
|
45
|
+
|
46
|
+
if status == :ok
|
47
|
+
channel.ack(delivery_info.delivery_tag)
|
48
|
+
elsif status == :retry
|
49
|
+
channel.reject(delivery_info.delivery_tag, true)
|
50
|
+
else # :error, nil etc
|
51
|
+
channel.reject(delivery_info.delivery_tag, false)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
wait_for_threads
|
56
|
+
end
|
57
|
+
|
58
|
+
def wait_for_threads
|
59
|
+
sleep
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
data/lib/smart_que/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: smart-que
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ashik Salman
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-02-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -71,6 +71,8 @@ files:
|
|
71
71
|
- bin/setup
|
72
72
|
- lib/smart_que.rb
|
73
73
|
- lib/smart_que/config.rb
|
74
|
+
- lib/smart_que/consumer.rb
|
75
|
+
- lib/smart_que/consumers/base.rb
|
74
76
|
- lib/smart_que/errors.rb
|
75
77
|
- lib/smart_que/publisher.rb
|
76
78
|
- lib/smart_que/publishers/base.rb
|