Class: Collective::Configuration
- Inherits:
-
Object
- Object
- Collective::Configuration
- Includes:
- Log
- Defined in:
- lib/collective/configuration.rb
Overview
Evaluate a ruby configuration file in the context of a Collective Configuration instance. Offers a DSL to build the jobs as well as setting before/after-fork hooks.
Collective configuration:
env() set_env(ENV) –env=ENV Sets the environment. Used in pid file and log file naming. Defaults to RAILS_ENV || RACK_ENV || “test”.
chdir(DIR) –chdir=DIR Changes the working directory. Creates it if necessary. Takes effect immediately. Can only be set once. Has no effect if specified more than once. Defaults to /tmp/$NAME
name() name=(NAME) –name=NAME Sets the name of the process. Defaults to the base name of the configuration file. Used in pid file and log file naming.
–path=PATH add_path(PATH) Adds a path to the Ruby load path. Can be used multiple times.
–require=LIB Requires a library or Ruby gem. Can be used multiple times.
Defined Under Namespace
Classes: PoolEnumerator
Instance Attribute Summary collapse
-
#after_forks ⇒ Object
readonly
Returns the value of attribute after_forks.
-
#args ⇒ Object
Returns the value of attribute args.
-
#before_forks ⇒ Object
readonly
Returns the value of attribute before_forks.
-
#defaults ⇒ Object
readonly
Returns the value of attribute defaults.
-
#dry_run ⇒ Object
Returns the value of attribute dry_run.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#name ⇒ Object
Returns the value of attribute name.
-
#pools ⇒ Object
readonly
Returns the value of attribute pools.
-
#root ⇒ Object
readonly
Returns the value of attribute root.
-
#verbose ⇒ Object
Returns the value of attribute verbose.
Class Method Summary collapse
Instance Method Summary collapse
-
#add_path(path) ⇒ Object
takes effect immediately.
- #add_pool(name, options = {}) ⇒ Object
- #after_fork(&block) ⇒ Object
- #args_for_daemon_spawn ⇒ Object
- #before_fork(&block) ⇒ Object
-
#chdir(path) ⇒ Object
takes effect immediately.
- #finalize ⇒ Object
-
#initialize(filename = nil) ⇒ Configuration
constructor
A new instance of Configuration.
- #load_file(filename) ⇒ Object
- #load_script(string) ⇒ Object
- #options_for_daemon_spawn ⇒ Object
- #policies ⇒ Object
-
#require_lib(r) ⇒ Object
convenience for -r on the command line.
- #set_default(key, value) ⇒ Object
- #set_defaults(options) ⇒ Object
-
#set_env(env) ⇒ Object
—————————————————————————- DSL —————————————————————————-.
- #set_name(name) ⇒ Object
Methods included from Log
#format_for_logging, #log, #logger, #logger=
Constructor Details
#initialize(filename = nil) ⇒ Configuration
Returns a new instance of Configuration.
110 111 112 113 114 115 116 |
# File 'lib/collective/configuration.rb', line 110 def initialize( filename = nil ) @verbose = 0 @dry_run = false @defaults = {} @pools = {} load_file(filename) if filename end |
Instance Attribute Details
#after_forks ⇒ Object (readonly)
Returns the value of attribute after_forks.
80 81 82 |
# File 'lib/collective/configuration.rb', line 80 def after_forks @after_forks end |
#args ⇒ Object
Returns the value of attribute args.
78 79 80 |
# File 'lib/collective/configuration.rb', line 78 def args @args end |
#before_forks ⇒ Object (readonly)
Returns the value of attribute before_forks.
79 80 81 |
# File 'lib/collective/configuration.rb', line 79 def before_forks @before_forks end |
#defaults ⇒ Object (readonly)
Returns the value of attribute defaults.
82 83 84 |
# File 'lib/collective/configuration.rb', line 82 def defaults @defaults end |
#dry_run ⇒ Object
Returns the value of attribute dry_run.
77 78 79 |
# File 'lib/collective/configuration.rb', line 77 def dry_run @dry_run end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
73 74 75 |
# File 'lib/collective/configuration.rb', line 73 def env @env end |
#name ⇒ Object
Returns the value of attribute name.
75 76 77 |
# File 'lib/collective/configuration.rb', line 75 def name @name end |
#pools ⇒ Object (readonly)
Returns the value of attribute pools.
83 84 85 |
# File 'lib/collective/configuration.rb', line 83 def pools @pools end |
#root ⇒ Object (readonly)
Returns the value of attribute root.
74 75 76 |
# File 'lib/collective/configuration.rb', line 74 def root @root end |
#verbose ⇒ Object
Returns the value of attribute verbose.
76 77 78 |
# File 'lib/collective/configuration.rb', line 76 def verbose @verbose end |
Class Method Details
.parse(argv = ARGV) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/collective/configuration.rb', line 47 def self.parse( argv = ARGV ) us = new optparse = OptionParser.new do |opts| opts. = "Usage: #{__FILE__} [options]* configuration_file_rb" opts.on( "-c", "--chdir DIR", "Change working directory." ) { |d| us.chdir(d) } opts.on( "-e", "--env ENV", "Set environment (env).") { |e| us.set_env(e) } opts.on( "-h", "--help", "Display this usage summary." ) { puts opts; exit } opts.on( "-n", "--name NAME", "Set daemon's name.") { |n| us.set_name(n) } opts.on( "-p", "--path PATH", "Add to load path.") { |d| us.add_path(d) } opts.on( "-r", "--require LIB", "Require a library.") { |l| us.require_lib(l) } opts.on( "-s", "--script DSL", "Include DSL script.") { |s| us.load_script(s) } opts.on( "-v", "--verbose", "Print stuff out.") { |s| us.verbose += 1 } opts.on( "--dry-run", "Don't launch the daemon.") { us.dry_run = true } end.parse!(argv) while argv.any? && File.exists?(argv.first) do us.load_file( argv.shift ) end us.args = argv us.finalize end |
Instance Method Details
#add_path(path) ⇒ Object
takes effect immediately
189 190 191 192 193 |
# File 'lib/collective/configuration.rb', line 189 def add_path(path) p = File.(path) log "Added #{p} to load path" if verbose >= 2 $:.push(p) unless $:.member?(p) end |
#add_pool(name, options = {}) ⇒ Object
215 216 217 218 219 220 221 222 |
# File 'lib/collective/configuration.rb', line 215 def add_pool( name, = {} ) before_forks = ([:before_forks] || []) + (self.before_forks || []) after_forks = ([:after_forks] || []) + (self.after_forks || []) = defaults.merge().merge before_forks: before_forks, after_forks: after_forks pools[name] = log "Added pool for #{name}" if verbose == 1 log "Added pool for #{name} with #{}" if verbose >= 2 end |
#after_fork(&block) ⇒ Object
229 230 231 232 |
# File 'lib/collective/configuration.rb', line 229 def after_fork(&block) @after_forks ||= [] @after_forks << block end |
#args_for_daemon_spawn ⇒ Object
159 160 161 |
# File 'lib/collective/configuration.rb', line 159 def args_for_daemon_spawn args + [self] end |
#before_fork(&block) ⇒ Object
224 225 226 227 |
# File 'lib/collective/configuration.rb', line 224 def before_fork(&block) @before_forks ||= [] @before_forks << block end |
#chdir(path) ⇒ Object
takes effect immediately
176 177 178 179 180 181 182 183 184 185 186 |
# File 'lib/collective/configuration.rb', line 176 def chdir(path) if ! @root then p = File.(path) mkdirp(p) if ! dry_run Dir.chdir(p) log "Changed working directory (root) to #{p}" if verbose >= 1 @root = p else log "Warning: working directory already set to #{root}; not changing to #{path}" end end |
#finalize ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/collective/configuration.rb', line 132 def finalize() if ! env then @env = ( ENV["RAILS_ENV"] || ENV["RACK_ENV"] || "test" ) log "Defaulting env to #{env}" if verbose >= 1 end if ! name then @name = "collective" log "Defaulting name to #{name}" if verbose >= 1 end if ! @root then chdir(default_root) end log inspect if verbose >= 2 freeze self end |
#load_file(filename) ⇒ Object
123 124 125 126 127 128 129 130 |
# File 'lib/collective/configuration.rb', line 123 def load_file(filename) log "Loading #{filename}" if verbose >= 1 instance_eval(File.read(filename),filename) if ! name then n = File.basename(filename).sub(/\.[^.]*$/,'') @name = n if n.size > 0 end end |
#load_script(string) ⇒ Object
118 119 120 121 |
# File 'lib/collective/configuration.rb', line 118 def load_script(string) log "Loading #{string}" if verbose >= 2 instance_eval(string) end |
#options_for_daemon_spawn ⇒ Object
149 150 151 152 153 154 155 156 157 |
# File 'lib/collective/configuration.rb', line 149 def mkdirp root, "#{root}/log", "#{root}/tmp", "#{root}/tmp/pids" if ! dry_run return { working_dir: root, log_file: "#{root}/log/#{name}_#{env}.log", pid_file: "#{root}/tmp/pids/#{name}_#{env}.pid", sync_log: local? } end |
#policies ⇒ Object
106 107 108 |
# File 'lib/collective/configuration.rb', line 106 def policies PoolEnumerator.new(pools) end |
#require_lib(r) ⇒ Object
convenience for -r on the command line
196 197 198 199 |
# File 'lib/collective/configuration.rb', line 196 def require_lib(r) require(r) log "Required #{r}" if verbose >= 2 end |
#set_default(key, value) ⇒ Object
201 202 203 204 205 206 207 208 209 |
# File 'lib/collective/configuration.rb', line 201 def set_default(key,value) # values which are arrays get merged, but nil will overwrite case value when Array @defaults[key] = (@defaults[key] || []) + value else @defaults[key] = value end end |
#set_defaults(options) ⇒ Object
211 212 213 |
# File 'lib/collective/configuration.rb', line 211 def set_defaults() .each { |k,v| set_default(k,v) } end |
#set_env(env) ⇒ Object
DSL
167 168 169 |
# File 'lib/collective/configuration.rb', line 167 def set_env(env) @env = env end |
#set_name(name) ⇒ Object
171 172 173 |
# File 'lib/collective/configuration.rb', line 171 def set_name(name) @name = name end |