wamp-worker 0.1.0
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 +7 -0
- data/.gitignore +13 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/.travis.yml +5 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +204 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/bin/wamp-worker +46 -0
- data/lib/wamp/worker.rb +132 -0
- data/lib/wamp/worker/config.rb +184 -0
- data/lib/wamp/worker/handler.rb +196 -0
- data/lib/wamp/worker/proxy/backgrounder.rb +38 -0
- data/lib/wamp/worker/proxy/base.rb +101 -0
- data/lib/wamp/worker/proxy/dispatcher.rb +115 -0
- data/lib/wamp/worker/proxy/requestor.rb +91 -0
- data/lib/wamp/worker/queue.rb +135 -0
- data/lib/wamp/worker/rails.rb +28 -0
- data/lib/wamp/worker/runner.rb +240 -0
- data/lib/wamp/worker/ticker.rb +30 -0
- data/lib/wamp/worker/version.rb +5 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/client_stub.rb +47 -0
- data/spec/support/handler_stub.rb +105 -0
- data/spec/support/redis_stub.rb +89 -0
- data/spec/support/session_stub.rb +101 -0
- data/spec/wamp/worker/config_spec.rb +90 -0
- data/spec/wamp/worker/handler_spec.rb +162 -0
- data/spec/wamp/worker/proxy_spec.rb +153 -0
- data/spec/wamp/worker/queue_spec.rb +49 -0
- data/spec/wamp/worker/runner_spec.rb +108 -0
- data/spec/wamp/worker_spec.rb +8 -0
- data/test/app_test.rb +124 -0
- data/test/hello.py +50 -0
- data/test/sidekiq.yml +5 -0
- data/test/wamp_test/.generators +8 -0
- data/test/wamp_test/.ruby-version +1 -0
- data/test/wamp_test/Gemfile +65 -0
- data/test/wamp_test/Gemfile.lock +246 -0
- data/test/wamp_test/README.md +24 -0
- data/test/wamp_test/Rakefile +6 -0
- data/test/wamp_test/app/assets/config/manifest.js +3 -0
- data/test/wamp_test/app/assets/images/.keep +0 -0
- data/test/wamp_test/app/assets/javascripts/application.js +16 -0
- data/test/wamp_test/app/assets/javascripts/cable.js +13 -0
- data/test/wamp_test/app/assets/javascripts/channels/.keep +0 -0
- data/test/wamp_test/app/assets/stylesheets/application.css +15 -0
- data/test/wamp_test/app/channels/application_cable/channel.rb +4 -0
- data/test/wamp_test/app/channels/application_cable/connection.rb +4 -0
- data/test/wamp_test/app/controllers/add_controller.rb +11 -0
- data/test/wamp_test/app/controllers/application_controller.rb +3 -0
- data/test/wamp_test/app/controllers/concerns/.keep +0 -0
- data/test/wamp_test/app/controllers/ping_controller.rb +7 -0
- data/test/wamp_test/app/handlers/add_handler.rb +9 -0
- data/test/wamp_test/app/handlers/back_add_handler.rb +26 -0
- data/test/wamp_test/app/handlers/back_ping_handler.rb +10 -0
- data/test/wamp_test/app/handlers/ping_handler.rb +10 -0
- data/test/wamp_test/app/helpers/application_helper.rb +2 -0
- data/test/wamp_test/app/jobs/application_job.rb +2 -0
- data/test/wamp_test/app/mailers/application_mailer.rb +4 -0
- data/test/wamp_test/app/models/application_record.rb +3 -0
- data/test/wamp_test/app/models/concerns/.keep +0 -0
- data/test/wamp_test/app/views/layouts/application.html.erb +15 -0
- data/test/wamp_test/app/views/layouts/mailer.html.erb +13 -0
- data/test/wamp_test/app/views/layouts/mailer.text.erb +1 -0
- data/test/wamp_test/bin/bundle +3 -0
- data/test/wamp_test/bin/rails +9 -0
- data/test/wamp_test/bin/rake +9 -0
- data/test/wamp_test/bin/setup +36 -0
- data/test/wamp_test/bin/spring +17 -0
- data/test/wamp_test/bin/update +31 -0
- data/test/wamp_test/bin/yarn +11 -0
- data/test/wamp_test/config.ru +5 -0
- data/test/wamp_test/config/application.rb +19 -0
- data/test/wamp_test/config/boot.rb +4 -0
- data/test/wamp_test/config/cable.yml +10 -0
- data/test/wamp_test/config/credentials.yml.enc +1 -0
- data/test/wamp_test/config/database.yml +25 -0
- data/test/wamp_test/config/environment.rb +5 -0
- data/test/wamp_test/config/environments/development.rb +61 -0
- data/test/wamp_test/config/environments/production.rb +94 -0
- data/test/wamp_test/config/environments/test.rb +46 -0
- data/test/wamp_test/config/initializers/application_controller_renderer.rb +8 -0
- data/test/wamp_test/config/initializers/assets.rb +14 -0
- data/test/wamp_test/config/initializers/backtrace_silencers.rb +7 -0
- data/test/wamp_test/config/initializers/content_security_policy.rb +25 -0
- data/test/wamp_test/config/initializers/cookies_serializer.rb +5 -0
- data/test/wamp_test/config/initializers/filter_parameter_logging.rb +4 -0
- data/test/wamp_test/config/initializers/inflections.rb +16 -0
- data/test/wamp_test/config/initializers/mime_types.rb +4 -0
- data/test/wamp_test/config/initializers/wamp-worker.rb +8 -0
- data/test/wamp_test/config/initializers/wrap_parameters.rb +14 -0
- data/test/wamp_test/config/locales/en.yml +33 -0
- data/test/wamp_test/config/master.key +1 -0
- data/test/wamp_test/config/puma.rb +34 -0
- data/test/wamp_test/config/routes.rb +4 -0
- data/test/wamp_test/config/sidekiq.yml +6 -0
- data/test/wamp_test/config/spring.rb +6 -0
- data/test/wamp_test/config/storage.yml +34 -0
- data/test/wamp_test/db/development.sqlite3 +0 -0
- data/test/wamp_test/db/seeds.rb +7 -0
- data/test/wamp_test/lib/assets/.keep +0 -0
- data/test/wamp_test/lib/tasks/.keep +0 -0
- data/test/wamp_test/package.json +5 -0
- data/test/wamp_test/public/404.html +67 -0
- data/test/wamp_test/public/422.html +67 -0
- data/test/wamp_test/public/500.html +66 -0
- data/test/wamp_test/public/apple-touch-icon-precomposed.png +0 -0
- data/test/wamp_test/public/apple-touch-icon.png +0 -0
- data/test/wamp_test/public/favicon.ico +0 -0
- data/test/wamp_test/public/robots.txt +1 -0
- data/test/wamp_test/storage/.keep +0 -0
- data/test/wamp_test/test/application_system_test_case.rb +5 -0
- data/test/wamp_test/test/controllers/.keep +0 -0
- data/test/wamp_test/test/fixtures/.keep +0 -0
- data/test/wamp_test/test/fixtures/files/.keep +0 -0
- data/test/wamp_test/test/helpers/.keep +0 -0
- data/test/wamp_test/test/integration/.keep +0 -0
- data/test/wamp_test/test/mailers/.keep +0 -0
- data/test/wamp_test/test/models/.keep +0 -0
- data/test/wamp_test/test/system/.keep +0 -0
- data/test/wamp_test/test/test_helper.rb +10 -0
- data/test/wamp_test/vendor/.keep +0 -0
- data/test/web/index.html +101 -0
- data/wamp-worker.gemspec +32 -0
- metadata +395 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 100ca25f15cff8d32a52b162a735f31cdb2753ef
|
4
|
+
data.tar.gz: a9895ee49189e59cfc5a90d7466da65cca11cd09
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f001f796d69ea9e0e076035efaa45c6f759f2cd573c5252a8aa083806e41eb506e8b56052d5e88e24e2ec357ee5e2564caa69a8acacc379f86586ce6d5b28d5b
|
7
|
+
data.tar.gz: 45d4550224b221af0e22d2164f5ea48dacd38442e017d286687eef8bcf5ac81e68f17005b3965c804b9f40e39aa09b14d5ff37e93abd262fdf1b16277b32f337
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.3.0
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Eric Chapman
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,204 @@
|
|
1
|
+
# Wamp::Worker
|
2
|
+
|
3
|
+
[](https://badge.fury.io/rb/wamp_client)
|
4
|
+
[](https://circleci.com/gh/ericchapman/ruby_wamp_worker/tree/master)
|
5
|
+
[](https://codecov.io/github/ericchapman/ruby_wamp_worker)
|
6
|
+
|
7
|
+
Rails worker for talking to a WAMP Router which is described [here](https://tools.ietf.org/html/draft-oberstet-hybi-tavendo-wamp-02)
|
8
|
+
|
9
|
+
This GEM is intended to replace [wamp_rails](https://github.com/ericchapman/ruby_wamp_rails).
|
10
|
+
|
11
|
+
This GEM is written using [wamp_client](https://github.com/ericchapman/ruby_wamp_client) to connect
|
12
|
+
to a WAMP router.
|
13
|
+
|
14
|
+
This GEM operates by using Redis to handle communication between your standard
|
15
|
+
Rails instances and the main Wamp::Worker "runner".
|
16
|
+
|
17
|
+
This GEM uses [Wamp::Client](https://github.com/ericchapman/ruby_wamp_client) to connect
|
18
|
+
to a WAMP router. Wamp::Client operates on top of EventMachine. One nuance of EventMachine
|
19
|
+
is that if an operation is blocking, it will block handling of all incoming operations until
|
20
|
+
it completes. To remedy this, Wamp::Worker supports integration with a Sidekiq worker in your
|
21
|
+
Rails application to push the operation to a background process. This will allow Sidekiq to
|
22
|
+
handle the operation while new requests come in.
|
23
|
+
|
24
|
+
This GEM operates using 2 different threads
|
25
|
+
|
26
|
+
- The main thread is responsible for executing the Wamp::Client EventMachine operation which
|
27
|
+
will establish the connection to the router. It also listens to the 2 other threads
|
28
|
+
looking for commands from Redis
|
29
|
+
- The other thread connects to Redis waiting for jobs to be pushed from either a Rails source
|
30
|
+
or jobs that were pushed to a background worker. As it receives responses, it will pass
|
31
|
+
them to the main thread for processing.
|
32
|
+
|
33
|
+
Some notes about Wamp::Worker
|
34
|
+
|
35
|
+
- intended to run as a Rails worker like 'Sidekiq'
|
36
|
+
- requires a Redis connection
|
37
|
+
- requires Sidekiq if background handlers are intended to be used
|
38
|
+
- supports WAMP call/publish from your standard Rails classes
|
39
|
+
- supports WAMP register/subscribe by including the "Handler" or "BackgroundHandler" modules
|
40
|
+
to classes you create
|
41
|
+
- BackgroundHandler requires Sidekiq
|
42
|
+
|
43
|
+
## Revision History
|
44
|
+
|
45
|
+
- v0.0.1:
|
46
|
+
- Initial Release
|
47
|
+
|
48
|
+
## Installation
|
49
|
+
|
50
|
+
Add this line to your application's Gemfile:
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
gem 'wamp-worker'
|
54
|
+
```
|
55
|
+
|
56
|
+
And then execute:
|
57
|
+
|
58
|
+
$ bundle
|
59
|
+
|
60
|
+
Or install it yourself as:
|
61
|
+
|
62
|
+
$ gem install wamp-worker
|
63
|
+
|
64
|
+
## Usage
|
65
|
+
|
66
|
+
### Configuration
|
67
|
+
|
68
|
+
To configure Wamp::Worker, create a "config/initializers/wamp-worker.rb" file with the
|
69
|
+
following options
|
70
|
+
|
71
|
+
``` ruby
|
72
|
+
Wamp::Worker.configure do
|
73
|
+
timeout 30
|
74
|
+
redis Redis.new
|
75
|
+
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
The attributes are defined as follows
|
80
|
+
|
81
|
+
- timeout - (default: 60) number of seconds a "call" will wait before timing out
|
82
|
+
- redis - (default: Redis.new) either a Redis object or parameters to pass to
|
83
|
+
creating a Redis object
|
84
|
+
- connection - options to pass to the "Wamp::Client::Connection". See
|
85
|
+
[wamp_client](https://github.com/ericchapman/ruby_wamp_client) for more details
|
86
|
+
|
87
|
+
You can also "subscribe" and "register" in the configuration object. That will be
|
88
|
+
described later.
|
89
|
+
|
90
|
+
**Note that you MUST make sure "config.eager_load = true" is set in the environment files**
|
91
|
+
|
92
|
+
### Multiple Workers
|
93
|
+
|
94
|
+
Wamp::Worker supports creating multiple workers with different options. To do this,
|
95
|
+
pass a "name" to the "configure" method like below
|
96
|
+
|
97
|
+
``` ruby
|
98
|
+
Wamp::Worker.configure do
|
99
|
+
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
|
100
|
+
end
|
101
|
+
|
102
|
+
Wamp::Worker.configure :other do
|
103
|
+
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm2'
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
When the name is omitted, it will use the name ":default".
|
108
|
+
|
109
|
+
### Handlers
|
110
|
+
|
111
|
+
Handlers are controllers used to implement "subscribe" and "register" callbacks. An
|
112
|
+
example of one is shown below
|
113
|
+
|
114
|
+
**app/handlers/my_handler.rb**
|
115
|
+
|
116
|
+
``` ruby
|
117
|
+
class MyHandler
|
118
|
+
include Wamp::Worker::Handler
|
119
|
+
|
120
|
+
register "com.example.add", :add, { invoke: "roundrobin" }
|
121
|
+
register "com.example.subtract", :subtract, { invoke: "roundrobin" }
|
122
|
+
subscribe "com.example.listener", :listener
|
123
|
+
|
124
|
+
def add
|
125
|
+
args[0] + args[1]
|
126
|
+
end
|
127
|
+
|
128
|
+
def subtract
|
129
|
+
args[0] - args[1]
|
130
|
+
end
|
131
|
+
|
132
|
+
def listener
|
133
|
+
# Do something
|
134
|
+
end
|
135
|
+
|
136
|
+
end
|
137
|
+
```
|
138
|
+
|
139
|
+
You can also "register" and "subscribe" in the configure block
|
140
|
+
|
141
|
+
``` ruby
|
142
|
+
Wamp::Worker.configure do
|
143
|
+
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
|
144
|
+
|
145
|
+
register "com.example.add", MyHandler, :add, { invoke: "roundrobin" }
|
146
|
+
register "com.example.subtract", MyHandler, :subtract, { invoke: "roundrobin" }
|
147
|
+
subscribe "com.example.listener", MyHandler, :listener
|
148
|
+
end
|
149
|
+
```
|
150
|
+
|
151
|
+
#### Background Handlers
|
152
|
+
|
153
|
+
For Rails applications that have Sidekiq, you can push the processing of the handler
|
154
|
+
to the background by including "Wamp::Worker::BackgroundHandler" instead of
|
155
|
+
"Wamp::Worker::Handler".
|
156
|
+
|
157
|
+
### Rails Access
|
158
|
+
|
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
|
161
|
+
|
162
|
+
**app/controllers/application_controller.rb**
|
163
|
+
|
164
|
+
``` ruby
|
165
|
+
class ApplicationController < ActionController::Base
|
166
|
+
include Wamp::Worker::Session.new
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
**app/controllers/add_controller.rb**
|
171
|
+
|
172
|
+
``` ruby
|
173
|
+
class AddController < ApplicationController
|
174
|
+
def index
|
175
|
+
response = nil
|
176
|
+
|
177
|
+
self.wamp_session.call "com.example.back.add", [params[:a].to_i, params[:b].to_i] do |result, error, details|
|
178
|
+
response = result
|
179
|
+
end
|
180
|
+
|
181
|
+
render json: { result: response }
|
182
|
+
end
|
183
|
+
end
|
184
|
+
```
|
185
|
+
|
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
|
+
```
|
194
|
+
|
195
|
+
### Starting the Worker
|
196
|
+
|
197
|
+
To start the worker, use the "wamp-worker" executable
|
198
|
+
|
199
|
+
$ bundle exec wamp-worker
|
200
|
+
|
201
|
+
this executable supports the following options
|
202
|
+
|
203
|
+
- "-l" (default: "info"): logging level (debug, info, warn, error)
|
204
|
+
- "-n" (default: "default"): name of the worker
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "wamp/worker"
|
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/setup
ADDED
data/bin/wamp-worker
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "wamp/worker/rails"
|
4
|
+
require "wamp/worker"
|
5
|
+
require "optparse"
|
6
|
+
require 'fileutils'
|
7
|
+
|
8
|
+
options = {
|
9
|
+
:log_level => :info,
|
10
|
+
:environment => "development",
|
11
|
+
:require => "./",
|
12
|
+
}
|
13
|
+
|
14
|
+
# Parse the options from the command line
|
15
|
+
OptionParser.new do |opts|
|
16
|
+
opts.banner = "Usage: wamp-worker [options]"
|
17
|
+
|
18
|
+
opts.on("-l", "--log-level LEVEL", "Sets the logging level") do |arg|
|
19
|
+
options[:log_level] = arg.strip.to_sym
|
20
|
+
end
|
21
|
+
|
22
|
+
opts.on("-n", "--name NAME", "Worker name") do |arg|
|
23
|
+
options[:name] = arg.strip.to_sym
|
24
|
+
end
|
25
|
+
|
26
|
+
opts.on '-r', '--require [PATH|DIR]', "Location of Rails application with workers or file to require" do |arg|
|
27
|
+
options[:require] = arg.strip
|
28
|
+
end
|
29
|
+
|
30
|
+
opts.on '-e', '--environment ENV', "Application environment" do |arg|
|
31
|
+
options[:environment] = arg.strip
|
32
|
+
end
|
33
|
+
end.parse!
|
34
|
+
|
35
|
+
# Set the logging level of the worker
|
36
|
+
log_level = options[:log_level]
|
37
|
+
Wamp::Worker.logger.info("Wamp::Worker using log level '#{log_level}'")
|
38
|
+
Wamp::Worker.log_level = log_level
|
39
|
+
|
40
|
+
# Load rails
|
41
|
+
environment = options[:environment]
|
42
|
+
Wamp::Worker.logger.info("Wamp::Worker using Rails environment '#{environment}'")
|
43
|
+
Wamp::Worker::Rails.load_app(environment, options[:require])
|
44
|
+
|
45
|
+
# Start the runner
|
46
|
+
Wamp::Worker.run(options[:name], **options)
|
data/lib/wamp/worker.rb
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
require "wamp/worker/version"
|
2
|
+
require "wamp/worker/proxy/requestor"
|
3
|
+
require "wamp/worker/proxy/dispatcher"
|
4
|
+
require "wamp/worker/proxy/backgrounder"
|
5
|
+
require "wamp/worker/queue"
|
6
|
+
require "wamp/worker/ticker"
|
7
|
+
require "wamp/worker/handler"
|
8
|
+
require "wamp/worker/config"
|
9
|
+
require "wamp/worker/runner"
|
10
|
+
require "wamp/client"
|
11
|
+
require "redis"
|
12
|
+
|
13
|
+
module Wamp
|
14
|
+
module Worker
|
15
|
+
|
16
|
+
# Used to include a requestor in a rails class
|
17
|
+
#
|
18
|
+
class Session
|
19
|
+
def self.new(name=nil, method: :wamp_session)
|
20
|
+
name ||= DEFAULT
|
21
|
+
Module.new do
|
22
|
+
define_method(method) { Wamp::Worker.requestor(name) }
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# Returns the config object
|
28
|
+
#
|
29
|
+
def self.config
|
30
|
+
unless defined?(@config)
|
31
|
+
@config = Config.new
|
32
|
+
end
|
33
|
+
@config
|
34
|
+
end
|
35
|
+
|
36
|
+
# Returns the logger object
|
37
|
+
#
|
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
|
45
|
+
end
|
46
|
+
|
47
|
+
# Sets the log level
|
48
|
+
#
|
49
|
+
# @param log_level [Symbol] - the desired log level
|
50
|
+
def self.log_level=(log_level)
|
51
|
+
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
|
+
end
|
67
|
+
|
68
|
+
# Method to configure the worker
|
69
|
+
#
|
70
|
+
# @param name [Symbol] - The name of the connection
|
71
|
+
def self.configure(name=nil, &callback)
|
72
|
+
ConfigProxy.new(self.config, name).configure(&callback)
|
73
|
+
end
|
74
|
+
|
75
|
+
# Method to start a worker
|
76
|
+
#
|
77
|
+
# @param name [Symbol] - The name of the connection
|
78
|
+
def self.run(name, **args)
|
79
|
+
name ||= DEFAULT
|
80
|
+
|
81
|
+
# Get the connection info
|
82
|
+
options = Wamp::Worker.config.connection(name).merge(args)
|
83
|
+
|
84
|
+
# Create the runner and start it
|
85
|
+
Runner::Main.new(name, **options).start
|
86
|
+
end
|
87
|
+
|
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
|
+
# Registers procedures
|
100
|
+
#
|
101
|
+
# @param name [Symbol] - The name of the connection
|
102
|
+
# @param proxy [Wamp::Worker::Proxy] - The proxy that will be used by the handler
|
103
|
+
# @param session [Wamp::Client::Session] - The session
|
104
|
+
def self.register_procedures(name, proxy, session)
|
105
|
+
Wamp::Worker.config.registrations(name).each do |r|
|
106
|
+
handler = -> a,k,d {
|
107
|
+
self.logger.debug("#{self.name} invoking handler '#{r.klass}##{r.method}' for procedure '#{r.procedure}'")
|
108
|
+
r.klass.create(proxy, :procedure, a, k, d).invoke(r.method)
|
109
|
+
}
|
110
|
+
session.register(r.procedure, handler, r.options)
|
111
|
+
self.logger.info("#{self.name} register '#{r.klass}##{r.method}' for procedure '#{r.procedure}'")
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# Subscribe to topics
|
116
|
+
#
|
117
|
+
# @param name [Symbol] - The name of the connection
|
118
|
+
# @param proxy [Wamp::Worker::Proxy] - The proxy that will be used by the handler
|
119
|
+
# @param session [Wamp::Client::Session] - The session
|
120
|
+
def self.subscribe_topics(name, proxy, session)
|
121
|
+
Wamp::Worker.config.subscriptions(name).each do |s|
|
122
|
+
handler = -> a, k, d {
|
123
|
+
self.logger.debug("#{self.name} invoking handler '#{s.klass}##{s.method}' for subscription '#{s.topic}'")
|
124
|
+
s.klass.create(proxy, :subscription, a, k, d).invoke(s.method)
|
125
|
+
}
|
126
|
+
session.subscribe(s.topic, handler, s.options)
|
127
|
+
self.logger.info("#{self.name} subscribe '#{s.klass}##{s.method}' for topic '#{s.topic}'")
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
end
|
132
|
+
end
|