Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/pione-config' into fix-test-prob…
Browse files Browse the repository at this point in the history
…lems

Conflicts:
	lib/pione/test-helper/command-helper.rb
	test/command/spec_pione-client.rb
  • Loading branch information
keita committed Dec 6, 2013
2 parents 9cd9200 + bc4be69 commit 82ae97d
Show file tree
Hide file tree
Showing 23 changed files with 561 additions and 113 deletions.
1 change: 1 addition & 0 deletions lib/pione/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ module Command; end
require 'pione/command/pione-update-package-info' # pione update-package-info
require 'pione/command/pione-action' # pione action
require 'pione/command/pione-action-list' # pione action:list
require 'pione/command/pione-config'
27 changes: 21 additions & 6 deletions lib/pione/command/basic-command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ def register_action(phase_actions, action, option={})

attr_reader :option
attr_reader :running_thread
attr_accessor :action_type

forward! :class, :option_definition, :command_name, :command_name_block
forward! :class, :command_banner, :command_front, :command_front_block
Expand All @@ -167,11 +168,20 @@ def initialize(argv)
@__exit_status__ = true
@__phase_name__ = nil
@__action_name__ = nil
@action_type = nil

# process has just one command object
Global.command = self
end

# Return current phase name.
#
# @return [Symbol]
# :init, :setup, :execution, or :termination
def current_phase
@__phase_name__
end

# Run 4 phase lifecycle of the command. This fires actions in each phase.
def run
@running_thread = Thread.current
Expand All @@ -195,7 +205,7 @@ def enter_phase(phase_name)
@__phase_name__ = phase_name
actions.each do |(targets, action_name, action_option)|
# check current mode is target or not
if not(targets.empty?) and not(targets.include?(option[:action_mode]))
if not(targets.empty?) and not(targets.include?(@action_type))
next
end

Expand Down Expand Up @@ -301,7 +311,7 @@ def init_signal_trap

# Initialize command options.
def init_option
@option = option_definition.parse(@argv, command_name, command_banner)
@option = option_definition.parse(@argv, self)
rescue OptionParser::ParseError, OptionError => e
abort(e)
end
Expand Down Expand Up @@ -355,7 +365,7 @@ module CommonCommandAction
define_action(:terminate_child_process) do |cmd|
if Global.front
# send signal TERM to the child process
Global.front.child.each do |pid, uri|
Global.front.child_pids.each do |pid|
Util.ignore_exception {Process.kill(:TERM, pid)}
end

Expand All @@ -368,14 +378,19 @@ module CommonCommandAction
end

define_action(:setup_parent_process_connection) do |cmd|
cmd.option[:parent_front].add_child(Process.pid, Global.front.uri)
ParentFrontWatchDog.new(self) # start to watch parent process
begin
cmd.option[:parent_front].register_child(Process.pid, Global.front.uri)
ParentFrontWatchDog.new(self) # start to watch parent process
rescue Front::ChildRegistrationError
# terminate if the registration failed
Global.command.terminate
end
end

define_action(:terminate_parent_process_connection) do |cmd|
# maybe parent process is dead in this timing
Util.ignore_exception do
cmd.option[:parent_front].remove_child(Process.pid)
cmd.option[:parent_front].unregister_child(Process.pid)
end
end
end
Expand Down
42 changes: 21 additions & 21 deletions lib/pione/command/option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,25 +57,25 @@ def item(name)
end

# Parse the command options.
def parse(argv, command_name, command_banner)
def parse(argv, cmd)
data = Hash.new

# parse options
OptionParser.new do |opt|
# set banner
opt.banner = "Usage: %s [options]" % command_name
opt.banner << "\n\n" + command_banner + "\n" if command_banner
opt.banner = "Usage: %s [options]" % cmd.command_name
opt.banner << "\n\n" + cmd.command_banner + "\n" if cmd.command_banner

# set version
opt.program_name = command_name
opt.program_name = cmd.command_name
opt.version = Pione::VERSION

# default values
@items.each {|item| data[item.name] = item.default if item.default}
data.merge!(@default)

# setup option parser
@items.sort{|a,b| a.long <=> b.long}.each {|item| setup_item(command_name, opt, data, item)}
@items.sort{|a,b| a.long <=> b.long}.each {|item| setup_item(cmd, opt, data, item)}
end.send(@parser_mode, argv)

# check option's validness
Expand All @@ -96,28 +96,28 @@ def validate(&b)
private

# Setup the option item.
def setup_item(command_name, opt, data, item)
def setup_item(cmd, opt, data, item)
defs = [item.short, item.long, item.desc].compact
[ :setup_item_action,
:setup_item_values,
:setup_item_value,
:setup_item_static_value
].find {|method_name| send(method_name, command_name, opt, data, item, defs)}
].find {|method_name| send(method_name, cmd, opt, data, item, defs)}
end

def setup_item_action(command_name, opt, data, item, defs)
def setup_item_action(cmd, opt, data, item, defs)
if item.action
opt.on(*defs, Proc.new{|*args| self.instance_exec(command_name, data, *args, &item.action)})
opt.on(*defs, Proc.new{|*args| self.instance_exec(cmd, data, *args, &item.action)})
end
end

def setup_item_values(command_name, opt, data, item, defs)
def setup_item_values(cmd, opt, data, item, defs)
if item.values.kind_of?(Proc)
opt.on(*defs, Proc.new{|*args| data[item.name] << self.instance_exec(*args, &item.values)})
end
end

def setup_item_value(command_name, opt, data, item, defs)
def setup_item_value(cmd, opt, data, item, defs)
case item.value
when Proc
opt.on(*defs, Proc.new{|*args| data[item.name] = self.instance_exec(*args, &item.value)})
Expand All @@ -126,7 +126,7 @@ def setup_item_value(command_name, opt, data, item, defs)
end
end

def setup_item_static_value(command_name, opt, data, item, defs)
def setup_item_static_value(cmd, opt, data, item, defs)
if item.value
opt.on(*defs, Proc.new{ data[item.name] = item.value})
end
Expand Down Expand Up @@ -169,7 +169,7 @@ module CommonOption
define(:debug) do |item|
item.long = '--debug[=TYPE]'
item.desc = "turn on debug mode about the type(system / rule_engine / ignored_exception / presence_notifier / communication)"
item.action = proc {|command_nane, _, type|
item.action = proc {|cmd, _, type|
Global.system_logger.level = :debug
case type
when "system", nil
Expand All @@ -191,35 +191,35 @@ module CommonOption
define(:features) do |item|
item.long = '--features=FEATURES'
item.desc = 'set features'
item.action = proc {|command_name, option, features|
item.action = proc {|cmd, option, features|
begin
# store features
Global.features = features
rescue Parslet::ParseFailed => e
raise OptionError.new(
"invalid feature expression \"%s\" is given for %s" % [features, command_name]
"invalid feature expression \"%s\" is given for %s" % [features, cmd.command_name]
)
end
}
end

define(:my_ip_address) do |item|
item.long = "--my-ip-address=ADDRESS"
item.desc = "set my IP address"
item.action = proc {|_, _, address| Global.my_ip_address = address}
define(:communication_address) do |item|
item.long = "--communication-address=ADDRESS"
item.desc = "set IP address for interprocess communication"
item.action = proc {|_, _, address| Global.communication_address = address}
end

define(:parent_front) do |item|
item.long = '--parent-front=URI'
item.desc = 'set parent front URI'
item.requisite = true
item.action = proc do |command_name, option, uri|
item.action = proc do |cmd, option, uri|
begin
option[:parent_front] = DRbObject.new_with_uri(uri)
timeout(1) {option[:parent_front].ping}
rescue Exception => e
raise HideableOptionError.new(
"%s couldn't connect to parent front \"%s\": %s" % [command_name, uri, e.message]
"%s couldn't connect to parent front \"%s\": %s" % [cmd.command_name, uri, e.message]
)
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/pione/command/pione-broker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class PioneBroker < BasicCommand
use_option :daemon
use_option :debug
use_option :features
use_option :my_ip_address
use_option :communication_address
use_option :task_worker

validate_option do |option|
Expand Down
55 changes: 23 additions & 32 deletions lib/pione/command/pione-client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,11 @@ class PioneClient < BasicCommand

use_option :debug
use_option :color
use_option :my_ip_address
use_option :communication_address
use_option :presence_notification_address
use_option :task_worker
use_option :features

option_default(:action_mode, :process_job)

define_option(:input_location) do |item|
item.short = '-i LOCATION'
item.long = '--input=LOCATION'
Expand All @@ -43,14 +41,14 @@ class PioneClient < BasicCommand
item.long = '--output=LOCATION'
item.desc = 'set output directory'
item.default = Location["local:./output/"]
item.action = proc do |command_name, option, uri|
item.action = proc do |cmd, option, uri|
begin
option[:output_location] = Location[uri]
if URI.parse(uri).scheme == "myftp"
option[:myftp] = URI.parse(uri).normalize
end
rescue ArgumentError
raise OptionError.new("output location '%s' is bad in %s" % [uri, command_name])
raise OptionError.new("output location '%s' is bad in %s" % [uri, cmd.command_name])
end
end
end
Expand All @@ -73,11 +71,11 @@ class PioneClient < BasicCommand
item.long = '--params="{Var:1,...}"'
item.desc = "set user parameters"
item.default = Lang::ParameterSetSequence.new
item.action = proc do |command_name, option, str|
item.action = proc do |cmd, option, str|
begin
option[:params] = option[:params].merge(Util.parse_param_set(str))
rescue Parslet::ParseFailed => e
raise OptionError.new("invalid parameters \"%s\" in %s" % [str, command_name])
raise OptionError.new("invalid parameters \"%s\" in %s" % [str, cmd.command_name])
end
end
end
Expand Down Expand Up @@ -108,12 +106,6 @@ class PioneClient < BasicCommand
item.value = proc {|uri| uri}
end

define_option(:list_params) do |item|
item.long = '--list-params'
item.desc = 'show user parameter list in the document'
item.action = proc {|_, option| option[:action_mode] = :list_params}
end

define_option(:rehearse) do |item|
item.long = '--rehearse [SCENARIO]'
item.desc = 'rehearse the scenario'
Expand Down Expand Up @@ -295,23 +287,14 @@ def start_relay_connection
# command lifecycle: execution phase
#

# mode "list params"
execute :list_params => :list_params

# mode "process_job"
execute :process_job => :job_terminator
execute :process_job => :messenger
execute :process_job => :logger
execute :process_job => :input_generator
execute :process_job => :tuple_space_provider
execute :process_job => :task_worker
execute :process_job => :process_manager
execute :process_job => :check_rehearsal_result

# Print list of user parameters.
def execute_list_params
Util::PackageParametersList.print(@env, @env.current_package_id)
end
execute :job_terminator
execute :messenger
execute :logger
execute :input_generator
execute :tuple_space_provider
execute :task_worker
execute :process_manager
execute :check_rehearsal_result

def execute_job_terminator
@job_terminator = Agent::JobTerminator.start(@tuple_space) do |status|
Expand Down Expand Up @@ -352,7 +335,11 @@ def execute_tuple_space_provider
end
@tuple_space_provider = spawner.child_front
rescue SpawnError => e
abort(e.message)
if termination?
Log::Debug.system(e.message)
else
abort(e.message)
end
end
end
@spawner_threads.add(thread)
Expand All @@ -372,7 +359,11 @@ def execute_task_worker
begin
Command::PioneTaskWorker.spawn(Global.features, @tuple_space.uuid)
rescue SpawnError => e
abort(e.message)
if termination?
Log::Debug.system(e.message)
else
abort(e.message)
end
end
end
end
Expand Down

0 comments on commit 82ae97d

Please sign in to comment.