Introduction
AgentDispatcher is (almost) a non-intrusive application micro-framework that turns your plain ruby objects into configurable, reactive, and easily operable “agents”. They will react on commands; combine behaviors using dynamic configurations; multiply instances, play nice with with *nix infrastructure. In other words, object-oriented, shell-script-enabled approach towards agents modelling.
No rocket science – just a practical construct.
News
- 2008-04-05 – Release of 0.7.0 – the initial public release
Download and Installation
The library is available as gem. Install it by: gem install agentdispatcher
Documentation
The most up-to-date documentation of the library is its API (RDoc) documentation. Although the most effective documentation are usually examples.
Features
- dispatching of commands with arguments. Handled by
on_#{command}callback methods. - static definition of acceptable commands (
@AllowedCommands) - PORO (Plain Old Ruby Objects) – the behavior can be applied (mixed-in) to any class not interfering its own methods and constructor
- chain of inheritable custom constructor and destructor methods (
upMethod,downMethod) - hierarchical (layered) configuration (code-provided defaults < config files < manual parameters on invocation)
- configurations by absolute path, or config file name
- class defined configuration defaults (
@DefaultCfg) - configuration overlays (something like multiple class inheritance) (
@cfg) - sensible default behaviors (id, command, config do not have to be provided)
- identity (agent instance id) (
@id) - pid file management
- customizable ROOT path (where resources are found, logs, pids written) (
AGENTS_ROOT) - logging per instance (can be overridden) (
@log)
Examples
The basic features can be best illustrated by simple example.
agent.rb:
1 require 'rubygems' 2 require 'agentdispatcher' 3 4 class Agent 5 include AgentDispatcher 6 @AllowedCommands =['run'] 7 def on_run *args 8 @log.info "Executing run command #{@id}, config: {@cfg.inspect}" 9 end 10 end 11 12 class AgentChild < Agent 13 @DefaultCfg = {:log_path=>'/tmp'} 14 @AllowedCommands = %w(foo run) 15 upMethod :initBussines 16 def initBussines 17 @log.debug "post creation placeholder" 18 end 19 downMethod :closeBussines 20 def closeBussines 21 @log.debug "pre-shutdown placeholder" 22 end 23 def on_foo *args 24 @log.info "Executing foo arguments:#{args}" 25 end 26 end 27 if __FILE__ == $0 28 AgentChild::Dispatch *ARGV 29 end 30
The “agent” can be easily invoked/customized from command-line:
... executes a single instance of agent with idagent.rb myId foo arg1 arg2myIdand invoke methodfoothat will be passed argumentsarg1, arg2. A default configuration is used.
... all default. Idagent.rbagentchild, methodrun, default config, no arguments.-
... agent loads two configurations (specified by name here).agent.rb --config cfgfile1,cfgfile2 agentID -
... prints a usage help.agent.rb --help
Note: If you are trying the examples your are probably getting an error complaining on missing path. This is because agents wants to write pid file (and logs). Just run the command with nopid options agent.rb --nopid ... (or specify it in the config).
The example shows the objects can use standard inheritance (AgentChild Copyright© 2008 Viktor Zigo. All rights reserved. Distributed under GNU GPL License (see LICENSE). Sponsored by: 7inf.comAuthor and License