Class: Patriot::Command::Base
- Inherits:
-
Object
- Object
- Patriot::Command::Base
- Extended by:
- CommandMacro
- Includes:
- Parser, Util::DateUtil, Util::Logger, Util::Param
- Defined in:
- lib/patriot/command/base.rb
Overview
The base class of every command. The command is an executable form of the job.
Direct Known Subclasses
Constant Summary
Constants included from Util::Config
Util::Config::ADMIN_USER_KEY, Util::Config::DEFAULT_CONFIG, Util::Config::DEFAULT_PLUGIN_DIR, Util::Config::INFO_SERVER_PORT_KEY, Util::Config::PASSWORD_KEY, Util::Config::PLUGIN_DIR_KEY, Util::Config::PLUGIN_INIT_SCRIPT, Util::Config::PLUGIN_KEY, Util::Config::PLUGIN_LIB_DIR, Util::Config::USERNAME_KEY, Util::Config::WORKER_HOST_KEY, Util::Config::WORKER_USER_KEY
Instance Attribute Summary collapse
-
#config ⇒ Object
Returns the value of attribute config.
-
#parser ⇒ Object
Returns the value of attribute parser.
-
#post_processors ⇒ Object
Returns the value of attribute post_processors.
-
#start_datetime ⇒ Object
writeonly
Sets the attribute start_datetime.
-
#target_datetime ⇒ Object
Returns the value of attribute target_datetime.
-
#test_mode ⇒ Object
Returns the value of attribute test_mode.
Instance Method Summary collapse
-
#[](attr_name) ⇒ Object
get the value of an attribute.
-
#_date_ ⇒ String
The target date in ‘%Y-%m-%d’.
-
#_HH_ ⇒ String
The tergat hour in %H format.
-
#_hour_ ⇒ Integer
The tergat hour.
-
#_month_ ⇒ String
The target month in ‘%Y-%m’.
-
#add_post_processor(post_processor) ⇒ Object
add a post processor.
-
#add_subcommand ⇒ Object
add a sub command.
-
#build(_param = {}) ⇒ Object
build this command as executables.
-
#command_name ⇒ String
- set default command name replace
-
to handle in JSON format.
-
#configure ⇒ Object
a hook method to implement comand-specific configuration.
-
#configure_attr(attr, default_value = nil) ⇒ Object
configure a command attribute and set as an instance variable.
-
#description ⇒ Object
Description of this command.
-
#execute ⇒ Object
execute this command.
-
#initialize(config) ⇒ Base
constructor
A new instance of Base.
-
#job_id ⇒ String
build the identifier of the job for this command.
-
#param(_param) ⇒ Object
update parameters with a given hash.
-
#produce(products) ⇒ Object
add products produced by this job.
-
#require(requisites) ⇒ Object
add products required by this job.
-
#skip ⇒ Object
mark this job to skip execution.
-
#start_date_time ⇒ Time
start datetime of this command.
-
#suspend ⇒ Object
mark this job to suspend execution.
-
#to_job ⇒ Patriot::JobStore::Job
convert this to a job so that it can be stored to JobStore.
-
#validate_command_attrs ⇒ Object
validate values of command attributes.
Methods included from CommandMacro
add_dsl_function, command_attr, command_attrs, declare_command_name, private_command_attr, serde_attrs, validate_attr, validate_existence, validation_logics, volatile_attr, volatile_attrs
Methods included from Util::Logger
Methods included from Util::Config
Methods included from Util::DateUtil
#date_add, #date_add_year, #date_format, #date_sub, #date_sub_year, #date_to_month, #days_of_month, #days_of_month_until, #days_of_week, #hours, #month_add, #month_sub, #to_date_obj, #to_end_of_last_month, #to_end_of_month, #to_month, #to_start_of_month
Methods included from Util::Param
Methods included from Parser
#batch_macro, #import_erb_config, #load_macro, #new_command, #parse
Constructor Details
#initialize(config) ⇒ Base
Returns a new instance of Base.
29 30 31 32 33 34 35 36 37 |
# 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 |
Instance Attribute Details
#config ⇒ Object
Returns the value of attribute config.
16 17 18 |
# File 'lib/patriot/command/base.rb', line 16 def config @config end |
#parser ⇒ Object
Returns the value of attribute parser.
16 17 18 |
# File 'lib/patriot/command/base.rb', line 16 def parser @parser end |
#post_processors ⇒ Object
Returns the value of attribute post_processors.
16 17 18 |
# File 'lib/patriot/command/base.rb', line 16 def post_processors @post_processors end |
#start_datetime=(value) ⇒ Object (writeonly)
Sets the attribute start_datetime
17 18 19 |
# File 'lib/patriot/command/base.rb', line 17 def start_datetime=(value) @start_datetime = value end |
#target_datetime ⇒ Object
Returns the value of attribute target_datetime.
16 17 18 |
# File 'lib/patriot/command/base.rb', line 16 def target_datetime @target_datetime end |
#test_mode ⇒ Object
Returns the value of attribute test_mode.
16 17 18 |
# File 'lib/patriot/command/base.rb', line 16 def test_mode @test_mode end |
Instance Method Details
#[](attr_name) ⇒ Object
get the value of an attribute
50 51 52 |
# File 'lib/patriot/command/base.rb', line 50 def [](attr_name) return instance_variable_get("@#{attr_name}".to_sym) end |
#_date_ ⇒ String
Returns the target date in ‘%Y-%m-%d’.
98 99 100 |
# File 'lib/patriot/command/base.rb', line 98 def _date_ return @target_datetime.strftime("%Y-%m-%d") end |
#_HH_ ⇒ String
Returns the tergat hour in %H format.
108 109 110 |
# File 'lib/patriot/command/base.rb', line 108 def _HH_ return @target_datetime.strftime("%H") end |
#_hour_ ⇒ Integer
Returns the tergat hour.
103 104 105 |
# File 'lib/patriot/command/base.rb', line 103 def _hour_ return @target_datetime.hour end |
#_month_ ⇒ String
Returns the target month in ‘%Y-%m’.
93 94 95 |
# File 'lib/patriot/command/base.rb', line 93 def _month_ return @target_datetime.strftime("%Y-%m") end |
#add_post_processor(post_processor) ⇒ Object
add a post processor
194 195 196 197 |
# File 'lib/patriot/command/base.rb', line 194 def add_post_processor(post_processor) @post_processors ||= [] @post_processors << post_processor end |
#add_subcommand ⇒ Object
add a sub command
188 189 190 |
# File 'lib/patriot/command/base.rb', line 188 def add_subcommand raise "sub command is not supported" end |
#build(_param = {}) ⇒ Object
build this command as executables
134 135 136 137 138 139 140 141 142 |
# 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 |
#command_name ⇒ String
set default command name
- replace
-
to handle in JSON format
64 65 66 |
# File 'lib/patriot/command/base.rb', line 64 def command_name return self.class.to_s.split("::").last.downcase.gsub(/command/,"") end |
#configure ⇒ Object
a hook method to implement comand-specific configuration
169 170 171 |
# File 'lib/patriot/command/base.rb', line 169 def configure return self end |
#configure_attr(attr, default_value = nil) ⇒ Object
configure a command attribute and set as an instance variable
162 163 164 165 166 |
# 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 |
#description ⇒ Object
Returns description of this command.
200 201 202 |
# File 'lib/patriot/command/base.rb', line 200 def description self.job_id end |
#execute ⇒ Object
execute this command
205 206 207 |
# File 'lib/patriot/command/base.rb', line 205 def execute() raise NotImplementedError end |
#job_id ⇒ String
build the identifier of the job for this command. This method should be overriden in sub-classes
57 58 59 |
# File 'lib/patriot/command/base.rb', line 57 def job_id raise NotImplementedError end |
#param(_param) ⇒ Object
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.
128 129 130 |
# File 'lib/patriot/command/base.rb', line 128 def param(_param) @param = @param.deep_merge(_param) end |
#produce(products) ⇒ Object
add products produced by this job.
77 78 79 80 |
# File 'lib/patriot/command/base.rb', line 77 def produce(products) return if products.nil? @products |= products.flatten end |
#require(requisites) ⇒ Object
add products required by this job.
70 71 72 73 |
# File 'lib/patriot/command/base.rb', line 70 def require(requisites) return if requisites.nil? @requisites |= requisites.flatten end |
#skip ⇒ Object
mark this job to skip execution
83 84 85 |
# File 'lib/patriot/command/base.rb', line 83 def skip param 'state' => Patriot::JobStore::JobState::SUCCEEDED end |
#start_date_time ⇒ Time
start datetime of this command. This command should be executed after the return value of this method
115 116 117 118 119 120 121 122 |
# 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 |
#suspend ⇒ Object
mark this job to suspend execution
88 89 90 |
# File 'lib/patriot/command/base.rb', line 88 def suspend param 'state' => Patriot::JobStore::JobState::SUSPEND end |
#to_job ⇒ Patriot::JobStore::Job
convert this to a job so that it can be stored to JobStore
41 42 43 44 45 |
# 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_command_attrs ⇒ Object
validate values of command attributes
175 176 177 178 179 180 181 182 183 184 |
# 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 |