ons_on_rails 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +10 -0
- data/.rspec +2 -0
- data/.rubocop.yml +47 -0
- data/.yardopts +2 -0
- data/Gemfile +1 -0
- data/LICENSE.txt +21 -0
- data/README.md +125 -0
- data/Rakefile +25 -0
- data/bin/console +14 -0
- data/bin/rake +17 -0
- data/bin/rspec +17 -0
- data/bin/rubocop +17 -0
- data/bin/setup +8 -0
- data/gemfiles/rails41.gemfile +5 -0
- data/gemfiles/rails42.gemfile +5 -0
- data/gemfiles/rails50.gemfile +5 -0
- data/lib/ons_on_rails/publisher.rb +52 -0
- data/lib/ons_on_rails/publishers/tcp.rb +14 -0
- data/lib/ons_on_rails/publishers/test.rb +12 -0
- data/lib/ons_on_rails/subscriber.rb +53 -0
- data/lib/ons_on_rails/version.rb +3 -0
- data/lib/ons_on_rails.rb +88 -0
- data/ons_on_rails.gemspec +47 -0
- metadata +236 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5500013e0fc7f8f209737d0acce0a689e0678723
|
4
|
+
data.tar.gz: ac62eb44fea0c3d3050fccfe746e408c1e1d3a8f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 23b94cedd8086a7934cd9d07533811c30c72a33d0a7245a13021150f8609f9a5b17eabd02cf6008f59fcaa54915aad83b49a39a0f29ee5143347504f748f591f
|
7
|
+
data.tar.gz: bc3305b4ec327dd2af246564d5743ba8147960a4e719e1f22980107b8397fa3f9e6db6be57ab442a3f23c89a144082f12331a8f68b420a8c60ecf7d1abdf643a
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
require: rubocop-rspec
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
Exclude:
|
5
|
+
- 'spec/dummy/db/**/*'
|
6
|
+
|
7
|
+
Metrics/AbcSize:
|
8
|
+
Enabled: false
|
9
|
+
|
10
|
+
Metrics/CyclomaticComplexity:
|
11
|
+
Enabled: false
|
12
|
+
|
13
|
+
Metrics/ClassLength:
|
14
|
+
Max: 160
|
15
|
+
|
16
|
+
Metrics/LineLength:
|
17
|
+
Max: 160
|
18
|
+
|
19
|
+
Metrics/MethodLength:
|
20
|
+
Max: 40
|
21
|
+
|
22
|
+
Metrics/PerceivedComplexity:
|
23
|
+
Max: 12
|
24
|
+
|
25
|
+
Style/Documentation:
|
26
|
+
Enabled: false
|
27
|
+
|
28
|
+
Style/FrozenStringLiteralComment:
|
29
|
+
Enabled: false
|
30
|
+
|
31
|
+
Style/GlobalVars:
|
32
|
+
AllowedVariables: ['$user_service_publisher']
|
33
|
+
|
34
|
+
Style/ParallelAssignment:
|
35
|
+
Enabled: false
|
36
|
+
|
37
|
+
Style/RedundantReturn:
|
38
|
+
Enabled: false
|
39
|
+
|
40
|
+
RSpec/NamedSubject:
|
41
|
+
Enabled: false
|
42
|
+
|
43
|
+
RSpec/ExampleLength:
|
44
|
+
Max: 16
|
45
|
+
|
46
|
+
RSpec/MultipleExpectations:
|
47
|
+
Max: 8
|
data/.yardopts
ADDED
data/Gemfile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
eval_gemfile File.expand_path('../gemfiles/rails50.gemfile', __FILE__)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Souche Car Service Co., Ltd, HANGZHOU.
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
[![GitHub issues](https://img.shields.io/github/issues/souche/ons_on_rails.svg)](https://github.com/souche/ons_on_rails/issues)
|
2
|
+
[![GitHub forks](https://img.shields.io/github/forks/souche/ons_on_rails.svg)](https://github.com/souche/ons_on_rails/network)
|
3
|
+
[![GitHub stars](https://img.shields.io/github/stars/souche/ons_on_rails.svg)](https://github.com/souche/ons_on_rails/stargazers)
|
4
|
+
[![Yard Docs](http://img.shields.io/badge/yard-docs-blue.svg)](http://www.rubydoc.info/github/souche/ons_on_rails/master)
|
5
|
+
[![Gem Version](http://img.shields.io/gem/v/ons_on_rails.svg)](https://rubygems.org/gems/ons_on_rails)
|
6
|
+
[![License](http://img.shields.io/:license-mit-blue.svg)](https://souche.mit-license.org/)
|
7
|
+
|
8
|
+
|
9
|
+
# OnsOnRails
|
10
|
+
|
11
|
+
整合阿里云 ONS 到 Rails 项目
|
12
|
+
|
13
|
+
## 项目依赖
|
14
|
+
|
15
|
+
* Linux/Unix 系统
|
16
|
+
* Ruby 2.1.5 或以上版本
|
17
|
+
* Rails 4.1.0 或以上版本
|
18
|
+
|
19
|
+
## 如何使用
|
20
|
+
|
21
|
+
### 配置 Rails 环境
|
22
|
+
|
23
|
+
#### 在 Gemfile 添加依赖规则
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
gem 'ons', group: :linux
|
27
|
+
gem 'ons_on_rails'
|
28
|
+
```
|
29
|
+
|
30
|
+
#### 在 config/application.rb 添加 require 规则
|
31
|
+
|
32
|
+
```ruby
|
33
|
+
Bundler.require(*Rails.groups)
|
34
|
+
Bundler.require(RUBY_PLATFORM.match(/(linux|darwin)/)[0].to_sym)
|
35
|
+
```
|
36
|
+
|
37
|
+
#### 在 config/ 目录下添加配置文件 ons\_on\_rails.yml
|
38
|
+
|
39
|
+
```yaml
|
40
|
+
#
|
41
|
+
# access_key,阿里云官网身份验证访问码
|
42
|
+
# secret_key,阿里云身份验证密钥
|
43
|
+
#
|
44
|
+
# user_service_subscriber,消费者名称,需要与实际定义的类名信息保持一致,具体见下文的消费者章节
|
45
|
+
# user_service_subscriber#consumer_id,阿里云 MQ 控制台创建的 Consumer ID
|
46
|
+
# user_service_subscriber#topic,阿里云 MQ 控制台创建的 Topic
|
47
|
+
# user_service_subscriber#tag,当前消费者订阅的 Topic 下所关注的消息标签表达式
|
48
|
+
#
|
49
|
+
# user_service_publisher,生成者名称,具体见下文的生成者章节
|
50
|
+
# user_service_publisher#producer_id,阿里云 MQ 控制台创建的 Producer ID
|
51
|
+
# user_service_publisher#topic,阿里云 MQ 控制台创建的 Topic
|
52
|
+
# user_service_publisher#tag,当前生成者发布的消息所使用的消息标签
|
53
|
+
#
|
54
|
+
default: &default
|
55
|
+
access_key: <%= ENV['ONS_ACCESS_KEY'] %>
|
56
|
+
secret_key: <%= ENV['ONS_SECRET_KEY'] %>
|
57
|
+
user_service_subscriber:
|
58
|
+
consumer_id: <%= ENV['ONS_CONSUMER_ID'] %>
|
59
|
+
topic: <%= ENV['ONS_TOPIC'] %>
|
60
|
+
tag: 'user_service'
|
61
|
+
user_service_publisher:
|
62
|
+
producer_id: <%= ENV['ONS_PRODUCER_ID'] %>
|
63
|
+
topic: <%= ENV['ONS_TOPIC'] %>
|
64
|
+
tag: 'user_service'
|
65
|
+
|
66
|
+
development:
|
67
|
+
<<: *default
|
68
|
+
|
69
|
+
test:
|
70
|
+
<<: *default
|
71
|
+
|
72
|
+
production:
|
73
|
+
<<: *default
|
74
|
+
```
|
75
|
+
|
76
|
+
### 消费者
|
77
|
+
|
78
|
+
#### 在 app/subscribers 目录下添加消费者实现文件,比如 user\_service\_subscriber.rb
|
79
|
+
|
80
|
+
```ruby
|
81
|
+
# 注意,类名应当与 config/ons_on_rails.yml 中的配置保持一致
|
82
|
+
class UserServiceSubscriber
|
83
|
+
include OnsOnRails::Subscriber
|
84
|
+
|
85
|
+
def consume(message)
|
86
|
+
# do something...
|
87
|
+
end
|
88
|
+
end
|
89
|
+
```
|
90
|
+
|
91
|
+
#### 在 daemons/ 目录下添加守护进程定义文件,比如 user\_service\_subscriber\_control.rb
|
92
|
+
|
93
|
+
```ruby
|
94
|
+
require 'rubygems'
|
95
|
+
require 'ons_on_rails'
|
96
|
+
|
97
|
+
APP_PATH = File.expand_path('../..', __FILE__)
|
98
|
+
OnsOnRails.run_subscriber_as_a_daemon(:user_service_subscriber, APP_PATH)
|
99
|
+
```
|
100
|
+
|
101
|
+
#### 启动或关闭消费者进程,此进程会与阿里云 MQ 建立 TCP 连接,然后在本地消费消息
|
102
|
+
|
103
|
+
```bash
|
104
|
+
$ RAILS_ENV=development bundle exec ruby daemons/user_service_subscriber_control.rb start
|
105
|
+
$ RAILS_ENV=development bundle exec ruby daemons/user_service_subscriber_control.rb stop
|
106
|
+
```
|
107
|
+
|
108
|
+
### 生产者
|
109
|
+
|
110
|
+
#### 在 config/initializers/ 目录下添加初始化文件,比如 ons\_on\_rails.rb
|
111
|
+
|
112
|
+
```ruby
|
113
|
+
# 注意,第一个参数应当与 config/ons_on_rails.yml 中的配置保持一致
|
114
|
+
$user_service_publisher = OnsOnRails.create_publisher(:user_service_publisher, backend: :tcp)
|
115
|
+
```
|
116
|
+
|
117
|
+
#### 调用 OnsOnRails::Publisher#publish 方法
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
$user_service_publisher.publish(operate: :create, user: { name: '123456lkjhgf' })
|
121
|
+
```
|
122
|
+
|
123
|
+
#### 测试相关
|
124
|
+
|
125
|
+
设置 OnsOnRails::Publisher 的 backend 为 :test 方法即可,这样生产者会将消息保存到 OnsOnRails::Publisher.deliveries 数组中
|
data/Rakefile
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
|
3
|
+
APP_RAKEFILE = File.expand_path('../spec/dummy/Rakefile', __FILE__)
|
4
|
+
load 'rails/tasks/engine.rake'
|
5
|
+
load 'rails/tasks/statistics.rake'
|
6
|
+
|
7
|
+
require 'bundler/gem_tasks'
|
8
|
+
require 'rspec/core/rake_task'
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new :spec do
|
11
|
+
Rails.env = 'test'
|
12
|
+
Rake::Task['app:db:environment:set'].invoke if Rails::VERSION::MAJOR == 5
|
13
|
+
Rake::Task['app:db:drop'].invoke
|
14
|
+
Rake::Task['app:db:create'].invoke
|
15
|
+
Rake::Task['app:db:schema:load'].invoke
|
16
|
+
end
|
17
|
+
|
18
|
+
task 'spec:all' do
|
19
|
+
Dir.glob(File.join(__dir__, 'gemfiles', '*.gemfile')).each do |gemfile|
|
20
|
+
sh "BUNDLE_GEMFILE=#{gemfile} bundle install --quiet"
|
21
|
+
sh "BUNDLE_GEMFILE=#{gemfile} bin/rake spec"
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
task default: 'spec:all'
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require 'bundler/setup'
|
4
|
+
require 'ons_on_rails'
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require 'irb'
|
14
|
+
IRB.start
|
data/bin/rake
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rake' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'pathname'
|
11
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
require 'bundler/setup'
|
16
|
+
|
17
|
+
load Gem.bin_path('rake', 'rake')
|
data/bin/rspec
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rspec' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'pathname'
|
11
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
require 'bundler/setup'
|
16
|
+
|
17
|
+
load Gem.bin_path('rspec-core', 'rspec')
|
data/bin/rubocop
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
#
|
4
|
+
# This file was generated by Bundler.
|
5
|
+
#
|
6
|
+
# The application 'rubocop' is installed as part of a gem, and
|
7
|
+
# this file is here to facilitate running it.
|
8
|
+
#
|
9
|
+
|
10
|
+
require 'pathname'
|
11
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile',
|
12
|
+
Pathname.new(__FILE__).realpath)
|
13
|
+
|
14
|
+
require 'rubygems'
|
15
|
+
require 'bundler/setup'
|
16
|
+
|
17
|
+
load Gem.bin_path('rubocop', 'rubocop')
|
data/bin/setup
ADDED
@@ -0,0 +1,52 @@
|
|
1
|
+
module OnsOnRails
|
2
|
+
class Publisher
|
3
|
+
# Create a Publisher.
|
4
|
+
#
|
5
|
+
# @param backend [#to_s] backend name, such as :tcp, :test, etc.
|
6
|
+
# @param options [Hash{Symbol => String}]
|
7
|
+
# @option options [String] :access_key the access key to aliyun ONS
|
8
|
+
# @option options [String] :secret_key the secret key to aliyun ONS
|
9
|
+
# @option options [String] :producer_id the producer ID
|
10
|
+
# @option options [String] :topic the message topic
|
11
|
+
# @option options [String] :tag the message tag
|
12
|
+
def initialize(backend, options)
|
13
|
+
required_keys = %i(access_key secret_key producer_id topic tag)
|
14
|
+
required_keys.each { |required_key| options.fetch(required_key) }
|
15
|
+
|
16
|
+
@default_topic = options.fetch(:topic)
|
17
|
+
@default_tag = options.fetch(:tag)
|
18
|
+
|
19
|
+
@backend_klass = OnsOnRails::Publishers.const_get(backend.to_s.camelize)
|
20
|
+
@backend = @backend_klass.new(options.slice(:access_key, :secret_key, :producer_id))
|
21
|
+
end
|
22
|
+
|
23
|
+
# Publish a message.
|
24
|
+
#
|
25
|
+
# @param data [Hash, String] the data which will be converted to the message body
|
26
|
+
# @param options [Hash{Symbol => String}]
|
27
|
+
# @option options [String] :topic overwrite the message topic
|
28
|
+
# @option options [String] :tag overwrite the message tag
|
29
|
+
# @option options [String] :key the message key, useful when debug
|
30
|
+
# @option options [String] :format('json') how convert the data to the message body, available format: 'json', 'raw', etc.
|
31
|
+
# @return [void]
|
32
|
+
def publish(data, options = {})
|
33
|
+
topic = options.fetch(:topic, @default_topic)
|
34
|
+
tag = options.fetch(:tag, @default_tag)
|
35
|
+
|
36
|
+
format = options.fetch(:format, 'json').to_sym
|
37
|
+
body =
|
38
|
+
case format
|
39
|
+
when :json then data.to_json
|
40
|
+
when :raw then data.to_s
|
41
|
+
else raise "unsupported message format #{format}"
|
42
|
+
end
|
43
|
+
|
44
|
+
@backend.publish(topic, tag, body, options.fetch(:key, ''))
|
45
|
+
end
|
46
|
+
|
47
|
+
# Keeps an array of all the messages published through the Publishers::Test backend. Most useful for unit and functional testing.
|
48
|
+
def self.deliveries
|
49
|
+
@deliveries ||= []
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module OnsOnRails
|
2
|
+
module Publishers
|
3
|
+
class Tcp
|
4
|
+
def initialize(options)
|
5
|
+
@client = Ons::Producer.new(options.fetch(:access_key), options.fetch(:secret_key), options.fetch(:producer_id))
|
6
|
+
@client.start
|
7
|
+
end
|
8
|
+
|
9
|
+
def publish(topic, tag, body, key)
|
10
|
+
@client.send_message(topic, tag, body, key)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module OnsOnRails
|
2
|
+
# .
|
3
|
+
module Subscriber
|
4
|
+
def self.included(base)
|
5
|
+
base.extend ClassMethods
|
6
|
+
end
|
7
|
+
|
8
|
+
module ClassMethods
|
9
|
+
# Allows customization for this type of subscriber.
|
10
|
+
#
|
11
|
+
# @param options [Hash{String, Symbol => String}]
|
12
|
+
# @option options [String] :access_key the access key to aliyun ONS
|
13
|
+
# @option options [String] :secret_key the secret key to aliyun ONS
|
14
|
+
# @option options [String] :consumer_id the consumer ID
|
15
|
+
# @option options [String] :topic the message topic
|
16
|
+
# @option options [String] :tag the subscribe expression used to filter messages
|
17
|
+
def ons_options(options = {})
|
18
|
+
@ons_options ||= begin
|
19
|
+
opts = OnsOnRails.ons_default_options
|
20
|
+
opts.slice(:access_key, :secret_key).merge(opts.fetch(name.to_s.underscore.to_sym, {}))
|
21
|
+
end
|
22
|
+
|
23
|
+
return @ons_options if options.blank?
|
24
|
+
@ons_options.merge!(options.symbolize_keys)
|
25
|
+
end
|
26
|
+
|
27
|
+
# Create a new subscriber instance to consume the incoming message.
|
28
|
+
#
|
29
|
+
# @param message [Hash{Symbol => Object}]
|
30
|
+
# @option message [String] topic, the message topic
|
31
|
+
# @option message [String] tag, the message tag
|
32
|
+
# @option message [String] body, the message body
|
33
|
+
# @option message [String] id, the message id
|
34
|
+
# @option message [String] key, the message key
|
35
|
+
# @return [Boolean] true/CommitMessage or false/ReconsumeLater
|
36
|
+
def consume(message)
|
37
|
+
new.consume(message)
|
38
|
+
true
|
39
|
+
rescue => ex
|
40
|
+
OnsOnRails.logger.error ex.message
|
41
|
+
OnsOnRails.logger.error ex.backtrace.join("\n")
|
42
|
+
false
|
43
|
+
end
|
44
|
+
|
45
|
+
# Determine whether it is a valid subscriber or not.
|
46
|
+
def check_subscriber_definition!
|
47
|
+
keys = %i(access_key secret_key consumer_id topic tag)
|
48
|
+
keys.each { |key| raise "missing key :#{key} in ons options" unless ons_options.key?(key) }
|
49
|
+
raise 'method #consume not implemented' unless instance_methods(false).include?(:consume)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/ons_on_rails.rb
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
require 'active_support/all'
|
2
|
+
require 'daemons'
|
3
|
+
|
4
|
+
require 'ons_on_rails/publisher'
|
5
|
+
Dir[File.expand_path('../ons_on_rails/publishers/*', __FILE__)].each { |f| require f }
|
6
|
+
|
7
|
+
require 'ons_on_rails/subscriber'
|
8
|
+
require 'ons_on_rails/version'
|
9
|
+
|
10
|
+
# .
|
11
|
+
module OnsOnRails
|
12
|
+
# Get the global logger.
|
13
|
+
def self.logger
|
14
|
+
@logger ||= initialize_logger
|
15
|
+
end
|
16
|
+
|
17
|
+
# Initialize an logger instance.
|
18
|
+
def self.initialize_logger
|
19
|
+
require 'logger'
|
20
|
+
Logger.new(STDOUT)
|
21
|
+
end
|
22
|
+
private_class_method :initialize_logger
|
23
|
+
|
24
|
+
# Get the ons default options.
|
25
|
+
def self.ons_default_options
|
26
|
+
@ons_default_options ||= initialize_ons_default_options
|
27
|
+
end
|
28
|
+
|
29
|
+
# Try to load ons options from config/ons_on_rails.yml.
|
30
|
+
def self.initialize_ons_default_options
|
31
|
+
return {} unless defined?(::Rails)
|
32
|
+
|
33
|
+
file = ::Rails.root.join('config', 'ons_on_rails.yml')
|
34
|
+
return {} unless file.exist?
|
35
|
+
|
36
|
+
require 'erb'
|
37
|
+
require 'yaml'
|
38
|
+
opts = YAML.load(ERB.new(IO.read(file)).result) || {}
|
39
|
+
opts.deep_symbolize_keys.fetch(::Rails.env.to_sym, {})
|
40
|
+
end
|
41
|
+
private_class_method :initialize_ons_default_options
|
42
|
+
|
43
|
+
# Create a Publisher.
|
44
|
+
#
|
45
|
+
# @param publisher_name [Symbol, String] the publisher's name
|
46
|
+
# @param backend [#to_s] backend name, such as :tcp, :test, etc.
|
47
|
+
#
|
48
|
+
# @see OnsOnRails::Publisher
|
49
|
+
def self.create_publisher(publisher_name, backend: :tcp)
|
50
|
+
options ||= begin
|
51
|
+
opts = OnsOnRails.ons_default_options
|
52
|
+
opts.slice(:access_key, :secret_key).merge(opts.fetch(publisher_name.to_s.underscore.to_sym, {}))
|
53
|
+
end
|
54
|
+
|
55
|
+
OnsOnRails::Publisher.new(backend, options)
|
56
|
+
end
|
57
|
+
|
58
|
+
# Run a subscriber as a separate process.
|
59
|
+
#
|
60
|
+
# @param subscriber_class_name [Symbol, String] the subscriber's class name
|
61
|
+
# @param app_path [String] the Rails root directory path
|
62
|
+
def self.run_subscriber_as_a_daemon(subscriber_class_name, app_path)
|
63
|
+
options = {
|
64
|
+
backtrace: true,
|
65
|
+
dir_mode: :normal,
|
66
|
+
dir: File.join(app_path, 'tmp', 'pids'),
|
67
|
+
log_dir: File.join(app_path, 'log'),
|
68
|
+
log_output: true
|
69
|
+
}
|
70
|
+
|
71
|
+
subscriber_class_name = subscriber_class_name.to_s.camelize
|
72
|
+
Daemons.run_proc(subscriber_class_name.underscore, options) do
|
73
|
+
require File.join(app_path, 'config', 'environment')
|
74
|
+
require 'ons' unless defined?(Ons)
|
75
|
+
|
76
|
+
subscriber_class = subscriber_class_name.constantize
|
77
|
+
subscriber_class.check_subscriber_definition!
|
78
|
+
|
79
|
+
options = subscriber_class.ons_options
|
80
|
+
Ons::Consumer.new(options.fetch(:access_key), options.fetch(:secret_key), options.fetch(:consumer_id))
|
81
|
+
.subscribe(options.fetch(:topic), options.fetch(:tag), &->(message) { subscriber_class.consume(message) })
|
82
|
+
.start
|
83
|
+
|
84
|
+
Ons.register_cleanup_hooks
|
85
|
+
Ons.loop_forever
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'ons_on_rails/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'ons_on_rails'
|
9
|
+
spec.version = OnsOnRails::VERSION
|
10
|
+
spec.authors = ['caochaoping']
|
11
|
+
spec.email = ['caochaoping@souche.com']
|
12
|
+
|
13
|
+
spec.summary = 'integrate the ons gem into a rails application'
|
14
|
+
spec.homepage = 'https://github.com/souche/ons_on_rails'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata) # rubocop: disable Style/GuardClause
|
20
|
+
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
21
|
+
else
|
22
|
+
raise 'RubyGems 2.0 or newer is required to protect against public gem pushes.'
|
23
|
+
end
|
24
|
+
|
25
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
26
|
+
spec.bindir = 'exe'
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ['lib']
|
29
|
+
|
30
|
+
spec.add_runtime_dependency 'daemons', '~> 1.2'
|
31
|
+
spec.add_runtime_dependency 'activesupport', '>= 4.1'
|
32
|
+
|
33
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
34
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
35
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
36
|
+
spec.add_development_dependency 'rubocop', '~> 0.41'
|
37
|
+
spec.add_development_dependency 'rubocop-rspec', '~> 1.5'
|
38
|
+
spec.add_development_dependency 'yard', '~> 0.9'
|
39
|
+
|
40
|
+
# Rails dependency
|
41
|
+
spec.add_development_dependency 'mysql2', '~> 0.3.13'
|
42
|
+
spec.add_development_dependency 'ons', '~> 1.0.0'
|
43
|
+
|
44
|
+
# Rails development dependency
|
45
|
+
spec.add_development_dependency 'rspec-rails', '~> 3.5'
|
46
|
+
spec.add_development_dependency 'database_cleaner', '~> 1.5'
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,236 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ons_on_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- caochaoping
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2016-10-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: daemons
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.2'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.1'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '4.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.12'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.12'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rubocop
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.41'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0.41'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rubocop-rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.5'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.5'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: yard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0.9'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0.9'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: mysql2
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: 0.3.13
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: 0.3.13
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: ons
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 1.0.0
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 1.0.0
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: rspec-rails
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '3.5'
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '3.5'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: database_cleaner
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - "~>"
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '1.5'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - "~>"
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '1.5'
|
181
|
+
description:
|
182
|
+
email:
|
183
|
+
- caochaoping@souche.com
|
184
|
+
executables: []
|
185
|
+
extensions: []
|
186
|
+
extra_rdoc_files: []
|
187
|
+
files:
|
188
|
+
- ".gitignore"
|
189
|
+
- ".rspec"
|
190
|
+
- ".rubocop.yml"
|
191
|
+
- ".yardopts"
|
192
|
+
- Gemfile
|
193
|
+
- LICENSE.txt
|
194
|
+
- README.md
|
195
|
+
- Rakefile
|
196
|
+
- bin/console
|
197
|
+
- bin/rake
|
198
|
+
- bin/rspec
|
199
|
+
- bin/rubocop
|
200
|
+
- bin/setup
|
201
|
+
- gemfiles/rails41.gemfile
|
202
|
+
- gemfiles/rails42.gemfile
|
203
|
+
- gemfiles/rails50.gemfile
|
204
|
+
- lib/ons_on_rails.rb
|
205
|
+
- lib/ons_on_rails/publisher.rb
|
206
|
+
- lib/ons_on_rails/publishers/tcp.rb
|
207
|
+
- lib/ons_on_rails/publishers/test.rb
|
208
|
+
- lib/ons_on_rails/subscriber.rb
|
209
|
+
- lib/ons_on_rails/version.rb
|
210
|
+
- ons_on_rails.gemspec
|
211
|
+
homepage: https://github.com/souche/ons_on_rails
|
212
|
+
licenses:
|
213
|
+
- MIT
|
214
|
+
metadata:
|
215
|
+
allowed_push_host: https://rubygems.org
|
216
|
+
post_install_message:
|
217
|
+
rdoc_options: []
|
218
|
+
require_paths:
|
219
|
+
- lib
|
220
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
221
|
+
requirements:
|
222
|
+
- - ">="
|
223
|
+
- !ruby/object:Gem::Version
|
224
|
+
version: '0'
|
225
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
226
|
+
requirements:
|
227
|
+
- - ">="
|
228
|
+
- !ruby/object:Gem::Version
|
229
|
+
version: '0'
|
230
|
+
requirements: []
|
231
|
+
rubyforge_project:
|
232
|
+
rubygems_version: 2.5.1
|
233
|
+
signing_key:
|
234
|
+
specification_version: 4
|
235
|
+
summary: integrate the ons gem into a rails application
|
236
|
+
test_files: []
|