Class: Hive::Configuration

Inherits:
Object
  • Object
show all
Includes:
Log
Defined in:
lib/hive/configuration.rb

Overview

Evaluate a ruby configuration file in the context of a Hive Configuration instance. Offers a DSL to build the jobs as well as setting before/after-fork hooks.

Hive 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

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Log

#format_for_logging, #log, #logger, #logger=

Constructor Details

#initialize(filename = nil) ⇒ Configuration

Returns a new instance of Configuration.



109
110
111
112
113
114
115
# File 'lib/hive/configuration.rb', line 109

def initialize( filename = nil )
  @verbose  = 0
  @dry_run  = false
  @defaults = {}
  @pools    = {}
  load_file(filename) if filename
end

Instance Attribute Details

#after_forksObject (readonly)

Returns the value of attribute after_forks.



79
80
81
# File 'lib/hive/configuration.rb', line 79

def after_forks
  @after_forks
end

#argsObject

Returns the value of attribute args.



77
78
79
# File 'lib/hive/configuration.rb', line 77

def args
  @args
end

#before_forksObject (readonly)

Returns the value of attribute before_forks.



78
79
80
# File 'lib/hive/configuration.rb', line 78

def before_forks
  @before_forks
end

#defaultsObject (readonly)

Returns the value of attribute defaults.



81
82
83
# File 'lib/hive/configuration.rb', line 81

def defaults
  @defaults
end

#dry_runObject

Returns the value of attribute dry_run.



76
77
78
# File 'lib/hive/configuration.rb', line 76

def dry_run
  @dry_run
end

#envObject (readonly)

Returns the value of attribute env.



72
73
74
# File 'lib/hive/configuration.rb', line 72

def env
  @env
end

#nameObject

Returns the value of attribute name.



74
75
76
# File 'lib/hive/configuration.rb', line 74

def name
  @name
end

#poolsObject (readonly)

Returns the value of attribute pools.



82
83
84
# File 'lib/hive/configuration.rb', line 82

def pools
  @pools
end

#rootObject (readonly)

Returns the value of attribute root.



73
74
75
# File 'lib/hive/configuration.rb', line 73

def root
  @root
end

#verboseObject

Returns the value of attribute verbose.



75
76
77
# File 'lib/hive/configuration.rb', line 75

def verbose
  @verbose
end

Class Method Details

.parse(argv = ARGV) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/hive/configuration.rb', line 46

def self.parse( argv = ARGV )
  us = new

  optparse = OptionParser.new do |opts|
    opts.banner = "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



174
175
176
177
178
# File 'lib/hive/configuration.rb', line 174

def add_path(path)
  p = File.expand_path(path)
  log "Added #{p} to load path" if verbose >= 2
  $:.push(p) unless $:.member?(p)
end

#add_pool(name, options = {}) ⇒ Object



200
201
202
203
204
205
206
207
# File 'lib/hive/configuration.rb', line 200

def add_pool( name, options = {} )
  before_forks = (options[:before_forks] || []) + (self.before_forks || [])
  after_forks  = (options[:after_forks] || []) + (self.after_forks || [])
  options      = defaults.merge(options).merge before_forks: before_forks, after_forks: after_forks
  pools[name]  = options
  log "Added pool for #{name}" if verbose == 1
  log "Added pool for #{name} with #{options}" if verbose >= 2
end

#after_fork(&block) ⇒ Object



214
215
216
217
# File 'lib/hive/configuration.rb', line 214

def after_fork(&block)
  @after_forks ||= []
  @after_forks << block
end

#before_fork(&block) ⇒ Object



209
210
211
212
# File 'lib/hive/configuration.rb', line 209

def before_fork(&block)
  @before_forks ||= []
  @before_forks << block
end

#chdir(path) ⇒ Object

takes effect immediately



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/hive/configuration.rb', line 161

def chdir(path)
  if ! @root then
    p = File.expand_path(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

#finalizeObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/hive/configuration.rb', line 131

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 = "hive"
    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



122
123
124
125
126
127
128
129
# File 'lib/hive/configuration.rb', line 122

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



117
118
119
120
# File 'lib/hive/configuration.rb', line 117

def load_script(string)
  log "Loading #{string}" if verbose >= 2
  instance_eval(string)
end

#policiesObject



105
106
107
# File 'lib/hive/configuration.rb', line 105

def policies
  PoolEnumerator.new(pools)
end

#require_lib(r) ⇒ Object

convenience for -r on the command line



181
182
183
184
# File 'lib/hive/configuration.rb', line 181

def require_lib(r)
  require(r)
  log "Required #{r}" if verbose >= 2
end

#set_default(key, value) ⇒ Object



186
187
188
189
190
191
192
193
194
# File 'lib/hive/configuration.rb', line 186

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



196
197
198
# File 'lib/hive/configuration.rb', line 196

def set_defaults(options)
  options.each { |k,v| set_default(k,v) }
end

#set_env(env) ⇒ Object


DSL




152
153
154
# File 'lib/hive/configuration.rb', line 152

def set_env(env)
  @env = env
end

#set_name(name) ⇒ Object



156
157
158
# File 'lib/hive/configuration.rb', line 156

def set_name(name)
  @name = name
end