Class: Pione::RuleEngine::ActionHandler

Inherits:
BasicHandler show all
Defined in:
lib/pione/rule-engine/action-handler.rb

Overview

ActionHandler handles ActionRule.

Constant Summary

Constants included from Log::MessageLog

Log::MessageLog::MESSAGE_QUEUE

Instance Attribute Summary collapse

Attributes inherited from BasicHandler

#base_location, #caller_id, #digest, #domain_id, #domain_location, #dry_run, #env, #inputs, #outputs, #package_id, #param_set, #plain_env, #rule_condition, #rule_definition, #rule_name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BasicHandler

#apply_touch_operation, #create_data_by_touch_operation, #eval_rule_condition, #find_outputs_from_space, #handle, #make_location, #make_output_location, #make_output_tuple, #make_rule_process_record, #make_task_process_record, #publish_outputs, #setup_env, #show_outputs, #update_time_by_touch_operation, #write_data_null

Methods included from Log::MessageLog

#debug_message, #debug_message_begin, #debug_message_end, debug_mode, debug_mode=, debug_mode?, message, quiet_mode, quiet_mode=, quiet_mode?, #show, #user_message, #user_message_begin, #user_message_end

Methods included from TupleSpace::TupleSpaceInterface

#process_log, #processing_error, #set_tuple_space, tuple_space_operation, #tuple_space_server, #with_process_log

Constructor Details

#initialize(param) ⇒ ActionHandler

Returns a new instance of ActionHandler.



12
13
14
15
16
17
# 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

Instance Attribute Details

#shell_scriptObject (readonly)

Returns the value of attribute shell_script.



10
11
12
# File 'lib/pione/rule-engine/action-handler.rb', line 10

def shell_script
  @shell_script
end

#working_directoryObject (readonly)

Returns the value of attribute working_directory.



9
10
11
# File 'lib/pione/rule-engine/action-handler.rb', line 9

def working_directory
  @working_directory
end

Class Method Details

.message_nameObject



5
6
7
# File 'lib/pione/rule-engine/action-handler.rb', line 5

def self.message_name
  "Action"
end

Instance Method Details

#copy_stdout_to_outputs(stdout) ⇒ void

This method returns an undefined value.

Copy stdout file in the working directory to outputs.

Parameters:



78
79
80
81
82
83
84
85
86
87
88
89
# 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

#executeObject

Execute the action.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 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) ⇒ Object

Make output tuple by name.



67
68
69
70
71
# 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_infoObject

Write action environment information file.



105
106
107
108
109
110
# 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_resourcesObject

Move other intermediate files to the domain location.



113
114
115
116
117
118
119
120
121
122
# 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) ⇒ void

This method returns an undefined value.

Write output data with caching.



94
95
96
97
98
99
100
101
102
# 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) ⇒ Object

Writes output tuples into the tuple space server.



125
126
127
# File 'lib/pione/rule-engine/action-handler.rb', line 125

def write_output_tuples(outputs)
  outputs.flatten.compact.each {|output| write(output)}
end