Class: Wolfpack::Runner

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

Overview

Encapsulates a command that is te be split up and run.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(command, args, config_path = nil) ⇒ Runner

Create a command that will run with the give arguments. Optionally a path may be given to a configuration file that sets up a runner.



41
42
43
44
# File 'lib/wolfpack.rb', line 41

def initialize(command, args, config_path = nil)
  @command, @args = command, args
  configure(config_path) if config_path
end

Instance Attribute Details

#after_forkObject

Returns the value of attribute after_fork.



37
38
39
# File 'lib/wolfpack.rb', line 37

def after_fork
  @after_fork
end

#argsObject

Returns the value of attribute args.



37
38
39
# File 'lib/wolfpack.rb', line 37

def args
  @args
end

#commandObject

Returns the value of attribute command.



37
38
39
# File 'lib/wolfpack.rb', line 37

def command
  @command
end

Instance Method Details

#configure(config_path) ⇒ Object

Configures the runner’s by reading a configuration file and eval-ing the ruby.



68
69
70
# File 'lib/wolfpack.rb', line 68

def configure(config_path)
  Configurator.new(self, config_path)
end

#run(processes = nil) ⇒ Object

Run the command ‘n` number of times. Default is the number of processes on the local machine.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/wolfpack.rb', line 48

def run(processes = nil)
  # Sometimes a nil will make it here because of the CLI. This will
  # make sure we have a number to work with.
  processes ||= Wolfpack.processor_count

  # If arguments exist then split args into groups of n processes.
  # Otherwise run commands on every runner
  partions = args ? partition(args, processes) : Array.new(processes){[]}

  # Now run the command with the processes.
  successes = Parallel.map_with_index(partions, :in_processes => processes) do |args, n|
    after_fork.call(n, args) if after_fork
    system @command
  end

  successes.all?
end