Class: Spawner::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/spawner/main.rb

Overview

The main application for the “spawner” command line tool.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeMain

Initializes a new main object.



21
22
23
# File 'lib/spawner/main.rb', line 21

def initialize
  @config = {}
end

Instance Attribute Details

#configObject (readonly)

The configuration options parsed from the command line arguments



17
18
19
# File 'lib/spawner/main.rb', line 17

def config
  @config
end

Class Method Details

.run(args) ⇒ Object

Convenience method for creating and running.



12
13
14
# File 'lib/spawner/main.rb', line 12

def self.run( args )
  new.run( args )
end

Instance Method Details

#parse(args) ⇒ Object

Parse the given array of command line args and return the command to run.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/spawner/main.rb', line 50

def parse( args )
  opts = OptionParser.new
  opts.banner = 'Usage: spawner [options] command'

  opts.separator ''
  opts.on('-s', '--spawn NUMBER', Integer,
          'number of child processes to spawn') {|s| config[:spawn] = s}
  opts.on('-p', '--pause SECONDS', Float,
          'time to wait before re-spawning') {|p| config[:pause] = p}

  opts.separator ''
  opts.on('--stdin FILENAME', String,
          'child process reads input from here') {|fn| config[:stdin] = fn}
  opts.on('--stdout FILENAME', String,
          'child process writes output to here') {|fn| config[:stdout] = fn}
  opts.on('--stderr FILENAME', String,
          'child process writes errors to here') {|fn| config[:stderr] = fn}

  opts.separator ''
  opts.separator 'common options:'

  opts.on_tail( '-h', '--help', 'show this message' ) do
    puts opts
    exit
  end
  opts.on_tail( '--version', 'show version' ) do
    puts "spawner #{::Spawner::VERSION}"
    exit
  end

  opts.parse! args
  if args.empty?
    puts opts
    abort "missing command"
  end
end

#run(args) ⇒ Object

Parse the given command line args and run the command according to the arguments passed in.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/spawner/main.rb', line 28

def run( args )
  parse(args)
  args << config

  spawner = ::Spawner.new(*args)
  Signal.trap('INT') { spawner.stop }

  count = config[:spawn] || 1
  puts "starting #{count} child processes (Ctrl-C to quit)"

  spawner.start
  spawner.join

  puts 'sleeping for 7 seconds'
  sleep 7

  puts 'finished'
end