Class: Britebox::FileJob
- Inherits:
-
Object
- Object
- Britebox::FileJob
- Defined in:
- lib/britebox/file_job.rb
Instance Attribute Summary collapse
-
#brite_client ⇒ Object
readonly
Returns the value of attribute brite_client.
-
#error ⇒ Object
readonly
Returns the value of attribute error.
-
#file_name ⇒ Object
readonly
Returns the value of attribute file_name.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
-
#semaphore ⇒ Object
readonly
Returns the value of attribute semaphore.
-
#size_processed ⇒ Object
Returns the value of attribute size_processed.
-
#size_total ⇒ Object
readonly
Returns the value of attribute size_total.
-
#status ⇒ Object
Returns the value of attribute status.
-
#threads_count ⇒ Object
readonly
Returns the value of attribute threads_count.
-
#timer ⇒ Object
readonly
Returns the value of attribute timer.
Instance Method Summary collapse
- #as_json ⇒ Object
- #buffer_size ⇒ Object
- #cancelled? ⇒ Boolean
- #duration ⇒ Object
- #error? ⇒ Boolean
- #id ⇒ Object
-
#initialize(file_name, brite_client, options = {}) ⇒ FileJob
constructor
A new instance of FileJob.
- #paused? ⇒ Boolean
- #pending? ⇒ Boolean
- #percent_complete ⇒ Object
- #processed_at ⇒ Object
- #started_at ⇒ Object
- #verify!(file_name_to) ⇒ Object
- #verifying? ⇒ Boolean
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, = {}) @file_name = file_name @brite_client = brite_client @threads_count = [:threads] @queue = [: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_client ⇒ Object (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 |
#error ⇒ Object (readonly)
Returns the value of attribute error.
7 8 9 |
# File 'lib/britebox/file_job.rb', line 7 def error @error end |
#file_name ⇒ Object (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 |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
7 8 9 |
# File 'lib/britebox/file_job.rb', line 7 def queue @queue end |
#semaphore ⇒ Object (readonly)
Returns the value of attribute semaphore.
7 8 9 |
# File 'lib/britebox/file_job.rb', line 7 def semaphore @semaphore end |
#size_processed ⇒ Object
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_total ⇒ Object (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 |
#status ⇒ Object
Returns the value of attribute status.
7 8 9 |
# File 'lib/britebox/file_job.rb', line 7 def status @status end |
#threads_count ⇒ Object (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 |
#timer ⇒ Object (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_json ⇒ Object
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_size ⇒ Object
68 69 70 |
# File 'lib/britebox/file_job.rb', line 68 def buffer_size @threads_count * 4 end |
#cancelled? ⇒ Boolean
80 81 82 |
# File 'lib/britebox/file_job.rb', line 80 def cancelled? status == 'cancelled' end |
#duration ⇒ Object
44 45 46 |
# File 'lib/britebox/file_job.rb', line 44 def duration @timer.duration end |
#error? ⇒ Boolean
96 97 98 |
# File 'lib/britebox/file_job.rb', line 96 def error? status == 'error' end |
#id ⇒ Object
64 65 66 |
# File 'lib/britebox/file_job.rb', line 64 def id File.basename(@file_name) end |
#paused? ⇒ Boolean
88 89 90 |
# File 'lib/britebox/file_job.rb', line 88 def paused? status == 'paused' end |
#pending? ⇒ Boolean
92 93 94 |
# File 'lib/britebox/file_job.rb', line 92 def pending? status == 'pending' end |
#percent_complete ⇒ Object
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_at ⇒ Object
40 41 42 |
# File 'lib/britebox/file_job.rb', line 40 def processed_at @timer.ended_at end |
#started_at ⇒ Object
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 get_flag if paused? release_flag sleep 0.3 else break end end if pending? || @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
84 85 86 |
# File 'lib/britebox/file_job.rb', line 84 def status == 'verifying' end |