-
Notifications
You must be signed in to change notification settings - Fork 0
Runner
The Runner class encapsulates all of the responsibilities of the worker into a class that can be instantiated and executed in numerous environments including custom scripts or even a Ruby on Rails application.
A simple runner can be created with the following code
runner = Wamp::Worker::Main.new uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
runner.start
When "start" is called, the runner will execute infinitely until either
- Something calls
runner.stop
, or - An "INT" or "TERM" signal is sent to the script (from pressing
ctrl-c
for example)
The main thread of the runner is responsible for maintaining the socket connection to the WAMP router (see wamp_client) and performs the duties of a dispatcher proxy (see Proxy).
The runner spawns a side thread that will watch Redis queues for requests from requestors and backgrounders. To minimize the access to Redis, the Redis "brpop" method is used which will block until a descriptor is added to the Redis queues being watched. Upon receiving a descriptor, the thread will place it in a Ruby Queue object which is designed to allow information to be passed between threads.
While the main Runner thread is executing, it will check the Queue on each tick of the Event Machine to see if there are descriptors to execute and process them accordingly.
Note that the main Runner thread will also increment a "tick" counter every second which signals to the requestors if the worker is running in the event of a timeout.
To start the runner in a custom script, do the following
require "wamp/worker"
Wamp::Worker.configure do
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
end
Wamp::Worker.run
If you don't want the runner to block, you can also spawn a thread
Thread.new do
Wamp::Worker.run
end
Runners with different configurations are also supported
require "wamp/worker"
Wamp::Worker.configure :runner1 do
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
end
Wamp::Worker.configure :runner2 do
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm2'
end
Thread.new do
Wamp::Worker.run :runner1
end
Thread.new do
Wamp::Worker.run :runner2
end
The Wamp::Worker GEM includes the script "bin/wamp-worker" that is intended to encapsulate the runner. It has the following options
- "-l" (default: "info"): logging level (debug, info, warn, error)
- "-n" (default: "default"): name of the worker
- "-e" (default: "development"): application environment to load
- "-r" (default: "./": the path to the file to require. Note that is "-r" is a directory, the script will assume you are loading a "Ruby on Rails" application
To start the runner from the command line, do the following
$ wamp-worker -r "<path to require file>" -l info
To start the runner in a Ruby on Rails environment, create a config file
app/config/initializers/wamp-worker.rb
Wamp::Worker.configure do
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
end
and call the script
$ cd "<top of Rails application>"
$ wamp-worker -e development -l debug
Some notes about running in Rails
- Make sure have
config.eager_load = true
set in your config/environments/* files - Multiple workers can be created with different configurations by passing the name to the
configure call (
Wamp::Worker.configure :other do
) and passing the "-n" option to the script ($ wamp-worker -n other
)
To run as a Heroku worker, add the following to your Procfile
app/Procfile
web: bundle exec puma -C config/puma.rb
worker: bundle exec wamp-worker -e production
Multiple workers are also supported
app/config/initializers/wamp-worker.rb
Wamp::Worker.configure :worker1 do
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm1'
end
Wamp::Worker.configure :worker2 do
connection uri: 'ws://127.0.0.1:8080/ws', realm: 'realm2'
end
app/Procfile
web: bundle exec puma -C config/puma.rb
worker1: bundle exec wamp-worker -n worker1 -e production
worker2: bundle exec wamp-worker -n worker2 -e production