Class: Britebox::FileJob

Inherits:
Object
  • Object
show all
Defined in:
lib/britebox/file_job.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_name, brite_client, options = {}) ⇒ FileJob

Returns a new instance of FileJob.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/britebox/file_job.rb', line 12

def initialize(file_name, brite_client, options = {})
  @file_name = file_name
  @brite_client = brite_client

  @threads_count = options[:threads]
  @queue = options[:queue]

  @status = 'pending'

  @size_processed = 0

  @semaphore = Mutex.new
  @timer = FileJobTimer.new

  unless File.exist?(file_name)
    report_error!("File not found")
    return
  end
  if (@size_total = File.size(file_name)) == 0
    report_error!("File is empty")
    return
  end
end

Instance Attribute Details

#brite_clientObject (readonly)

Returns the value of attribute brite_client.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def brite_client
  @brite_client
end

#errorObject (readonly)

Returns the value of attribute error.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def error
  @error
end

#file_nameObject (readonly)

Returns the value of attribute file_name.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def file_name
  @file_name
end

#queueObject (readonly)

Returns the value of attribute queue.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def queue
  @queue
end

#semaphoreObject (readonly)

Returns the value of attribute semaphore.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def semaphore
  @semaphore
end

#size_processedObject

Returns the value of attribute size_processed.



10
11
12
# File 'lib/britebox/file_job.rb', line 10

def size_processed
  @size_processed
end

#size_totalObject (readonly)

Returns the value of attribute size_total.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def size_total
  @size_total
end

#statusObject

Returns the value of attribute status.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def status
  @status
end

#threads_countObject (readonly)

Returns the value of attribute threads_count.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def threads_count
  @threads_count
end

#timerObject (readonly)

Returns the value of attribute timer.



7
8
9
# File 'lib/britebox/file_job.rb', line 7

def timer
  @timer
end

Instance Method Details

#as_jsonObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/britebox/file_job.rb', line 48

def as_json
  {
    id: id,
    file_name: id,
    status: @status,
    threads: @threads_count,
    size_total: @size_total,
    size_processed: @size_processed,
    started_at: started_at,
    processed_at: processed_at,
    duration: duration,
    percent_complete: percent_complete,
    error: @error
  }
end

#buffer_sizeObject



68
69
70
# File 'lib/britebox/file_job.rb', line 68

def buffer_size
  @threads_count * 4
end

#cancelled?Boolean

Returns:

  • (Boolean)


80
81
82
# File 'lib/britebox/file_job.rb', line 80

def cancelled?
  status == 'cancelled'
end

#durationObject



44
45
46
# File 'lib/britebox/file_job.rb', line 44

def duration
  @timer.duration
end

#error?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/britebox/file_job.rb', line 96

def error?
  status == 'error'
end

#idObject



64
65
66
# File 'lib/britebox/file_job.rb', line 64

def id
  File.basename(@file_name)
end

#paused?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/britebox/file_job.rb', line 88

def paused?
  status == 'paused'
end

#pending?Boolean

Returns:

  • (Boolean)


92
93
94
# File 'lib/britebox/file_job.rb', line 92

def pending?
  status == 'pending'
end

#percent_completeObject



72
73
74
75
76
77
78
# File 'lib/britebox/file_job.rb', line 72

def percent_complete
  if @size_total.to_i > 0
    (100.0 * @size_processed / @size_total).round(1)
  else
    0.0
  end
end

#processed_atObject



40
41
42
# File 'lib/britebox/file_job.rb', line 40

def processed_at
  @timer.ended_at
end

#started_atObject



36
37
38
# File 'lib/britebox/file_job.rb', line 36

def started_at
  @timer.started_at
end

#verify!(file_name_to) ⇒ Object



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
# File 'lib/britebox/file_job.rb', line 132

def verify!(file_name_to)
  return if error?

  recognizer = FormatRecognizer.new(@file_name)
  if recognizer.recognize!
    opts = recognizer.opts
  else
    report_error! recognizer.error
    return
  end


  # Block processing if some another FileJob processing lines
  loop do
    break if verifying?
    get_flag

    if paused?
      release_flag
      sleep 0.3
    else
      break
    end
  end

  if pending? || verifying?
    @timer.start
    @status = 'verifying'

    LinesVerifier.new(self).process!(file_name_to, opts)

    @timer.stop
  end

  if cancelled?
    File.delete file_name_to
  else
    @status = 'complete'
  end

  release_flag

  true
end

#verifying?Boolean

Returns:

  • (Boolean)


84
85
86
# File 'lib/britebox/file_job.rb', line 84

def verifying?
  status == 'verifying'
end