Class: Patriot::JobStore::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/patriot/job_store/job.rb

Overview

a record stored in jobstore

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_id) ⇒ Job

Returns a new instance of Job.

Parameters:

  • job_id (String)

    the identifier of the job



9
10
11
12
13
14
15
# File 'lib/patriot/job_store/job.rb', line 9

def initialize(job_id)
  @job_id     = job_id
  @attributes = {
                  Patriot::Command::PRIORITY_ATTR => Patriot::JobStore::DEFAULT_PRIORITY,
                  Patriot::Command::STATE_ATTR    => Patriot::JobStore::JobState::INIT
                }
end

Instance Attribute Details

#attributesObject

Returns the value of attribute attributes.



6
7
8
# File 'lib/patriot/job_store/job.rb', line 6

def attributes
  @attributes
end

#job_idObject

Returns the value of attribute job_id.



6
7
8
# File 'lib/patriot/job_store/job.rb', line 6

def job_id
  @job_id
end

#update_idObject

Returns the value of attribute update_id.



6
7
8
# File 'lib/patriot/job_store/job.rb', line 6

def update_id
  @update_id
end

Instance Method Details

#[](k) ⇒ Object

get an attribute to this job

Parameters:

  • k (String)

    attribute name

Returns:

  • (Object)

    the attribute value



28
29
30
31
# File 'lib/patriot/job_store/job.rb', line 28

def [](k)
  raise "key #{k} should be symbol but #{k.class}" unless k.is_a?(Symbol)
  return @attributes[k]
end

#[]=(k, v) ⇒ Object

set an attribute to this job

Parameters:

  • k (String)

    attribute name

  • v (Object)

    attribute value



20
21
22
23
# File 'lib/patriot/job_store/job.rb', line 20

def []=(k,v)
  raise "key #{k} should be symbol but #{k.class}" unless k.is_a?(Symbol)
  @attributes[k] = v
end

#_from_stdobj(obj, config) ⇒ Object

convert corresponding objects in the given argument into Command instances.

Parameters:

  • obj (Object)

    a object to be deserialized

  • config (Patriot::util::Config::Base)

    configuration used for deserialization

Returns:

  • (Object)

    a starndard object (primitive or command, or array) for the obj



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/patriot/job_store/job.rb', line 94

def _from_stdobj(obj, config)
  if obj.is_a?(Hash)
    if obj.has_key?(Patriot::Command::COMMAND_CLASS_KEY)
      cmd_cls = obj.delete(Patriot::Command::COMMAND_CLASS_KEY)
      cmd_cls = cmd_cls.split('.').inject(Object){|c,name| c.const_get(name)}
      cmd     = cmd_cls.new(config)
      obj.each do |k,v|
        cmd.instance_variable_set("@#{k}".to_sym, _from_stdobj(v, config))
      end
      return cmd
    elsif obj.has_key?(Patriot::Command::PostProcessor::POST_PROCESSOR_CLASS_KEY)
      cmd_cls = obj.delete(Patriot::Command::PostProcessor::POST_PROCESSOR_CLASS_KEY)
      cmd_cls = cmd_cls.split('.').inject(Object){|c,name| c.const_get(name)}
      return cmd_cls.new(obj)
    else
      hash = {}
      obj.each{|k,v| hash[k.to_s] = _from_stdobj(v, config)}
      return hash
    end
  elsif obj.is_a?(Array)
    return obj.map{|e| _from_stdobj(e, config)}
  else
    return obj
  end
end

#delete(k) ⇒ Object

delete an attribute

Parameters:

  • k (String)

    attribute name

Returns:

  • (Object)

    the deleted attribute value



36
37
38
39
# File 'lib/patriot/job_store/job.rb', line 36

def delete(k)
  raise "key #{k} should be symbol but #{k.class}" unless k.is_a?(Symbol)
  return @attributes.delete(k)
end

#filter_attributes(attrs) ⇒ Hash

Returns a set of attribute name value pairs for specified attributes.

Parameters:

  • attrs (Array<Symbol>)

    a list of attribute names

Returns:

  • (Hash)

    a set of attribute name value pairs for specified attributes



122
123
124
125
126
# File 'lib/patriot/job_store/job.rb', line 122

def filter_attributes(attrs)
  filtered = {}
  attrs.each{|a| filtered[a] = self[a]}
  return filtered
end

#read_command(command) ⇒ Object

read the content of command

Parameters:



43
44
45
46
47
48
49
# File 'lib/patriot/job_store/job.rb', line 43

def read_command(command)
  Patriot::Command::COMMON_ATTRIBUTES.each do |attr|
    value = command.instance_variable_get("@#{attr}".to_sym)
    self[attr] = _to_stdobj(value) unless value.nil?
  end
  _to_stdobj(command).each{|k,v| self[k] = v}
end

#to_command(config) ⇒ Patriot::Command::Base

Returns an executable for this job.

Parameters:

  • config (Patriot::Util::Command::Base)

    configuration for building a command

Returns:



84
85
86
87
# File 'lib/patriot/job_store/job.rb', line 84

def to_command(config)
  raise "configuration is not set" if config.nil?
  return _from_stdobj(self.attributes, config)
end