Class: NERA::JobLayerController

Inherits:
Object
  • Object
show all
Defined in:
lib/nera/nera_job_layer_controller.rb

Instance Method Summary collapse

Constructor Details

#initialize(path_db_folder) ⇒ JobLayerController

Returns a new instance of JobLayerController.

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
# File 'lib/nera/nera_job_layer_controller.rb', line 20

def initialize( path_db_folder)
  raise ArgumentError unless path_db_folder.is_a?(String)
  
  @db_folders = NERA::DbFolders.new( path_db_folder)
  @job_records = NERA::JobRecords.new( @db_folders.path_to_jobs_table)
  @job_records.set_yaml_file( @db_folders.path_to_jobs_yaml)
end

Instance Method Details

#cancel_jobs(job_ids) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nera/nera_job_layer_controller.rb', line 57

def cancel_jobs( job_ids)
  unless job_ids.is_a?(Array)
    raise ArgumentError, "job_ids must be an Array."
  end
  job_ids.each do |x|
    raise ArgumentError, "each element of job_ids must be an Integer" unless x.is_a?(Integer)
  end

  destroyed_jobids = []
  @job_records.transaction {
    job_ids.each do |jid|
      d = @job_records.destroy( jid)
      next unless d
      sim_recs = NERA::SimulatorRecords.new( @db_folders.path_to_simulators_table)
      sim_recs.set_yaml_file( @db_folders.path_to_simulators_yaml )
      found = sim_recs.list.find do |item| item[:name] == d[:simulator] end
      sim_id = found[:id]
      klass = NERA::Simulator.inherited_simulators.find do |sim|
        sim.to_s == d[:simulator]
      end
      raise RuntimeError, "must not happen" unless klass
      p_tab_path = @db_folders.path_to_parameters_table( klass)
      param_recs = NERA::ParameterRecords.new( p_tab_path, sim_recs, sim_id)
      param_recs.set_yaml_file( @db_folders.path_to_parameters_yaml( klass) )
      run_records = NERA::RunRecords.new( @db_folders.path_to_runs_table( klass, d[:parameter_id]), param_recs, d[:parameter_id] )
      run_records.set_yaml_file( @db_folders.path_to_runs_yaml( klass, d[:parameter_id]) )
      param_recs.transaction {
        run_records.transaction {
          a = run_records.destroy_job_id( jid)
          raise RuntimeError, "must not happen" unless a
          FileUtils.rm( @db_folders.path_to_job_script(jid) )
          param_recs.update_num_runs( d[:parameter_id], run_records.list_all_finished.size, run_records.list_all.size)
        }
      }
      destroyed_jobids << jid
      @db_folders.logger.info(self.class.to_s) {
        "canceled a job (jobid:#{jid})"
      }
    end
  }
  destroyed_jobids = nil if destroyed_jobids.size == 0
  return destroyed_jobids
end

#execute_jobs_now(job_ids) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/nera/nera_job_layer_controller.rb', line 101

def execute_jobs_now( job_ids)
  unless job_ids.is_a?(Array)
    raise ArgumentError, "job_ids must be an Array."
  end
  job_ids.each do |x|
    raise ArgumentError, "each element of job_ids must be an Integer" unless x.is_a?(Integer)
  end

   = []
  @job_records.transaction {
    jids = []
    job_ids.uniq.each do |jid|
      jids << jid if @job_records.find_by_id(jid)
    end
    return nil if jids.size == 0

    jids.each do |jid|
      js_path = File.expand_path( @db_folders.path_to_job_script(jid) )
      FileUtils.chmod(0744, js_path)
      next if File.directory?( @db_folders.path_to_include_layer+File.basename( js_path).sub(/\.sh$/,'') ) or File.exist?( @db_folders.path_to_include_layer + File.basename( js_path).sub(/\.sh$/,'.tar.bz2'))
      Dir.chdir( @db_folders.path_to_include_layer) {
        system "nohup #{js_path} &"
      }
       << jid
      @job_records.( jid, "localhost")
      @db_folders.logger.info( self.class.to_s) {
        "submitted a job (jobid:#{jid}) to localhost"
      }
    end
  }
   = nil if .size == 0
  return 
end

#include(filename) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/nera/nera_job_layer_controller.rb', line 144

def include( filename)
  @job_records.transaction {
    fpath = @db_folders.path_to_include_layer + filename
    return nil unless File.exist?(fpath)
    jinfo, rinfo = expand_and_parse( fpath)
    run_layer_path, run_recs, param_recs, param_id = check_consistency( jinfo, rinfo)
    param_recs.transaction {
      run_recs.transaction {
        folder_path = fpath.sub(/.tar.bz2$/,'/')
        file_move( folder_path, run_layer_path, jinfo[:run_ids])
        update_tables( run_recs, jinfo, rinfo)
        param_recs.update_num_runs( param_id, run_recs.list_all_finished.size, run_recs.list_all.size)
      }
    }
    remove_files( fpath)
    @db_folders.logger.info(self.class.to_s) {
      "included a file (#{filename})"
    }

  }
  return true
end

#include_allObject



308
309
310
311
312
313
314
315
# File 'lib/nera/nera_job_layer_controller.rb', line 308

def include_all
  flag = true
  include_list.each do |fname|
    f = include(fname)
    flag = nil unless f
  end
  return flag
end

#include_listObject



136
137
138
139
140
141
142
# File 'lib/nera/nera_job_layer_controller.rb', line 136

def include_list
  list = @db_folders.search_include_files
  list.map! do |path|
    path.sub( @db_folders.path_to_include_layer, '')
  end
  return list
end

#not_finished_listObject



33
34
35
# File 'lib/nera/nera_job_layer_controller.rb', line 33

def not_finished_list
  @job_records.list_all
end

#not_finished_list_in_csvObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/nera/nera_job_layer_controller.rb', line 37

def not_finished_list_in_csv
  list = @job_records.list_all
  header = @job_records.keys.dup
  header_ab = "id, state, simulator, param_id, run_id, num_runs, created_at, updated_at, host_name"

  csv_list = []
  list.each do |r_hash|
    strings = []
    header.each do |key|
      if r_hash[key].is_a?(DateTime)
        strings << r_hash[key].to_s.split('T')[0]
      else
        strings << r_hash[key].to_s
      end
    end
    csv_list << strings.join(", ")
  end
  return header_ab, csv_list
end

#path_to_job_layerObject



29
30
31
# File 'lib/nera/nera_job_layer_controller.rb', line 29

def path_to_job_layer
  return @db_folders.path_to_job_layer
end

#recently_included_listObject

Return the array of the finished records. Each record is a Hash. Return the records which are included on the same day as the most recently included record.



170
171
172
173
174
175
176
177
178
179
# File 'lib/nera/nera_job_layer_controller.rb', line 170

def recently_included_list
  recs = []
  fname = @db_folders.path_to_recently_finished_jobs_file
  if File.exist?( fname)
    YAML.load_documents( File.open(fname,'r') ) do |obj|
      recs << obj if obj
    end
  end
  return recs
end