class Pione::RuleEngine::ActionHandler

ActionHandler handles ActionRule.

Attributes

shell_script[R]
working_directory[R]

Public Class Methods

message_name() click to toggle source
# File lib/pione/rule-engine/action-handler.rb, line 5
def self.message_name
  "Action"
end
new(param) click to toggle source
Calls superclass method
# File lib/pione/rule-engine/action-handler.rb, line 12
def initialize(param)
  super(param)

  @working_directory = WorkingDirectory.new(@env, @base_location, @inputs)
  @shell_script = ActionShellScript.new(@env, @working_directory, @rule_definition, @dry_run)
end

Public Instance Methods

copy_stdout_to_outputs(stdout) click to toggle source

Copy stdout file in the working directory to outputs.

@param stdout [Location]

stdout file

@return [void]

# File lib/pione/rule-engine/action-handler.rb, line 78
def copy_stdout_to_outputs(stdout)
  if stdout.exist?
    @rule_condition.outputs.map do |output|
      condition = output.eval(@env)
      if condition.output_mode == :stdout
        condition.pieces.each do |piece|
          stdout.copy(@working_directory.location + piece.pattern)
        end
      end
    end
  end
end
execute() click to toggle source

Execute the action.

# File lib/pione/rule-engine/action-handler.rb, line 20
def execute
  # prepare input files
  @working_directory.import

  # call shell script
  sh = @shell_script.write
  user_message(["-"*60, sh.split("\n"), "-"*60].flatten, 0, "SH")
  result = @shell_script.call(@sesstion_id, @request_from, @client_ui)
  unless result
    # the case the script has errored
    raise ActionError.new(self, @digest, @shell_script.stderr.read)
  end

  # handle stdout mode of outputs
  copy_stdout_to_outputs(@shell_script.stdout)

  # collect outputs
  output_conditions = @rule_condition.outputs.map {|condition| condition.eval(@env)}
  outputs = @working_directory.collect_outputs(output_conditions).map.with_index do |names, i|
    tuples = names.map {|name| make_output_tuple_with_time(name)}

    # apply touch operation
    apply_touch_operation(output_conditions[i], tuples) || tuples
  end

  # write data null if needed
  outputs.each_with_index do |output, i|
    write_data_null(output_conditions[i], outputs[i], i)
  end

  # write output data
  write_output_data(outputs)
  # write tuples
  write_output_tuples(outputs)
  # write environment info
  write_env_info
  # write other resources
  write_other_resources

  # clear working directory
  @working_directory.close

  # return tuples
  return outputs
end
make_output_tuple_with_time(name) click to toggle source

Make output tuple by name.

# File lib/pione/rule-engine/action-handler.rb, line 67
def make_output_tuple_with_time(name)
  time = (@working_directory.location + name).mtime
  location = make_output_location(name)
  TupleSpace::DataTuple.new(name: name, domain: @domain_id, location: location, time: time)
end
write_env_info() click to toggle source

Write action environment information file.

# File lib/pione/rule-engine/action-handler.rb, line 105
def write_env_info
  @env.variable_table.keys.map do |var|
    val = @env.variable_get(var)
    "%s: %s" % [var.name, val.textize]
  end.tap {|x| (@working_directory.location + ".pione-env").create(x.join("\n"))}
end
write_other_resources() click to toggle source

Move other intermediate files to the domain location.

# File lib/pione/rule-engine/action-handler.rb, line 113
def write_other_resources
  @working_directory.location.file_entries.each do |entry|
    location = make_location(entry.path.basename, @domain_id)
    begin
      entry.move(location)
    rescue => e
      Log::SystemLog.warn("cannot move %s to %s: %s" % [entry.path, location, e.message])
    end
  end
end
write_output_data(outputs) click to toggle source

Write output data with caching.

@return [void]

# File lib/pione/rule-engine/action-handler.rb, line 94
def write_output_data(outputs)
  outputs.flatten.compact.each do |output|
    src = @working_directory.location + output.name
    dest = output.location
    System::FileCache.put(src, dest)
    # copy the data to the caller's domain in file server
    src.copy(dest)
  end
end
write_output_tuples(outputs) click to toggle source

Writes output tuples into the tuple space server.

# File lib/pione/rule-engine/action-handler.rb, line 125
def write_output_tuples(outputs)
  outputs.flatten.compact.each {|output| write(output)}
end