Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial (only for sync actions) support of pure ruby nats #12

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
138 changes: 70 additions & 68 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
PATH
remote: .
specs:
artery (0.5.4)
artery (0.6.0)
multiblock (~> 0.2)
nats (~> 0.8)
rails (>= 4.2, < 6.0)

GEM
remote: https://rubygems.org/
specs:
actioncable (5.2.0)
actionpack (= 5.2.0)
actioncable (5.2.1)
actionpack (= 5.2.1)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailer (5.2.0)
actionpack (= 5.2.0)
actionview (= 5.2.0)
activejob (= 5.2.0)
actionmailer (5.2.1)
actionpack (= 5.2.1)
actionview (= 5.2.1)
activejob (= 5.2.1)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 2.0)
actionpack (5.2.0)
actionview (= 5.2.0)
activesupport (= 5.2.0)
actionpack (5.2.1)
actionview (= 5.2.1)
activesupport (= 5.2.1)
rack (~> 2.0)
rack-test (>= 0.6.3)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (5.2.0)
activesupport (= 5.2.0)
actionview (5.2.1)
activesupport (= 5.2.1)
builder (~> 3.1)
erubi (~> 1.4)
rails-dom-testing (~> 2.0)
rails-html-sanitizer (~> 1.0, >= 1.0.3)
activejob (5.2.0)
activesupport (= 5.2.0)
activejob (5.2.1)
activesupport (= 5.2.1)
globalid (>= 0.3.6)
activemodel (5.2.0)
activesupport (= 5.2.0)
activerecord (5.2.0)
activemodel (= 5.2.0)
activesupport (= 5.2.0)
activemodel (5.2.1)
activesupport (= 5.2.1)
activerecord (5.2.1)
activemodel (= 5.2.1)
activesupport (= 5.2.1)
arel (>= 9.0)
activestorage (5.2.0)
actionpack (= 5.2.0)
activerecord (= 5.2.0)
activestorage (5.2.1)
actionpack (= 5.2.1)
activerecord (= 5.2.1)
marcel (~> 0.3.1)
activesupport (5.2.0)
activesupport (5.2.1)
concurrent-ruby (~> 1.0, >= 1.0.2)
i18n (>= 0.7, < 2)
minitest (~> 5.1)
Expand All @@ -55,7 +55,7 @@ GEM
builder (3.2.3)
childprocess (0.9.0)
ffi (~> 1.0, >= 1.0.11)
concurrent-ruby (1.0.5)
concurrent-ruby (1.1.3)
crass (1.0.4)
diff-lcs (1.3)
erubi (1.7.1)
Expand All @@ -68,17 +68,18 @@ GEM
ffi (1.9.25)
globalid (0.4.1)
activesupport (>= 4.2.0)
i18n (1.0.1)
i18n (1.1.1)
concurrent-ruby (~> 1.0)
iniparse (1.4.4)
loofah (2.2.2)
jaro_winkler (1.5.1)
loofah (2.2.3)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
mail (2.7.0)
mail (2.7.1)
mini_mime (>= 0.1.1)
marcel (0.3.2)
marcel (0.3.3)
mimemagic (~> 0.3.2)
method_source (0.9.0)
method_source (0.9.2)
mimemagic (0.3.2)
mini_mime (1.0.1)
mini_portile2 (2.3.0)
Expand All @@ -87,69 +88,70 @@ GEM
nats (0.10.0)
eventmachine (~> 1.2, >= 1.2)
nio4r (2.3.1)
nokogiri (1.8.2)
nokogiri (1.8.5)
mini_portile2 (~> 2.3.0)
overcommit (0.45.0)
overcommit (0.46.0)
childprocess (~> 0.6, >= 0.6.3)
iniparse (~> 1.4)
parallel (1.12.1)
parser (2.5.1.0)
parser (2.5.3.0)
ast (~> 2.4.0)
powerpack (0.1.1)
rack (2.0.5)
rack-test (1.0.0)
powerpack (0.1.2)
rack (2.0.6)
rack-test (1.1.0)
rack (>= 1.0, < 3)
rails (5.2.0)
actioncable (= 5.2.0)
actionmailer (= 5.2.0)
actionpack (= 5.2.0)
actionview (= 5.2.0)
activejob (= 5.2.0)
activemodel (= 5.2.0)
activerecord (= 5.2.0)
activestorage (= 5.2.0)
activesupport (= 5.2.0)
rails (5.2.1)
actioncable (= 5.2.1)
actionmailer (= 5.2.1)
actionpack (= 5.2.1)
actionview (= 5.2.1)
activejob (= 5.2.1)
activemodel (= 5.2.1)
activerecord (= 5.2.1)
activestorage (= 5.2.1)
activesupport (= 5.2.1)
bundler (>= 1.3.0)
railties (= 5.2.0)
railties (= 5.2.1)
sprockets-rails (>= 2.0.0)
rails-dom-testing (2.0.3)
activesupport (>= 4.2.0)
nokogiri (>= 1.6)
rails-html-sanitizer (1.0.4)
loofah (~> 2.2, >= 2.2.2)
railties (5.2.0)
actionpack (= 5.2.0)
activesupport (= 5.2.0)
railties (5.2.1)
actionpack (= 5.2.1)
activesupport (= 5.2.1)
method_source
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
thor (>= 0.19.0, < 2.0)
rainbow (3.0.0)
rake (12.3.1)
rspec-core (3.7.1)
rspec-support (~> 3.7.0)
rspec-expectations (3.7.0)
rspec-core (3.8.0)
rspec-support (~> 3.8.0)
rspec-expectations (3.8.2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-mocks (3.7.0)
rspec-support (~> 3.8.0)
rspec-mocks (3.8.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.7.0)
rspec-rails (3.7.2)
rspec-support (~> 3.8.0)
rspec-rails (3.8.1)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (~> 3.7.0)
rspec-expectations (~> 3.7.0)
rspec-mocks (~> 3.7.0)
rspec-support (~> 3.7.0)
rspec-support (3.7.1)
rubocop (0.55.0)
rspec-core (~> 3.8.0)
rspec-expectations (~> 3.8.0)
rspec-mocks (~> 3.8.0)
rspec-support (~> 3.8.0)
rspec-support (3.8.0)
rubocop (0.60.0)
jaro_winkler (~> 1.5.1)
parallel (~> 1.10)
parser (>= 2.5)
parser (>= 2.5, != 2.5.1.1)
powerpack (~> 0.1)
rainbow (>= 2.2.2, < 4.0)
ruby-progressbar (~> 1.7)
unicode-display_width (~> 1.0, >= 1.0.1)
ruby-progressbar (1.9.0)
unicode-display_width (~> 1.4.0)
ruby-progressbar (1.10.0)
sprockets (3.7.2)
concurrent-ruby (~> 1.0)
rack (> 1, < 3)
Expand All @@ -158,11 +160,11 @@ GEM
activesupport (>= 4.0)
sprockets (>= 3.0.0)
sqlite3 (1.3.13)
thor (0.20.0)
thor (0.20.3)
thread_safe (0.3.6)
tzinfo (1.2.5)
thread_safe (~> 0.1)
unicode-display_width (1.3.2)
unicode-display_width (1.4.0)
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
Expand Down
1 change: 1 addition & 0 deletions artery.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,6 @@ Gem::Specification.new do |s|

s.add_dependency 'multiblock', '~> 0.2'
s.add_dependency 'nats', '~> 0.8'
# s.add_dependency 'nats-pure', '~> 0.5'
s.add_dependency 'rails', '>= 4.2', '< 6.0'
end
2 changes: 2 additions & 0 deletions exe/artery-sync
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ require ENV['APP_PATH'] || File.join(File.expand_path('.'), 'config', 'applicati
Rails.application.initialize!
Rails.application.eager_load!

Artery.use_backend :nats # redefine because only default works in worker

begin
Artery::Sync.new.execute
ensure
Expand Down
2 changes: 2 additions & 0 deletions exe/artery-worker
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ require ENV['APP_PATH'] || File.join(File.expand_path('.'), 'config', 'applicati
Rails.application.initialize!
Rails.application.eager_load!

Artery.use_backend :nats # redefine because only default works in worker

Artery::Worker.new.run
10 changes: 8 additions & 2 deletions lib/artery.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,16 @@ module Artery
include Subscriptions

module Backends
autoload :Base, 'artery/backends/base'
autoload :NATS, 'artery/backends/nats'
autoload :Base, 'artery/backends/base'
autoload :NATS, 'artery/backends/nats'
autoload :NATSPure, 'artery/backends/nats_pure'
end

register_backend :nats, :NATS
register_backend :nats_pure, :NATSPure

use_backend :nats # default

class << self
attr_accessor :worker
def handle_signals
Expand Down
22 changes: 19 additions & 3 deletions lib/artery/backend.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,26 @@ module Backend

included do
class << self
attr_accessor :backend
attr_reader :backend_in_use, :backends

def register_backend(type, class_name)
@backends ||= {}
@backends[type.to_sym] = Artery::Backends.const_get class_name
rescue LoadError, NameError
false
end

def use_backend(type)
raise ArgumentError, "Artery has no registered backend '#{type}'" unless backends[type.to_sym]

@backend_in_use = type

@backend&.stop
@backend = nil
end

def backend
@backend ||= Backends::NATS.new backend_config
@backend ||= backends[backend_in_use].new backend_config
end

delegate :start, :stop, :connect, :unsubscribe, to: :backend
Expand Down Expand Up @@ -68,8 +84,8 @@ def request(route, data = nil, _options = {})
# rubocop:enable Metrics/AbcSize

def publish(route, data)
Artery.logger.debug "PUBLISHED: [#{route}] #{data.to_json}"
backend.publish(route, data.to_json)
Artery.logger.debug "PUBLISHED: [#{route}] #{data.to_json}"
end
end
end
Expand Down
65 changes: 65 additions & 0 deletions lib/artery/backends/nats_pure.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# frozen_string_literal: true

require 'nats/io/client'

module Artery
module Backends
class NATSPure < Base
attr_accessor :client

def initialize(*args)
super
connect
end

def client
@client ||= ::NATS::IO::Client.new
end

delegate :connected?, :connecting?, to: :client

def connect
client.connect(options)
Artery.logger.debug "Connected to #{client.connected_server}"
end

def stop
client.close
end

# subscribe, unsubscribe, start not implemented, should use this backend ONLY for synchronous requests

def request(route, data, opts = {}, &blk)
# Always synchronous for now
response = client.request route, data, timeout: Artery.request_timeout
yield response.data
rescue ::NATS::IO::Timeout
yield(TimeoutError.new(request: { route: route, data: data }))
end

def publish(route, data)
client.publish route, data
end

private

def options
options = {}

options[:servers] = config[:servers] unless config[:servers].blank?
options[:user] = config[:user] unless config[:user].blank?
options[:pass] = config[:password] unless config[:password].blank?

options[:reconnect_time_wait] = config[:reconnect_timeout] unless config[:reconnect_timeout].blank?
options[:max_reconnect_attempts] = config[:reconnect_attempts] unless config[:reconnect_attempts].blank?

if ENV.key?('NATS_URL')
options[:servers] ||= []
options[:servers] << ENV['NATS_URL']
end

options
end
end
end
end
2 changes: 1 addition & 1 deletion lib/artery/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Artery
VERSION = '0.5.4'
VERSION = '0.6.0'
end