Class: PBS::Query

Inherits:
Object
  • Object
show all
Defined in:
lib/pbs/query.rb

Constant Summary collapse

STATTYPE =
{job: :pbs_statjob, queue: :pbs_statque,
node: :pbs_statnode, server: :pbs_statserver}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Query

Needs a connection object and a query type Query types: :job, :queue, :server, :node



12
13
14
15
16
# File 'lib/pbs/query.rb', line 12

def initialize(args = {})
  @conn = args[:conn] || Conn.new
  @type = args[:type] || :job
  @where_procs = []
end

Instance Attribute Details

#connObject (readonly)

Returns the value of attribute conn.



4
5
6
# File 'lib/pbs/query.rb', line 4

def conn
  @conn
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/pbs/query.rb', line 3

def type
  @type
end

#where_procsObject

Returns the value of attribute where_procs.



5
6
7
# File 'lib/pbs/query.rb', line 5

def where_procs
  @where_procs
end

Instance Method Details

#_filter_where_values(array) ⇒ Object

Filter an array of hashes based on the defined where procs Comparisons are done inside the :attribs hash only



72
73
74
75
76
77
78
79
80
# File 'lib/pbs/query.rb', line 72

def _filter_where_values(array)
  array.select do |hash|
    pass = true
    where_procs.each do |p|
      pass = false unless p.call(hash[:attribs])
    end
    pass
  end
end

#_pbs_batchstat(id, filters) ⇒ Object

Connect, get status on batch server, disconnect, parse output, and finally check for errors Don’t forget to free up memory the C-library creates



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/pbs/query.rb', line 85

def _pbs_batchstat(id, filters)
  # Generate attribute list from filter list
  attrib_list = PBS::Torque::Attrl.from_list(filters) if filters

  batch_status = nil
  conn.connect unless conn.connected?
  if type == :server
    batch_status = Torque.send(STATTYPE[type], conn.conn_id, attrib_list, nil)
  else
    batch_status = Torque.send(STATTYPE[type], conn.conn_id, id, attrib_list, nil)
  end
  conn.disconnect
  batch_list = batch_status.to_a
  Torque.pbs_statfree(batch_status)
  Torque.check_for_error
  batch_list
end

#find(args = {}) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/pbs/query.rb', line 58

def find(args = {})
  id = args[:id] || nil
  filters = args[:filters]
  filters = [args[:filter]] if args[:filter]

  # Get array of batch status hashes
  batch_list = _pbs_batchstat(id, filters)

  # Further filter results and then output them
  _filter_where_values(batch_list)
end

#is(hash) ⇒ Object

Used to filter where key attrib is equal to value

where.is(PBS::ATTR[:N] => "SimpleJob")

Raises:



34
35
36
37
38
39
# File 'lib/pbs/query.rb', line 34

def is(hash)
  key, value = hash.first
  raise PBS::Error, "`where' method not called before" if where_procs.empty? || where_procs[-1]
  self.where_procs[-1] = Proc.new {|h| h[key] == value}
  self
end

#not(hash) ⇒ Object

Used to filter where key attrib is NOT equal to value

where.not(PBS::ATTR[:N] => "SimpleJob")

Raises:



43
44
45
46
47
48
# File 'lib/pbs/query.rb', line 43

def not(hash)
  key, value = hash.first
  raise PBS::Error, "`where' method not called before" if where_procs.empty? || where_procs[-1]
  self.where_procs[-1] = Proc.new {|h| h[key] != value}
  self
end

#user(name) ⇒ Object

Used to filter specific user

where.user("username")

Raises:



52
53
54
55
56
# File 'lib/pbs/query.rb', line 52

def user(name)
  raise PBS::Error, "`where' method not called before" if where_procs.empty? || where_procs[-1]
  self.where_procs[-1] = Proc.new {|h| /^#{name}@/ =~ h[ATTR[:owner]]}
  self
end

#where(arg = nil, &block) ⇒ Object

Boolean procs used to filter out query results Examples:

where {|h| h[PBS::ATTR[:N]] == "SimpleJob"}
where(PBS::ATTR[:N]) {|v| v == "SimpleJob"}
where

the last one is used with other methods i.e., where.not(PBS::ATTR) => “SimpleJob”)



25
26
27
28
29
30
# File 'lib/pbs/query.rb', line 25

def where(arg = nil, &block)
  relation = self.clone
  relation.where_procs = @where_procs.clone
  relation.where_procs << (arg ? Proc.new {|h| block.call(h[arg])} : block)
  relation
end