Class: Pione::Command::BasicCommand
- Inherits:
-
Object
- Object
- Pione::Command::BasicCommand
- Defined in:
- lib/pione/command/basic-command.rb
Overview
BasicCommand provides PIONE command model. PIONE commands have 4 phases: “init”, “setup”, “action”, “termination”. Concrete commands implement some processings as each phases.
Direct Known Subclasses
PioneAction, PioneActionList, PioneBroker, PioneClean, PioneClient, PioneCommand, PioneCompiler, PioneLog, PionePackage, PioneRelay, PioneRelayAccountDB, PioneRelayClientDB, PioneSyntaxChecker, PioneTaskWorker, PioneTupleSpaceProvider, PioneTupleSpaceReceiver, PioneTupleSpaceViewer, PioneUpdatePackageInfo, PioneVal
Class Attribute Summary collapse
-
.command_front_block ⇒ Object
readonly
Returns the value of attribute command_front_block.
-
.command_name_block ⇒ Object
readonly
Returns the value of attribute command_name_block.
-
.exception_handler ⇒ Object
readonly
Returns the value of attribute exception_handler.
-
.execution_actions ⇒ Object
readonly
Returns the value of attribute execution_actions.
-
.init_actions ⇒ Object
readonly
Returns the value of attribute init_actions.
-
.option_definition ⇒ Object
readonly
Returns the value of attribute option_definition.
-
.phase_option ⇒ Object
readonly
Returns the value of attribute phase_option.
-
.setup_actions ⇒ Object
readonly
Returns the value of attribute setup_actions.
-
.termination_actions ⇒ Object
readonly
Returns the value of attribute termination_actions.
Instance Attribute Summary collapse
-
#option ⇒ Object
readonly
Returns the value of attribute option.
-
#running_thread ⇒ Object
readonly
Returns the value of attribute running_thread.
Class Method Summary collapse
-
.command_banner(banner = nil) ⇒ Object
Set program banner or return the banner.
-
.command_front(front_class = nil, &b) ⇒ Object
Set command front or return the front class.
-
.command_name(name = nil, &b) ⇒ Object
Set progaram name or return the name.
-
.execute(action, option = {}) ⇒ Object
Register the action to execution phase.
-
.execution_phase(option) ⇒ Object
Set execution phase options.
- .handle_exception(phase_name, exceptions, &action) ⇒ Object
- .handle_execution_exception(*exceptions, &action) ⇒ Object
- .handle_setup_exception(*exceptions, &action) ⇒ Object
- .handle_termination_exception(*exceptions, &action) ⇒ Object
- .inherited(subclass) ⇒ Object
-
.init(action, option = {}) ⇒ Object
Register the action to init phase.
-
.init_phase(option) ⇒ Object
Set setup phase options.
- .option_parser_mode(mode) ⇒ Object
-
.run(argv) ⇒ Object
Run the command with the arguments.
-
.setup(action, option = {}) ⇒ Object
Register the action to setup phase.
-
.setup_phase(option) ⇒ Object
Set setup phase options.
-
.terminate(action, option = {}) ⇒ Object
Register the action to termination phase.
-
.termination_phase(option) ⇒ Object
Set termination phase options.
Instance Method Summary collapse
-
#abort(msg_or_exception, pos = caller(1).first) ⇒ Object
Exit running command and return failure status.
-
#enter_phase(phase_name) ⇒ Object
Enter setup phase.
-
#execution? ⇒ Boolean
Return true if it is in execution phase.
-
#exit ⇒ Object
Exit running command and return status.
-
#init? ⇒ Boolean
Return true if it is in init phase.
-
#initialize(argv) ⇒ BasicCommand
constructor
A new instance of BasicCommand.
-
#run ⇒ Object
Run 4 phase lifecycle of the command.
-
#setup? ⇒ Boolean
Return true if it is in setup phase.
-
#terminate ⇒ Object
Terminate the command.
-
#termination? ⇒ Boolean
Return true if it is in termination phase.
Constructor Details
#initialize(argv) ⇒ BasicCommand
Returns a new instance of BasicCommand.
164 165 166 167 168 169 170 171 172 173 |
# File 'lib/pione/command/basic-command.rb', line 164 def initialize(argv) @argv = argv @option = {} @__exit_status__ = true @__phase_name__ = nil @__action_name__ = nil # process has just one command object Global.command = self end |
Class Attribute Details
.command_front_block ⇒ Object (readonly)
Returns the value of attribute command_front_block.
11 12 13 |
# File 'lib/pione/command/basic-command.rb', line 11 def command_front_block @command_front_block end |
.command_name_block ⇒ Object (readonly)
Returns the value of attribute command_name_block.
10 11 12 |
# File 'lib/pione/command/basic-command.rb', line 10 def command_name_block @command_name_block end |
.exception_handler ⇒ Object (readonly)
Returns the value of attribute exception_handler.
16 17 18 |
# File 'lib/pione/command/basic-command.rb', line 16 def exception_handler @exception_handler end |
.execution_actions ⇒ Object (readonly)
Returns the value of attribute execution_actions.
14 15 16 |
# File 'lib/pione/command/basic-command.rb', line 14 def execution_actions @execution_actions end |
.init_actions ⇒ Object (readonly)
Returns the value of attribute init_actions.
12 13 14 |
# File 'lib/pione/command/basic-command.rb', line 12 def init_actions @init_actions end |
.option_definition ⇒ Object (readonly)
Returns the value of attribute option_definition.
8 9 10 |
# File 'lib/pione/command/basic-command.rb', line 8 def option_definition @option_definition end |
.phase_option ⇒ Object (readonly)
Returns the value of attribute phase_option.
9 10 11 |
# File 'lib/pione/command/basic-command.rb', line 9 def phase_option @phase_option end |
.setup_actions ⇒ Object (readonly)
Returns the value of attribute setup_actions.
13 14 15 |
# File 'lib/pione/command/basic-command.rb', line 13 def setup_actions @setup_actions end |
.termination_actions ⇒ Object (readonly)
Returns the value of attribute termination_actions.
15 16 17 |
# File 'lib/pione/command/basic-command.rb', line 15 def termination_actions @termination_actions end |
Instance Attribute Details
#option ⇒ Object (readonly)
Returns the value of attribute option.
158 159 160 |
# File 'lib/pione/command/basic-command.rb', line 158 def option @option end |
#running_thread ⇒ Object (readonly)
Returns the value of attribute running_thread.
159 160 161 |
# File 'lib/pione/command/basic-command.rb', line 159 def running_thread @running_thread end |
Class Method Details
.command_banner(banner = nil) ⇒ Object
Set program banner or return the banner.
57 58 59 60 61 62 63 |
# File 'lib/pione/command/basic-command.rb', line 57 def (=nil) if @command_banner = else @command_banner end end |
.command_front(front_class = nil, &b) ⇒ Object
Set command front or return the front class.
66 67 68 69 70 71 72 73 |
# File 'lib/pione/command/basic-command.rb', line 66 def command_front(front_class=nil, &b) if front_class @command_front = front_class @command_front_block = b else @command_front end end |
.command_name(name = nil, &b) ⇒ Object
Set progaram name or return the name.
47 48 49 50 51 52 53 54 |
# File 'lib/pione/command/basic-command.rb', line 47 def command_name(name=nil, &b) if name @command_name = name @command_name_block = block_given? ? b : nil else @command_name end end |
.execute(action, option = {}) ⇒ Object
Register the action to execution phase.
117 118 119 |
# File 'lib/pione/command/basic-command.rb', line 117 def execute(action, option={}) register_action(@execution_actions, action, option) end |
.execution_phase(option) ⇒ Object
Set execution phase options.
97 98 99 |
# File 'lib/pione/command/basic-command.rb', line 97 def execution_phase(option) set_phase_option(:execution, option) end |
.handle_exception(phase_name, exceptions, &action) ⇒ Object
126 127 128 |
# File 'lib/pione/command/basic-command.rb', line 126 def handle_exception(phase_name, exceptions, &action) exceptions.each {|e| @exception_handler[phase_name][e] = action} end |
.handle_execution_exception(*exceptions, &action) ⇒ Object
134 135 136 |
# File 'lib/pione/command/basic-command.rb', line 134 def handle_execution_exception(*exceptions, &action) handle_exception(:execution, exceptions, &action) end |
.handle_setup_exception(*exceptions, &action) ⇒ Object
130 131 132 |
# File 'lib/pione/command/basic-command.rb', line 130 def handle_setup_exception(*exceptions, &action) handle_exception(:setup, exceptions, &action) end |
.handle_termination_exception(*exceptions, &action) ⇒ Object
138 139 140 |
# File 'lib/pione/command/basic-command.rb', line 138 def handle_termination_exception(*exceptions, &action) handle_exception(:termination, exceptions, &action) end |
.inherited(subclass) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/pione/command/basic-command.rb', line 18 def inherited(subclass) subclass.instance_eval do @phase_option = {:init => {}, :setup => {}, :execution => {}, :termination => {}} @option_definition = OptionDefinition.new @command_name = nil @command_name_block = nil @command_banner = nil @command_front = nil @command_front_block = nil @init_actions = Array.new @setup_actions = Array.new @execution_actions = Array.new @termination_actions = Array.new @exception_handler = {:init => {}, :setup => {}, :execution => {}, :termination => {}} # define init phase actions init :process_name init :signal_trap init :option init :front init :process_additional_information end end |
.init(action, option = {}) ⇒ Object
Register the action to init phase.
107 108 109 |
# File 'lib/pione/command/basic-command.rb', line 107 def init(action, option={}) register_action(@init_actions, action, option) end |
.init_phase(option) ⇒ Object
Set setup phase options.
87 88 89 |
# File 'lib/pione/command/basic-command.rb', line 87 def init_phase(option) set_phase_option(:init, option) end |
.option_parser_mode(mode) ⇒ Object
42 43 44 |
# File 'lib/pione/command/basic-command.rb', line 42 def option_parser_mode(mode) @option_definition.parser_mode = mode end |
.run(argv) ⇒ Object
Run the command with the arguments.
82 83 84 |
# File 'lib/pione/command/basic-command.rb', line 82 def run(argv) self.new(argv).run end |
.setup(action, option = {}) ⇒ Object
Register the action to setup phase.
112 113 114 |
# File 'lib/pione/command/basic-command.rb', line 112 def setup(action, option={}) register_action(@setup_actions, action, option) end |
.setup_phase(option) ⇒ Object
Set setup phase options.
92 93 94 |
# File 'lib/pione/command/basic-command.rb', line 92 def setup_phase(option) set_phase_option(:setup, option) end |
.terminate(action, option = {}) ⇒ Object
Register the action to termination phase.
122 123 124 |
# File 'lib/pione/command/basic-command.rb', line 122 def terminate(action, option={}) register_action(@termination_actions, action, option) end |
.termination_phase(option) ⇒ Object
Set termination phase options.
102 103 104 |
# File 'lib/pione/command/basic-command.rb', line 102 def termination_phase(option) set_phase_option(:termination, option) end |
Instance Method Details
#abort(msg_or_exception, pos = caller(1).first) ⇒ Object
Exit running command and return failure status. Note that this method enters termination phase before it exits.
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
# File 'lib/pione/command/basic-command.rb', line 260 def abort(msg_or_exception, pos=caller(1).first) # hide the message because some option errors are meaningless invisible = msg_or_exception.is_a?(HideableOptionError) # setup abortion message msg = msg_or_exception.is_a?(Exception) ? msg_or_exception. : msg_or_exception # show the message if invisible Log::Debug.system(msg, pos) else Log::SystemLog.fatal(msg, pos) end # set exit status code Global.exit_status = false # go to termination phase terminate end |
#enter_phase(phase_name) ⇒ Object
Enter setup phase.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/pione/command/basic-command.rb', line 185 def enter_phase(phase_name) # avoid double launch return if @__phase_name__ == phase_name # show debug message for entering phase Log::Debug.system("%s enters phase \"%s\"" % [command_name, phase_name]) limit = self.class.phase_option[phase_name][:timeout] phase_keyword, actions = find_phase_actions(phase_name) timeout(limit) do @__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])) next end # show debug message for firing action Log::Debug.system("%s fires action \"%s\" in phase \"%s\"" % [command_name, action_name, phase_name]) # fire action @__action_name__ = action_name full_action_name = ("%s_%s" % [phase_keyword, action_name]).to_sym if action_option[:module] # call action in command action module instance_eval(&action_option[:module].get(full_action_name)) else # call action in self object method(full_action_name).call end end end rescue *self.class.exception_handler[phase_name].keys => e self.class.exception_handler[phase_name][e.class].call(self, e) rescue Timeout::Error args = [command_name, @__action_name__, @__phase_name__, limit] abort("%s timeouted at action \"%s\" in phase \"%s\". (%i sec)" % args) end |
#execution? ⇒ Boolean
Return true if it is in execution phase.
242 243 244 |
# File 'lib/pione/command/basic-command.rb', line 242 def execution? @__phase_name__ == :execution end |
#exit ⇒ Object
Exit running command and return status.
252 253 254 255 256 |
# File 'lib/pione/command/basic-command.rb', line 252 def exit Log::Debug.system("%s exits with status \"%s\"" % [command_name, @__exit_status__]) Global.system_logger.terminate Kernel.exit(Global.exit_status) end |
#init? ⇒ Boolean
Return true if it is in init phase.
232 233 234 |
# File 'lib/pione/command/basic-command.rb', line 232 def init? @__phase_name__ == :init end |
#run ⇒ Object
Run 4 phase lifecycle of the command. This fires actions in each phase.
176 177 178 179 180 181 182 |
# File 'lib/pione/command/basic-command.rb', line 176 def run @running_thread = Thread.current enter_phase(:init) enter_phase(:setup) enter_phase(:execution) terminate # => enter_phase(:termination) and exit end |
#setup? ⇒ Boolean
Return true if it is in setup phase.
237 238 239 |
# File 'lib/pione/command/basic-command.rb', line 237 def setup? @__phase_name__ == :setup end |
#terminate ⇒ Object
Terminate the command. Note that this enters in termination phase first, and command exit.
226 227 228 229 |
# File 'lib/pione/command/basic-command.rb', line 226 def terminate enter_phase(:termination) exit # end with status code end |
#termination? ⇒ Boolean
Return true if it is in termination phase.
247 248 249 |
# File 'lib/pione/command/basic-command.rb', line 247 def termination? @__phase_name__ == :termination end |