class Patriot::Command::Base
The base class of every command. The command is an executable form of the job.
Attributes
Public Class Methods
@param config [Patriot::Util::Config::Base] configuration for this command
# File lib/patriot/command/base.rb, line 29 def initialize(config) @config = config @logger = create_logger(config) @param = {} @requisites = [] @products = [] @macros = {} @test_mode = false end
Public Instance Methods
get the value of an attribute @param attr_name [String] attribute name @return [Object] the value of the attribute specified with argument
# File lib/patriot/command/base.rb, line 50 def [](attr_name) return instance_variable_get("@#{attr_name}".to_sym) end
@return [String] the tergat hour in %H format
# File lib/patriot/command/base.rb, line 108 def _HH_ return @target_datetime.strftime("%H") end
@return [String] the target date in '%Y-%m-%d'
# File lib/patriot/command/base.rb, line 98 def _date_ return @target_datetime.strftime("%Y-%m-%d") end
@return [Integer] the tergat hour
# File lib/patriot/command/base.rb, line 103 def _hour_ return @target_datetime.hour end
@return [String] the target month in '%Y-%m'
# File lib/patriot/command/base.rb, line 93 def _month_ return @target_datetime.strftime("%Y-%m") end
add a post processor @param [Patriot::Command::PostProcessor::Base] a post processor for this job
# File lib/patriot/command/base.rb, line 194 def add_post_processor(post_processor) @post_processors ||= [] @post_processors << post_processor end
add a sub command @raise if sub command is not supported
# File lib/patriot/command/base.rb, line 188 def add_subcommand raise "sub command is not supported" end
build this command as executables @param _param [Hash] default parameter
# File lib/patriot/command/base.rb, line 134 def build(_param={}) @param = _param.deep_merge(@param) init_param @start_datetime = start_date_time cmds = configure() cmds = [cmds] unless cmds.is_a?(Array) cmds.each(&:validate_command_attrs) return cmds.flatten end
set default command name
- replace
-
to handle in JSON format
@return [String] a simplified command namd
# File lib/patriot/command/base.rb, line 64 def command_name return self.class.to_s.split("::").last.downcase.gsub(/command/,"") end
a hook method to implement comand-specific configuration
# File lib/patriot/command/base.rb, line 169 def configure return self end
configure a command attribute and set as an instance variable @param attr [String] an attribute name to be configured @param default_value default value of the attribute
# File lib/patriot/command/base.rb, line 162 def configure_attr(attr, default_value = nil) v = instance_variable_get("@#{attr}".to_sym) v = default_value if v.nil? instance_variable_set("@#{attr}".to_sym, eval_attr(v)) end
@return description of this command
# File lib/patriot/command/base.rb, line 200 def description self.job_id end
execute this command
# File lib/patriot/command/base.rb, line 205 def execute() raise NotImplementedError end
build the identifier of the job for this command. This method should be overriden in sub-classes @return [String] the identifier of the job.
# File lib/patriot/command/base.rb, line 57 def job_id raise NotImplementedError end
update parameters with a given hash. If the hash includes keys which values have already been defined, the value for the key is replaced with the new value. @param _param [Hash] a hash from attribute name to its value
# File lib/patriot/command/base.rb, line 128 def param(_param) @param = @param.deep_merge(_param) end
add products produced by this job. @param products [Array<String>] a list of products produced by this job
# File lib/patriot/command/base.rb, line 77 def produce(products) return if products.nil? @products |= products.flatten end
add products required by this job. @param requisites [Array<String>] a list of products required by this job.
# File lib/patriot/command/base.rb, line 70 def require(requisites) return if requisites.nil? @requisites |= requisites.flatten end
mark this job to skip execution
# File lib/patriot/command/base.rb, line 83 def skip param 'state' => Patriot::JobStore::JobState::SUCCEEDED end
start datetime of this command. This command should be executed after the return value of this method @return [Time]
# File lib/patriot/command/base.rb, line 115 def start_date_time return @start_datetime if @exec_date.nil? && @start_after.nil? # set tomorrow as default date = (@exec_date || date_add(_date_, 1)).split("-").map(&:to_i) # set midnight as default time = (@start_after || "00:00:00").split(":").map(&:to_i) return Time.new(date[0], date[1], date[2], time[0], time[1], time[2]) end
mark this job to suspend execution
# File lib/patriot/command/base.rb, line 88 def suspend param 'state' => Patriot::JobStore::JobState::SUSPEND end
convert this to a job so that it can be stored to JobStore
@return [Patriot::JobStore::Job] a job for this command.
# File lib/patriot/command/base.rb, line 41 def to_job job = Patriot::JobStore::Job.new(self.job_id) job.read_command(self) return job end
validate values of command attributes @see Patriot::Command::CommandMacro#validate_attr
# File lib/patriot/command/base.rb, line 175 def validate_command_attrs self.class.validation_logics.each do |attr, logics| val = self.instance_variable_get("@#{attr}".to_sym) logics.each do |l| unless l.call(self, attr, val) raise "validation error : #{attr}=#{val} (#{self.class})" end end end end
Protected Instance Methods
initialize command attributes
# File lib/patriot/command/base.rb, line 145 def init_param # set parameter value to instance variable @param.each do |k,v| raise "a reserved word #{k} is used as parameter name" if Patriot::Command::COMMAND_CLASS_KEY == k.to_sym raise "#{k} is already used in #{self.job_id}" unless instance_variable_get("@#{k}".to_sym).nil? # don't evaluate here since all parameters are not set to instance variables instance_variable_set("@#{k}".to_sym,v) end # evaluate command attributes using parameters (instance variables) self.class.command_attrs.each { |a, d| configure_attr(a, d) } end