Class: Britebox::FileJobPool

Inherits:
Object
  • Object
show all
Includes:
Helpers
Defined in:
lib/britebox/file_job_pool.rb

Overview

Keep current status for all processing FileJobs

Constant Summary collapse

SPINNERS =
["|", "/", "", "\\"]

Constants included from Helpers

Helpers::LINE_UP, Helpers::SYM_CLEAR, Helpers::SYM_UP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helpers

#human_size

Constructor Details

#initializeFileJobPool

Returns a new instance of FileJobPool.



10
11
12
13
14
15
16
17
# File 'lib/britebox/file_job_pool.rb', line 10

def initialize
  # Only single FileJob can own :flag & process own lines
  @queue = Queue.new
  @queue << :flag

  @file_jobs = []
  @refresh_number = 0
end

Instance Attribute Details

#file_jobsObject (readonly)

Returns the value of attribute file_jobs.



6
7
8
# File 'lib/britebox/file_job_pool.rb', line 6

def file_jobs
  @file_jobs
end

Instance Method Details

#add(file_job) ⇒ Object



19
20
21
22
# File 'lib/britebox/file_job_pool.rb', line 19

def add(file_job)
  return if include? file_job.file_name
  @file_jobs << file_job
end

#include?(file_name) ⇒ Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/britebox/file_job_pool.rb', line 24

def include?(file_name)
  @file_jobs.map{ |fj| fj.file_name }.include? file_name
end


76
77
78
79
80
81
# File 'lib/britebox/file_job_pool.rb', line 76

def print_event_log
  if (event = EventLog.pop)
    # disable printing events for now
    # puts "[#{event.type}] #{event.message}"
  end
end


83
84
85
86
87
88
89
90
91
92
93
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
119
120
121
# File 'lib/britebox/file_job_pool.rb', line 83

def print_full_status

  if @prev_buffer_height
    print LINE_UP * @prev_buffer_height
  end

  buffer =  "\nFiles in list: #{@file_jobs.count}" # | MEM used: #{(`ps -o rss= -p #{Process.pid}`.to_f/1024).round(2)} MB"

  @file_jobs.each do |fj|
    fname = File.basename(fj.file_name)
    if fname.length > 20
      fname = fname[0..16] + '...'
    end

    if fj.status == 'error'
      status_str = fj.error
    elsif fj.status == 'complete'
      status_str = "100.0 %"
    else
      status_str = "#{fj.percent_complete} %".rjust(7)
    end

    if fj.status != 'error'
      status_str += ' | '
      if fj.duration
        status_str += Time.at(fj.duration.ceil).gmtime.strftime('%R:%S').gsub(/\A00:/,'').rjust(8)
      end
    end

    buffer << "\n#{fname.ljust(20)} | #{fj.status.to_s.ljust(9)} | #{human_size(fj.size_total).rjust(8)} | #{status_str}"
  end

  buffer << "\n #{SPINNERS[@refresh_number % 4]}\n"

  print buffer

  @prev_buffer_height = buffer.count("\n")
  @refresh_number += 1
end


68
69
70
71
72
73
74
# File 'lib/britebox/file_job_pool.rb', line 68

def print_status
  if Britebox::Config.ui_enabled
    print_event_log
  else
    print_full_status
  end
end

#process_file!(file, in_dir, out_dir, brite_client, fj_options = {}) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/britebox/file_job_pool.rb', line 28

def process_file!(file, in_dir, out_dir, brite_client, fj_options = {})
  file_name = File.expand_path(file, in_dir)

  # Do not process same files twice
  if self.include? file_name
    EventLog.add 'processing', "#{file}: ignoring, already processed file with same name"
    return
  end
  EventLog.add 'processing', "#{file}: Processing started"

  fj_options[:queue] = @queue
  fj_options[:threads] = Config.threads

  fj = FileJob.new(file_name, brite_client, fj_options)
  self.add(fj)
  Thread.new do
    fj.verify!(File.expand_path(file, out_dir))

    case fj.status
    when 'error'
      err_name = File.basename(file, File.extname(file)) + '_error' + File.extname(file)
      File.rename file_name, File.expand_path(err_name, out_dir)
      File.open(File.expand_path(err_name + '.log', out_dir), 'w+') do |f|
        f.write fj.error # TODO: add extended error log
      end
    when 'complete'
      File.delete file_name
    when 'cancelled'
      cancelled_name = File.basename(file, File.extname(file)) + '_cancelled' + File.extname(file)
      File.rename file_name, File.expand_path(cancelled_name, out_dir)
    else
      raise "unexpected error, status: #{fj.status}"
    end

    EventLog.add 'processing', "#{file}: Finished processing with status #{fj.status}"
  end
end