wamp-worker 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 100ca25f15cff8d32a52b162a735f31cdb2753ef
4
- data.tar.gz: a9895ee49189e59cfc5a90d7466da65cca11cd09
3
+ metadata.gz: d59dde6fad655ba748c0aaec07b00104922c21ad
4
+ data.tar.gz: 04b00eacf5d7a51f84c2e1d5bbaed32b24f68a41
5
5
  SHA512:
6
- metadata.gz: f001f796d69ea9e0e076035efaa45c6f759f2cd573c5252a8aa083806e41eb506e8b56052d5e88e24e2ec357ee5e2564caa69a8acacc379f86586ce6d5b28d5b
7
- data.tar.gz: 45d4550224b221af0e22d2164f5ea48dacd38442e017d286687eef8bcf5ac81e68f17005b3965c804b9f40e39aa09b14d5ff37e93abd262fdf1b16277b32f337
6
+ metadata.gz: 241db674bc4577109ddc1ceea83200ba0f592d97c2c69b0a568422ea05708aee5b1b9900c3c38dbc967030f2ce46c44cbaf14859fcaa56f2298382c6a8b6f345
7
+ data.tar.gz: 158cb1345b8401eccc38e50a5fa83508de319b4371ce6450cabf8b1d659afa07137463dba1f533a1e36f81d93050c615cde47b1d39d3efe2c83438dfa30e21c5
data/.gitignore CHANGED
@@ -11,3 +11,4 @@
11
11
  /test/wamp_test/log/
12
12
  *.pyc
13
13
  .idea/
14
+ *.gem
@@ -2,4 +2,4 @@ sudo: false
2
2
  language: ruby
3
3
  rvm:
4
4
  - 2.3.0
5
- before_install: gem install bundler -v 1.13.2
5
+ before_install: gem install bundler -v 1.17
data/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # Wamp::Worker
2
2
 
3
- [![Gem Version](https://badge.fury.io/rb/wamp-worker.svg)](https://badge.fury.io/rb/wamp_client)
4
- [![Circle CI](https://circleci.com/gh/ericchapman/ruby_wamp_worker/tree/master.svg?&style=shield&circle-token=92813c17f9c9510c4c644e41683e7ba2572e0b2a)](https://circleci.com/gh/ericchapman/ruby_wamp_worker/tree/master)
3
+ [![Gem Version](https://badge.fury.io/rb/wamp-worker.svg)](https://badge.fury.io/rb/wamp-worker)
4
+ [![CI Status](https://travis-ci.org/ericchapman/ruby_wamp_worker.svg?branch=master)](https://travis-ci.org/ericchapman/ruby_wamp_worker)
5
5
  [![Codecov](https://img.shields.io/codecov/c/github/ericchapman/ruby_wamp_worker/master.svg)](https://codecov.io/github/ericchapman/ruby_wamp_worker)
6
6
 
7
7
  Rails worker for talking to a WAMP Router which is described [here](https://tools.ietf.org/html/draft-oberstet-hybi-tavendo-wamp-02)
@@ -42,6 +42,9 @@ Some notes about Wamp::Worker
42
42
 
43
43
  ## Revision History
44
44
 
45
+ - v0.0.2:
46
+ - Updated to use Wamp::Client logger
47
+ - Other minor cleanup
45
48
  - v0.0.1:
46
49
  - Initial Release
47
50
 
@@ -65,8 +68,9 @@ Or install it yourself as:
65
68
 
66
69
  ### Configuration
67
70
 
68
- To configure Wamp::Worker, create a "config/initializers/wamp-worker.rb" file with the
69
- following options
71
+ To configure Wamp::Worker, create a initializer file with the following options
72
+
73
+ **config/initializers/wamp-worker.rb**
70
74
 
71
75
  ``` ruby
72
76
  Wamp::Worker.configure do
@@ -87,7 +91,8 @@ The attributes are defined as follows
87
91
  You can also "subscribe" and "register" in the configuration object. That will be
88
92
  described later.
89
93
 
90
- **Note that you MUST make sure "config.eager_load = true" is set in the environment files**
94
+ **Note that you MUST make sure "config.eager_load = true" is set in the environment files
95
+ located in "config/environments/\*.rb"**
91
96
 
92
97
  ### Multiple Workers
93
98
 
@@ -154,27 +159,42 @@ For Rails applications that have Sidekiq, you can push the processing of the han
154
159
  to the background by including "Wamp::Worker::BackgroundHandler" instead of
155
160
  "Wamp::Worker::Handler".
156
161
 
157
- ### Rails Access
162
+ ### Call/Publish Methods
158
163
 
159
- The library also supports "call" and "publish" methods from Rails objects. This is
160
- done by including "Wamp::Worker::Session" in your class. For example
164
+ The library also supports "call" and "publish" methods from objects outside of the
165
+ worker. This is done by including "Wamp::Worker::Session" in your class. For example
161
166
 
162
- **app/controllers/application_controller.rb**
167
+ **app/controllers/add_controller.rb**
163
168
 
164
169
  ``` ruby
165
- class ApplicationController < ActionController::Base
170
+ class AddController < ApplicationController
166
171
  include Wamp::Worker::Session.new
172
+
173
+ def index
174
+ response = nil
175
+
176
+ self.wamp_session.call "com.example.back.add", [params[:a].to_i, params[:b].to_i] do |result, error, details|
177
+ response = result
178
+ end
179
+
180
+ render json: { result: response }
181
+ end
167
182
  end
168
183
  ```
169
184
 
185
+ Note that the name defaults to ":default" and the method "wamp_session". These
186
+ can be overridden by adding the following attributes to the "include"
187
+
170
188
  **app/controllers/add_controller.rb**
171
189
 
172
190
  ``` ruby
173
191
  class AddController < ApplicationController
192
+ include Wamp::Worker::Session.new, :other, :different_session
193
+
174
194
  def index
175
195
  response = nil
176
196
 
177
- self.wamp_session.call "com.example.back.add", [params[:a].to_i, params[:b].to_i] do |result, error, details|
197
+ self.different_session.call "com.example.back.add", [params[:a].to_i, params[:b].to_i] do |result, error, details|
178
198
  response = result
179
199
  end
180
200
 
@@ -183,22 +203,52 @@ class AddController < ApplicationController
183
203
  end
184
204
  ```
185
205
 
186
- Note that the name defaults to ":default" and the method ":wamp_session". These
187
- can be overridden by including in the "include"
188
-
189
- ``` ruby
190
- class ApplicationController < ActionController::Base
191
- include Wamp::Worker::Session.new :other, method: :different_session
192
- end
193
- ```
206
+ This will talk to the ":other" worker and expose it using "different_session"
207
+ rather than "wamp_session"
194
208
 
195
209
  ### Starting the Worker
196
210
 
211
+ There are 2 different ways you can start a worker
212
+
213
+ #### Rails/Heroku worker
214
+
197
215
  To start the worker, use the "wamp-worker" executable
198
216
 
199
217
  $ bundle exec wamp-worker
200
218
 
201
- this executable supports the following options
219
+ This executable supports the following options
202
220
 
203
221
  - "-l" (default: "info"): logging level (debug, info, warn, error)
204
222
  - "-n" (default: "default"): name of the worker
223
+ - "-e" (default: "development"): application environment to load
224
+
225
+ or in your Procfile for Heroku
226
+
227
+ **Procfile**
228
+
229
+ ```
230
+ web: bundle exec puma -C config/puma.rb
231
+ worker: bundle exec wamp-worker -e production
232
+ ```
233
+
234
+ #### Thread
235
+
236
+ You can also spawn a thread inside if your application. One way to do this
237
+ is as follows
238
+
239
+ **config/initializers/wamp-worker.rb**
240
+
241
+ ``` ruby
242
+ require "thread"
243
+
244
+ Thread.new do
245
+ Wamp::Worker.run uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
246
+ end
247
+ ```
248
+
249
+ Note that this method is not recommended for scalable designs because it will
250
+ create a new instance of the worker every time you scale the web instance.
251
+ Only do this if you really know what you are doing.
252
+
253
+ Also, when calling "register", you must include the "invoke" option. See WAMP
254
+ documentation for more details.
@@ -13,13 +13,13 @@ require "redis"
13
13
  module Wamp
14
14
  module Worker
15
15
 
16
- # Used to include a requestor in a rails class
16
+ # Used to include a requestor in any class
17
17
  #
18
18
  class Session
19
19
  def self.new(name=nil, method: :wamp_session)
20
20
  name ||= DEFAULT
21
21
  Module.new do
22
- define_method(method) { Wamp::Worker.requestor(name) }
22
+ define_method(method) { Proxy::Requestor.new(name) }
23
23
  end
24
24
  end
25
25
  end
@@ -36,12 +36,7 @@ module Wamp
36
36
  # Returns the logger object
37
37
  #
38
38
  def self.logger
39
- unless defined?(@logger)
40
- $stdout.sync = true unless ENV['RAILS_ENV'] == "production"
41
- @logger = Logger.new $stdout
42
- @logger.level = Logger::INFO
43
- end
44
- @logger
39
+ Wamp::Client.logger
45
40
  end
46
41
 
47
42
  # Sets the log level
@@ -49,20 +44,6 @@ module Wamp
49
44
  # @param log_level [Symbol] - the desired log level
50
45
  def self.log_level=(log_level)
51
46
  Wamp::Client.log_level = log_level
52
- level =
53
- case log_level
54
- when :error
55
- Logger::ERROR
56
- when :debug
57
- Logger::DEBUG
58
- when :fatal
59
- Logger::FATAL
60
- when :warn
61
- Logger::WARN
62
- else
63
- Logger::INFO
64
- end
65
- self.logger.level = level
66
47
  end
67
48
 
68
49
  # Method to configure the worker
@@ -75,7 +56,7 @@ module Wamp
75
56
  # Method to start a worker
76
57
  #
77
58
  # @param name [Symbol] - The name of the connection
78
- def self.run(name, **args)
59
+ def self.run(name=nil, **args)
79
60
  name ||= DEFAULT
80
61
 
81
62
  # Get the connection info
@@ -85,17 +66,6 @@ module Wamp
85
66
  Runner::Main.new(name, **options).start
86
67
  end
87
68
 
88
- # Returns a requestor for objects to perform calls to the worker
89
- #
90
- # @param name [Symbol] - The name of the connection
91
- # @return [Wamp::Worker::Proxy::Requestor] - An object that can be used to make requests
92
- def self.requestor(name)
93
- name ||= DEFAULT
94
-
95
- # Create a requestor proxy for the connection
96
- Proxy::Requestor.new(name)
97
- end
98
-
99
69
  # Registers procedures
100
70
  #
101
71
  # @param name [Symbol] - The name of the connection
@@ -107,8 +77,14 @@ module Wamp
107
77
  self.logger.debug("#{self.name} invoking handler '#{r.klass}##{r.method}' for procedure '#{r.procedure}'")
108
78
  r.klass.create(proxy, :procedure, a, k, d).invoke(r.method)
109
79
  }
110
- session.register(r.procedure, handler, r.options)
111
- self.logger.info("#{self.name} register '#{r.klass}##{r.method}' for procedure '#{r.procedure}'")
80
+ session.register(r.procedure, handler, r.options) do |result, error, details|
81
+ if error
82
+ self.logger.error("#{self.name} register failed '#{r.klass}##{r.method}' for procedure '#{r.procedure}'")
83
+ self.logger.error(" error: #{error.inspect}")
84
+ else
85
+ self.logger.info("#{self.name} registered for '#{r.klass}##{r.method}' for procedure '#{r.procedure}'")
86
+ end
87
+ end
112
88
  end
113
89
  end
114
90
 
@@ -123,8 +99,14 @@ module Wamp
123
99
  self.logger.debug("#{self.name} invoking handler '#{s.klass}##{s.method}' for subscription '#{s.topic}'")
124
100
  s.klass.create(proxy, :subscription, a, k, d).invoke(s.method)
125
101
  }
126
- session.subscribe(s.topic, handler, s.options)
127
- self.logger.info("#{self.name} subscribe '#{s.klass}##{s.method}' for topic '#{s.topic}'")
102
+ session.subscribe(s.topic, handler, s.options) do |result, error, details|
103
+ if error
104
+ self.logger.error("#{self.name} subscribe failed '#{s.klass}##{s.method}' for topic '#{s.topic}'")
105
+ self.logger.error(" error: #{error.inspect}")
106
+ else
107
+ self.logger.info("#{self.name} subscribed '#{s.klass}##{s.method}' for topic '#{s.topic}'")
108
+ end
109
+ end
128
110
  end
129
111
  end
130
112
 
@@ -110,11 +110,6 @@ module Wamp
110
110
  @client = options[:client] || Wamp::Client::Connection.new(options)
111
111
  @active = false
112
112
 
113
- # Log the event
114
- logger.info("#{self.class.name} '#{self.name}' created with options")
115
- logger.info(" uri: #{options[:uri]}")
116
- logger.info(" realm: #{options[:realm]}")
117
-
118
113
  # Create a queue for passing messages to the main runner
119
114
  @descriptor_queue = ::Queue.new
120
115
 
@@ -1,5 +1,5 @@
1
1
  module Wamp
2
2
  module Worker
3
- VERSION = "0.1.0"
3
+ VERSION = "0.1.1"
4
4
  end
5
5
  end
@@ -1,11 +1,13 @@
1
1
  require 'simplecov'
2
- SimpleCov.start do
3
- add_filter 'spec/'
4
- end
2
+ SimpleCov.start
5
3
 
6
- $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
7
4
  require "wamp/worker"
8
5
 
6
+ if ENV['CODECOV_TOKEN']
7
+ require 'codecov'
8
+ SimpleCov.formatter = SimpleCov::Formatter::Codecov
9
+ end
10
+
9
11
  Dir[File.expand_path('spec/support/**/*.rb')].each { |f| require f }
10
12
 
11
13
  require 'sidekiq/testing'
@@ -3,7 +3,7 @@ require "spec_helper"
3
3
  describe Wamp::Worker::Runner do
4
4
  let(:name) { :other }
5
5
  let(:runner) { described_class::Main.new(name) }
6
- let(:requestor) { Wamp::Worker.requestor(name) }
6
+ let(:requestor) { Wamp::Worker::Proxy::Requestor.new(name) }
7
7
 
8
8
  def execute_runner
9
9
 
@@ -65,12 +65,10 @@ describe Wamp::Worker::Runner do
65
65
 
66
66
  it "increments the ticker" do
67
67
  expect(runner.dispatcher.ticker.get).to eq(0)
68
-
69
- expect {
70
- execute_runner do
71
- sleep(2.5)
72
- end
73
- }.to change{ runner.dispatcher.ticker.get }.by(3)
68
+ execute_runner do
69
+ sleep(2)
70
+ end
71
+ expect(runner.dispatcher.ticker.get).not_to eq(0)
74
72
  end
75
73
 
76
74
  context "challenge" do
@@ -34,7 +34,7 @@ gem 'jbuilder', '~> 2.5'
34
34
  # gem 'capistrano-rails', group: :development
35
35
 
36
36
  gem 'sidekiq'
37
- gem 'wamp-worker'
37
+ gem 'wamp-worker', '~> 0.1.1'
38
38
 
39
39
  # Reduces boot times through caching; required in config/boot.rb
40
40
  gem 'bootsnap', '>= 1.1.0', require: false
@@ -189,10 +189,10 @@ GEM
189
189
  thread_safe (~> 0.1)
190
190
  uglifier (4.1.20)
191
191
  execjs (>= 0.3.0, < 3)
192
- wamp-worker (0.1.0)
193
- redis
194
- wamp_client (>= 0.1.2)
195
- wamp_client (0.2.0)
192
+ wamp-worker (0.1.1)
193
+ redis (~> 4.0)
194
+ wamp_client (~> 0.2.2)
195
+ wamp_client (0.2.2)
196
196
  json (~> 2.0)
197
197
  websocket-eventmachine-client (~> 1.1)
198
198
  web-console (3.7.0)
@@ -236,7 +236,7 @@ DEPENDENCIES
236
236
  turbolinks (~> 5)
237
237
  tzinfo-data
238
238
  uglifier (>= 1.3.0)
239
- wamp-worker
239
+ wamp-worker (~> 0.1.1)
240
240
  web-console (>= 3.3.0)
241
241
 
242
242
  RUBY VERSION
@@ -27,6 +27,6 @@ Gem::Specification.new do |spec|
27
27
 
28
28
  spec.required_ruby_version = '>= 2.3'
29
29
 
30
- spec.add_dependency 'wamp_client', '~> 0.2.0'
30
+ spec.add_dependency 'wamp_client', '~> 0.2.2'
31
31
  spec.add_dependency 'redis', '~> 4.0'
32
32
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wamp-worker
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Eric Chapman
@@ -100,14 +100,14 @@ dependencies:
100
100
  requirements:
101
101
  - - "~>"
102
102
  - !ruby/object:Gem::Version
103
- version: 0.2.0
103
+ version: 0.2.2
104
104
  type: :runtime
105
105
  prerelease: false
106
106
  version_requirements: !ruby/object:Gem::Requirement
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: 0.2.0
110
+ version: 0.2.2
111
111
  - !ruby/object:Gem::Dependency
112
112
  name: redis
113
113
  requirement: !ruby/object:Gem::Requirement