Class: WorkflowManager::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/workflow_manager/server.rb

Defined Under Namespace

Classes: Config, KyotoDB, PStoreDB, RedisDB

Constant Summary collapse

@@config =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeServer

Returns a new instance of Server.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/workflow_manager/server.rb', line 130

def initialize
  @interval = config.interval
  @resubmit = config.resubmit
  extension = case DB_MODE
                when "PStore"
                  '.pstore'
                when "KyotoCabinet"
                  '.kch'
                when "Redis"
                  @redis_conf = config.redis_conf
                  '.rdb'
                end
  @db_stat = File.join(config.db_dir, 'statuses'+extension)
  @db_logs  = File.join(config.db_dir, 'logs'+extension)

  @log_dir = File.expand_path(config.log_dir)
  @db_dir  = File.expand_path(config.db_dir)
  FileUtils.mkdir_p @log_dir unless File.exist?(@log_dir)
  FileUtils.mkdir_p @db_dir unless File.exist?(@db_dir)
  @statuses = case DB_MODE
                when "PStore"
                  PStoreDB.new(@db_stat)
                when "KyotoCabinet"
                  KyotoDB.new(@db_stat)
                when "Redis"
                  RedisDB.new(0, @redis_conf)
              end
  @logs = case DB_MODE
              when "PStore"
                PStoreDB.new(@db_logs)
              when "KyotoCabinet"
                KyotoDB.new(@db_logs)
              when "Redis"
                RedisDB.new(1, @redis_conf)
            end
  @jobs = RedisDB.new(2, @redis_conf)
  @trees = RedisDB.new(4, @redis_conf)

  @system_log = File.join(@log_dir, "system.log")
  @mutex = Mutex.new
  @cluster = config.cluster
  puts("DB = #{DB_MODE}")
  if DB_MODE == "Redis"
    puts("Redis conf = #{config.redis_conf}")
    puts("Redis port = #{@logs.port}")
  end
  puts("Cluster = #{@cluster.name}")
  log_puts("DB = #{DB_MODE}")
  log_puts("Cluster = #{@cluster.name}")
  log_puts("Server starts")
  log_puts("Recovery check")
  sleep 2
  recovery_job_checker
end

Class Method Details

.configObject



64
65
66
# File 'lib/workflow_manager/server.rb', line 64

def self.config
  @@config
end

.config=(config) ⇒ Object



61
62
63
# File 'lib/workflow_manager/server.rb', line 61

def self.config=(config)
  @@config = config
end

.configure {|@@config| ... } ⇒ Object

Yields:



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/workflow_manager/server.rb', line 70

def self.configure
  @@config = Config.new
  # default values
  @@config.log_dir  = LOG_DIR
  @@config.db_dir   = DB_DIR
  @@config.interval = INTERVAL  # interval to check jobs, [s]
  @@config.resubmit = RESUBMIT  # how many times at maximum to resubmit when job fails
  yield(@@config)
  if @@config.cluster
    @@config.cluster.log_dir = File.expand_path(@@config.log_dir)
  end
  @@config
end

Instance Method Details

#cluster_node_listObject



559
560
561
# File 'lib/workflow_manager/server.rb', line 559

def cluster_node_list
  @cluster.node_list
end

#cluster_nodesObject



215
216
217
# File 'lib/workflow_manager/server.rb', line 215

def cluster_nodes
  @cluster.cluster_nodes
end

#configObject



67
68
69
# File 'lib/workflow_manager/server.rb', line 67

def config
  @@config ||= WorkflowManager.configure{}
end

#copy_commands(org_dir, dest_parent_dir, now = nil, queue = "light") ⇒ Object



205
206
207
# File 'lib/workflow_manager/server.rb', line 205

def copy_commands(org_dir, dest_parent_dir, now=nil, queue="light")
  @cluster.copy_commands(org_dir, dest_parent_dir, now, queue)
end

#default_nodeObject



218
219
220
# File 'lib/workflow_manager/server.rb', line 218

def default_node
  @cluster.default_node.to_s
end

#delete_command(target) ⇒ Object



212
213
214
# File 'lib/workflow_manager/server.rb', line 212

def delete_command(target)
  @cluster.delete_command(target)
end

#finalize_monitoring(current_status, log_file, log_dir) ⇒ Object



285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
# File 'lib/workflow_manager/server.rb', line 285

def finalize_monitoring(current_status, log_file, log_dir)
  if current_status == 'success' or current_status == 'fail'
    unless log_dir.empty?
      copy_commands(log_file, log_dir).each do |command|
        log_puts(command)
        system command
      end
      err_file = log_file.gsub('_o.log','_e.log')
      copy_commands(err_file, log_dir).each do |command|
        log_puts(command)
        system command
      end
    end
    Thread.current.kill
  end
end

#get_log(job_id, with_err = false) ⇒ Object



504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
# File 'lib/workflow_manager/server.rb', line 504

def get_log(job_id, with_err=false)
  log_file = nil
  @logs.transaction do |logs|
    log_file = logs[job_id.to_s]
  end
  log_data = if log_file and File.exist?(log_file)
               "__STDOUT LOG__\n\n" + File.read(log_file)
             else
               'no log file'
             end
  if with_err
    err_file = log_file.gsub(/_o\.log/,'_e.log')
    if err_file and File.exist?(err_file)
      log_data << "\n\n__STDERR LOG__\n\n"
      log_data << File.read(err_file)
    end
  end
  log_data
end

#get_script(job_id) ⇒ Object



523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# File 'lib/workflow_manager/server.rb', line 523

def get_script(job_id)
  script_file = nil
  @logs.transaction do |logs|
    script_file = logs[job_id.to_s]
  end
  if script_file
    script_file = script_file.gsub(/_o\.log/,'')
  end
  script = if script_file and File.exist?(script_file)
             File.read(script_file)
           else
             'no script file'
           end
  script
end

#get_script_path(job_id) ⇒ Object



538
539
540
541
542
543
544
545
546
# File 'lib/workflow_manager/server.rb', line 538

def get_script_path(job_id)
  script_file = nil
  @logs.transaction do |logs|
    script_file = logs[job_id.to_s]
  end
  script_path = if script_file and File.exist?(script_file)
                  script_file.gsub(/_o\.log/,'')
                end
end

#helloObject



202
203
204
# File 'lib/workflow_manager/server.rb', line 202

def hello
  'hello hoge hoge bar boo bundle, '+ @cluster.name
end

#input_dataset_exist?(file_list) ⇒ Boolean

Returns:

  • (Boolean)


253
254
255
256
257
258
259
260
261
262
# File 'lib/workflow_manager/server.rb', line 253

def input_dataset_exist?(file_list)
  flag = true
  file_list.each do |file|
    unless File.exist?(file)
      flag = false
      break
    end
  end
  flag
end

#input_dataset_file_list(dataset_tsv_path) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
# File 'lib/workflow_manager/server.rb', line 242

def input_dataset_file_list(dataset_tsv_path)
  file_list = []
  CSV.foreach(dataset_tsv_path, :headers=>true, :col_sep=>"\t") do |row|
    row.each do |header, value|
      if header =~ /\[File\]/
        file_list << value
      end
    end
  end
  file_list
end

#input_dataset_tsv_path(script_content) ⇒ Object



229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/workflow_manager/server.rb', line 229

def input_dataset_tsv_path(script_content)
  gstore_dir = nil
  input_dataset_path = nil
  script_content.split(/\n/).each do |line|
    if line =~ /GSTORE_DIR=(.+)/
      gstore_dir = $1.chomp
    elsif line =~ /INPUT_DATASET=(.+)/
      input_dataset_path = $1.chomp
      break
    end
  end
  [gstore_dir, input_dataset_path]
end

#job_list(with_results = false, project_number = nil, job_ids: nil) ⇒ Object



475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
# File 'lib/workflow_manager/server.rb', line 475

def job_list(with_results=false, project_number=nil, job_ids:nil)
  s = []
  job_idsh = if job_ids
               Hash[*(job_ids.split(',')).map{|job_id| [job_id, true]}.flatten]
             end
  if project_number
    s_ = {}
    @jobs.transaction do |jobs|
      if project_jobs = jobs[project_number]
        s_ = Hash[*eval(project_jobs)]
      end
    end
    @statuses.transaction do |statuses|
      s_.each do |job_id, stat|
        s << [job_id, statuses[job_id]]
      end
    end
  else
    @statuses.transaction do |statuses|
      statuses.each do |key, value|
        s << [key, value]
      end
    end
  end
  if job_ids
    s = s.select{|job_id, stat| job_idsh[job_id]}
  end
  s.sort_by{|key, value| value.split(',')[2]}.reverse.map{|v| v.join(',')}.join("\n")
end

#kill_job(job_id) ⇒ Object



208
209
210
211
# File 'lib/workflow_manager/server.rb', line 208

def kill_job(job_id)
  status(job_id, 'FAIL')
  status = `#{@cluster.kill_command(job_id)}`
end

#load_dataset_tree(project_number) ⇒ Object



568
569
570
571
572
573
574
# File 'lib/workflow_manager/server.rb', line 568

def load_dataset_tree(project_number)
  json = nil
  @trees.transaction do |trees|
    json = trees[project_number]
  end
  json
end

#log_puts(str) ⇒ Object



221
222
223
224
225
226
227
228
# File 'lib/workflow_manager/server.rb', line 221

def log_puts(str)
  time = Time.now.strftime("[%Y.%m.%d %H:%M:%S]")
  @mutex.synchronize do
     open(@system_log, "a") do |out|
       out.print time + " " + str + "\n"
     end
  end
end

#recovery_job_checkerObject



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# File 'lib/workflow_manager/server.rb', line 184

def recovery_job_checker
  @logs.transaction do |logs|
  @statuses.transaction do |statuses|
    statuses.each do |job_id, status|
      # puts [job_id, status].join(",")
      # 120249,RUNNING,QC_ventricles_100k.sh,2021-07-30 09:47:04/2021-07-30 09:47:04,masaomi,1535
      stat, script_basename, time, user, project_number, next_dataset_id, rails_host, log_dir = status.split(",")
      if stat == "RUNNING" or stat == "PENDING"
        log_file = logs[job_id]
        log_puts("JobID (in recovery check): #{job_id}")
        puts "JobID (in recovery check): #{job_id}"
        copy_command_template = copy_commands("org_file", log_dir).first
        job_checker = JobChecker.perform_async(job_id, script_basename, log_file, user, project_number, next_dataset_id, rails_host, log_dir, copy_command_template)
      end
    end
  end
  end
end

#save_dataset_tree(project_number, json) ⇒ Object



562
563
564
565
566
567
# File 'lib/workflow_manager/server.rb', line 562

def save_dataset_tree(project_number, json)
  @trees.transaction do |trees|
    trees[project_number] = json
  end
  json
end

#start_monitoring(submit_command, user = 'sushi lover', resubmit = 0, script = '', project_number = 0, sge_options = '', log_dir = '') ⇒ Object



364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/workflow_manager/server.rb', line 364

def start_monitoring(submit_command, user = 'sushi lover', resubmit = 0, script = '', project_number = 0, sge_options='', log_dir = '')
  log_puts("monitoring: script=" + submit_command + " user=" + user + " resubmit=" + resubmit.to_s + " project=" + project_number.to_s + " sge option=" + sge_options + " log dir=" + log_dir.to_s)

  #warn submit_command
  #
  # TODO: analyze arguments
  #
  job_id, log_file, command = @cluster.submit_job(submit_command, script, sge_options)
  log_puts("submit: " + job_id + " " + command)

  #
  # monitor worker
  #
  if job_id and log_file
    monitor_worker = Thread.new(job_id, log_file, submit_command, user, resubmit, script, project_number, sge_options, log_dir) do |t_job_id, t_log_file, t_submit_command, t_user, t_resubmit, t_script, t_project_number, t_sge_options, t_log_dir|
      loop do
        status = success_or_fail(t_job_id, t_log_file) 
        script_name = File.basename(submit_command).split(/-/).first
        #@statuses.open(@db_stat)
        @statuses.transaction do |statuses|
        #start_time = if stat = @statuses[t_job_id] and stat = stat.split(/,/) and time = stat[2]
          start_time = if stat = statuses[t_job_id] and stat = stat.split(/,/) and time = stat[2]
                         time
                       end
          time = if start_time 
                   if status == 'success' or status == 'fail'
                     start_time + '/' + Time.now.strftime("%Y-%m-%d %H:%M:%S")
                   else
                     start_time
                   end
                 else
                   Time.now.strftime("%Y-%m-%d %H:%M:%S")
                 end
        #@statuses[t_job_id] = [status, script_name, time, user, project_number].join(',')
          statuses[t_job_id] = [status, script_name, time, user, project_number].join(',')
        #@statuses.close
        end
        @logs.transaction do |logs|
          logs[t_job_id] = t_log_file
        end
        #warn t_job_id + " " + status
        if status == 'success'
          log_puts(status + ": " + t_job_id)
          unless t_log_dir.empty?
            copy_commands(t_log_file, t_log_dir).each do |command|
              log_puts(command)
              system command
            end
            err_file = t_log_file.gsub('_o.log','_e.log')
            copy_commands(err_file, t_log_dir).each do |command|
              log_puts(command)
              system command
            end
          end
          Thread.current.kill
        elsif status == 'fail'
          log_puts(status + ": " + t_job_id)
          #
          # TODO: re-submit
          #
          if t_resubmit < RESUBMIT
            log_puts("resubmit: " + t_job_id)
            resubmit_job_id = start_monitoring(t_submit_command, t_user, t_resubmit + 1, t_script, t_project_number, t_sge_options)
            script_name = File.basename(submit_command).split(/-/).first
            #@statuses.open(@db_stat)
            @statuses.transaction do |statuses|
              statuses[t_job_id] = ["resubmit: " + resubmit_job_id.to_s, script_name, Time.now.strftime("%Y-%m-%d %H:%M:%S"), t_user, t_project_number].join(',')
            #@statuses.close
            end
          else
            log_puts("fail: " + t_job_id)
          end
          unless t_log_dir.empty?
            copy_commands(t_log_file, t_log_dir).each do |command|
              log_puts(command)
              system command
            end
            err_file = t_log_file.gsub('_o.log','_e.log')
            copy_commands(err_file, t_log_dir).each do |command|
              log_puts(command)
              system command
            end
          end
          Thread.current.kill
        end
        sleep @interval
      end	
    end
    job_id.to_i
  end
end

#start_monitoring2(script_path, script_content, user = 'sushi_lover', project_number = 0, sge_options = '', log_dir = '') ⇒ Object



314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
# File 'lib/workflow_manager/server.rb', line 314

def start_monitoring2(script_path, script_content, user='sushi_lover', project_number=0, sge_options='', log_dir='')
  # script_path is only used to generate a log file name
  # It is not used to read the script contents
  go_submit = false
  waiting_time = 0
  gstore_dir, input_dataset_path = input_dataset_tsv_path(script_content)
  if gstore_dir and input_dataset_path
    waiting_max = 60*60*8 # 8h
    # wait until the files come
    until waiting_time > waiting_max or 
      File.exist?(input_dataset_path) and
      file_list = input_dataset_file_list(input_dataset_path) and
      file_list.map!{|file| File.join(gstore_dir, file)} and
      go_submit = input_dataset_exist?(file_list)
      sleep @interval
      waiting_time += @interval
    end
  end 

  job_id, log_file, command = if go_submit 
                                @cluster.submit_job(script_path, script_content, sge_options)
                              else
                                raise "stop submitting #{File.basename(script_path)}, since waiting_time #{waiting_time} > #{waiting_max}"
                              end

  if job_id and log_file 
    # save log_file in logs
    @logs.transaction do |logs|
      logs[job_id] = log_file
    end

    # job status check until it finishes with success or fail 
    worker = Thread.new(log_dir, script_path, script_content, sge_options) do |log_dir, script_path, script_content, sge_options|
      loop do
        # check status
        current_status = check_status(job_id, log_file)

        # save time and status
        update_time_status(job_id, current_status, script_path, user, project_number)

        # finalize (kill current thred) in case of success or fail 
        finalize_monitoring(current_status, log_file, log_dir)

        # wait
        sleep @interval
      end # loop
    end
    job_id
  end
end

#start_monitoring3(script_path, script_content, user = 'sushi_lover', project_number = 0, sge_options = '', log_dir = '', next_dataset_id = '', rails_host = nil) ⇒ Object



301
302
303
304
305
306
307
308
309
310
311
312
313
# File 'lib/workflow_manager/server.rb', line 301

def start_monitoring3(script_path, script_content, user='sushi_lover', project_number=0, sge_options='', log_dir='', next_dataset_id='', rails_host=nil)
  script_basename = File.basename(script_path)
  job_id, log_file, command = @cluster.submit_job(script_path, script_content, sge_options)
  #p command
  #p log_file
  #p job_id
  puts "JobID (in WorkflowManager): #{job_id}"
  #puts "log_dir=#{log_dir}"
  sleep 1
  copy_command_template = copy_commands("org_file", log_dir).first
  job_checker = JobChecker.perform_async(job_id, script_basename, log_file, user, project_number, next_dataset_id, rails_host, log_dir, copy_command_template)
  job_id
end

#status(job_id, new_status = nil) ⇒ Object



455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
# File 'lib/workflow_manager/server.rb', line 455

def status(job_id, new_status=nil)
  stat = nil
  #@statuses.open(@db_stat)
  @statuses.transaction do |statuses|
    if new_status and stat = statuses[job_id.to_s]
      status_list = ['CONPLETED', 'RUNNING', 'PENDING', 'FAIL']
      if status_list.include?(new_status)
        items = stat.split(/,/)
        items.shift
        items.unshift(new_status)
        stat = items.join(',')
        statuses[job_id.to_s] = stat
      end
    else
      stat = statuses[job_id.to_s]
    end
  end
  #@statuses.close
  stat
end

#success_or_fail(job_id, log_file) ⇒ Object Also known as: check_status



547
548
549
550
551
552
553
554
555
556
557
# File 'lib/workflow_manager/server.rb', line 547

def success_or_fail(job_id, log_file)
  msg = if @cluster.job_running?(job_id)
          'running'
        elsif @cluster.job_ends?(log_file)
          'success'
        elsif @cluster.job_pending?(job_id)
          'pending'
        else
          'fail'
        end
end

#update_time_status(job_id, current_status, script_name, user, project_number) ⇒ Object



263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
# File 'lib/workflow_manager/server.rb', line 263

def update_time_status(job_id, current_status, script_name, user, project_number)
  # if the current status changed from last time, then save, otherwise do nothing
  # once status changes into success or fail, then the thread is expected to be killed in later process
  @statuses.transaction do |statuses|
    start_time = nil
    if stat = statuses[job_id] 
      last_status, script_name, start_time, user, project_number = stat.split(/,/)
    end
    time = if start_time 
             if current_status == 'success' or current_status == 'fail'
               start_time + '/' + Time.now.strftime("%Y-%m-%d %H:%M:%S")
             elsif current_status != last_status
               Time.now.strftime("%Y-%m-%d %H:%M:%S")
             end
           else
             Time.now.strftime("%Y-%m-%d %H:%M:%S")
           end
    if time
      statuses[job_id] = [current_status, script_name, time, user, project_number].join(',')
    end
  end
end