Class: Patriot::Command::Base

Inherits:
Object
  • Object
show all
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

CommandGroup, ShCommand

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

Instance Method Summary collapse

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

#create_logger

Methods included from Util::Config

#load_config, #load_plugins

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

#eval_attr, #eval_string_attr

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.

Parameters:



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

#configObject

Returns the value of attribute config.



16
17
18
# File 'lib/patriot/command/base.rb', line 16

def config
  @config
end

#parserObject

Returns the value of attribute parser.



16
17
18
# File 'lib/patriot/command/base.rb', line 16

def parser
  @parser
end

#post_processorsObject

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

Parameters:

  • value

    the value to set the attribute start_datetime to.



17
18
19
# File 'lib/patriot/command/base.rb', line 17

def start_datetime=(value)
  @start_datetime = value
end

#target_datetimeObject

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_modeObject

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

Parameters:

  • attr_name (String)

    attribute name

Returns:

  • (Object)

    the value of the attribute specified with argument



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’.

Returns:

  • (String)

    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.

Returns:

  • (String)

    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.

Returns:

  • (Integer)

    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’.

Returns:

  • (String)

    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

Parameters:



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_subcommandObject

add a sub command

Raises:

  • if sub command is not supported



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

Parameters:

  • _param (Hash) (defaults to: {})

    default parameter



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_nameString

set default command name

replace

to handle in JSON format

Returns:

  • (String)

    a simplified command namd



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

#configureObject

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

Parameters:

  • attr (String)

    an attribute name to be configured

  • default_value (defaults to: nil)

    default value of the attribute



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

#descriptionObject

Returns description of this command.

Returns:

  • description of this command



200
201
202
# File 'lib/patriot/command/base.rb', line 200

def description
  self.job_id
end

#executeObject

execute this command

Raises:

  • (NotImplementedError)


205
206
207
# File 'lib/patriot/command/base.rb', line 205

def execute()
  raise NotImplementedError
end

#job_idString

build the identifier of the job for this command. This method should be overriden in sub-classes

Returns:

  • (String)

    the identifier of the job.

Raises:

  • (NotImplementedError)


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.

Parameters:

  • _param (Hash)

    a hash from attribute name to its 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.

Parameters:

  • products (Array<String>)

    a list of 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.

Parameters:

  • requisites (Array<String>)

    a list of 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

#skipObject

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_timeTime

start datetime of this command. This command should be executed after the return value of this method

Returns:

  • (Time)


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

#suspendObject

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_jobPatriot::JobStore::Job

convert this to a job so that it can be stored to JobStore

Returns:



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_attrsObject

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