|
Configuration
How to configure a13g
The dials to turn, and switches to flip, to make a13g behave the way you want it: broker.ymlWhen you run the processor generator for the first time, a default 'config/broker.yml' is created. Its purpose is to configure the message broker connection for each environment, much the same as 'database.yml' for ActiveRecord. Like database.yml, the top level of the yaml file is divided by environment, and the section that matches the RAILS_ENV environment value is the one that gets loaded: development: ... test: ... production: By default, a13g will create a single connection to a single broker as configured within the environment section. This 'default' broker configuration will be used for all queues unless a different broker is specified for the queue. It is possible to define multiple connections in the broker.yml. This is done by adding another level to the yaml file, giving the new connection info a name, for example, to add a 'foo' connection and a 'bar' connection configuration to the development section, do the following: development:
adapter: stomp
host: localhost
reliable: true
foo:
adapter: asqs
access_key_id: XXXXXXXXXXXXXXXXXXXX
secret_access_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
bar:
adapter: wmq
q_mgr_name: my_queue_manager
In the above configuration, the stomp adapter is the default, and will be used for queues that have no other broker name specified, but foo and bar are also defined, and can be specified as the broker to use for any queue in a messaging.rb queue statement (see syntax below). Each adapter has it's own defaults and possible configuration values, but all of them start with an 'adapter: something' value to identify which adapter should be used to create the connection based on all the remaining configuration parameters. StompHere are the defaults for an example stomp connection. With the defaults below, all you really have to specify is the adapter as stomp and you are ready to go: development:
adapter: stomp
login: ""
passcode: ""
host: localhost
port: 61613
reliable: true
reconnectDelay: 5Here's what you can do with these options:
Amazon SQSFor Amazon SQS, you must specify a valid access key and secret key, all other values below are defaults, and optional. development:
adapter: asqs
access_key_id: XXXXXXXXXXXXXXXXXXXX
secret_access_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
host: queue.amazonaws.com
port: 80
reliable: true
reconnectDelay: 5
aws_version: 2006-04-01
content_type: text/plain
poll_interval: 1
cache_queue_list: trueHere's what you can do with these options:
BeanstalkFor Beanstalk, you need to have the gem beanstalk client gem installed (see here for more information: http://beanstalk.rubyforge.org/). sudo gem install beanstalk-client development:
adapter: beanstalk
host: localhost
port: Here's what you can do with these options:
Websphere MQFor Websphere MQ, at a minimum you need to specify the queue manager name, 'q_mgr_name', all other parameters are passed in to the underlying WMQ connection, see the WMQ::QueueManager connection api docs. development:
adapter: wmq
q_mgr_name: ""
poll_interval: .1Here's what you can do with these options:
ReliableMessagingTBD messaging.rbThe main place for configuring your messaging environment is 'config/messaging.rb', also created when the generator is first run. Here are the methods available on the 'ActiveMessaging::Gateway' while in the 'define' block. It bears mentioning that since this is just a ruby block, you can do whatever the language and Rails environment allow to solve your own unique issues. destination ['name', symbol for destination] ['destination', string] ['headers', hash of defaults for send] ['broker name', string to specify broker connection info to use] You use this method to define destinations - be they queues or topics or whatever else. 'name' should be a symbol, and is the logical name for this destination that will be used in the code. 'destination' string is the actual value for the destination that the broker understands. 'headers' hash is how you can pass in the default headers that should be applied to each message sent to this destination, can be any pair. The default includes :persistent => true, even if nothing is specified, in order to make sure messages will be saved by ActiveMQ when sent to a destination. Other header values mapped to JMS headers are described in the ActiveMQ documentation, http://activemq.apache.org/stomp.html. 'broker name' string is the name of the broker connection configuration in 'broker.yml' to use for this queue, both for sending to and requesting messages from. When not specified, uses the default broker connection configuration for the environment. Here is an example of a 'bar_queue' using the default broker connection, and the 'foo_queue' specified to use the 'foo' Amazon SQS broker defined in broker.yml: ActiveMessaging::Gateway.define do |s|
s.queue :bar_queue, '/topic/bar.queue'
s.queue :foo_queue, 'foosqsqueue', {}, 'foo'
endWith this definition, messages sent to :foo_queue will be sent to Amazon SQS as configured for foo, and the poller will create a connection to Amazon SQS and a thread to receive messages for foo if a processor subscribes to the foo_queue. connection_configuration= ['connection configuration', hash] You can use this method to override the configuration loaded for the connection in the 'config/broker.yml'. All the same hash values can be set, the keys should be set as symbols. You usually won't need this, but it could be useful for some environments, such as loading configuration from the database, or some other external resource. processor_group ['group name'] ['array of processors'] A processor group is a way to run the poller to only execute a subset of the processors by passing the name of the group in the poller command line arguments. You specify the name of the processor as its underscored lowercase version. So if you have a FooBarProcessor and BarFooProcessor in a processor group, it would look like this: pollerIncluded with the procesor generator is the creation of a 'script/poller' script. This script uses the ruby daemons library to turn the poller.rb into a daemon. You need to have the daemon gem for this to work: sudo gem install daemons It is configured to allow running multiple instances, and to create a 'monitor' for each process which will restart it if the poller process ends. The log file for the poller and the pid files are all put under the 'tmp' directory. Daemon options can be configured in the 'script/poller' file's options hash: options = {
:app_name => "poller",
:dir_mode => :normal,
:dir => tmp_dir, #change this to move the pid and poller.output files
:multiple => true, #change this to allow only one instance of the script
:ontop => false,
:mode => :load,
:backtrace => true,
:monitor => true, #change this to turn off the monitor process
:log_output => true
}See the ruby daemon documentation for more information. Here are the available options when running 'script/poller': > ./script/poller -h
Usage: poller <command> <options> -- <application options>
* where <command> is one of:
start start an instance of the application
stop stop all instances of the application
restart stop all instances and restart them afterwards
run start the application and stay on top
zap set the application to a stopped state
* and where <options> may contain several of the following:
-t, --ontop Stay on top (does not daemonize)
-f, --force Force operation
Common options:
-h, --help Show this message
--version Show versionAdditional options can be passed to the underlying script, poller.rb in this case, by using a '--' separator. At the moment the only supported option is the process_group, mentioned above. If you start the poller without specifying a process group, then all processors will be subscribed and run. If you start with a 'script/poller run -- process-group=critical_group' command line argument, then only the processors added to that group will be run. |
fwiw, connection_configuration() is not available. Instead, it looks like the configuration file is processed as a ERB file and therefor you can values in the yaml file via ERB functions
How do I get the poller to run in production mode instead of development?
warnis:
I did the following to get it to work. In vendor/plugins/activemessaging/poller.rb edit the file to look like this:
#!/usr/bin/env ruby # Make sure stdout and stderr write out without delay for using with daemon like scripts STDOUT.sync = true; STDOUT.flush STDERR.sync = true; STDERR.flush unless ENV['RAILS_ENV'] rails_mode = ARGV.first || "development" unless ["development", "test", "production"].include?(rails_mode) raise "Unknown rails environment '#{rails_mode}'. (Choose 'development', 'test' or 'production')" end ENV['RAILS_ENV'] = rails_mode end # Load Rails RAILS_ROOT=File.expand_path(File.join(File.dirname(__FILE__), '..','..','..')) load File.join(RAILS_ROOT, 'config', 'environment.rb') # Load ActiveMessaging processors ActiveMessaging::load_processors # Start it up! ActiveMessaging::startthen you can get poller to start in various modes with the following options:
script/poller [start|stop|run] -- [development|test|production]
I have a weird problem with running the poller.
I have an upload to S3 logic on_message() in my Processor. I use Paperclip for connection to Amazon. In case that I've started the poller by this command: ./script/poller start
I have the access on s3.yml configs only on startup. I get an exception on every next access of this action: AWS access keys are required to operate on S3
In case that I start the poller by a rake task: rake activemessaging:start_consumers I don't have such problems.
Here it is the code of the rake task: task :start_consumers do
endSo, it looks that there's an environment difference if I use RAKE command, compared to direct run of the script. Does anyone know where this difference is?
I found the problem. It looks like when running script/poller , there's no default environment. So I should pass it explicitly like this:
RAILS_ENV=development script/poller start
I hope it'll be useful for someone.
Has anybody successfully implemented a real time chat using AM? I'm having a hard time making the Processor communicate with my Controller..