Class: BackgroundQueue::Worker::Progress
- Inherits:
-
Object
- Object
- BackgroundQueue::Worker::Progress
- Defined in:
- lib/background_queue/worker/progress.rb
Overview
a way of notifying worker progress
Instance Attribute Summary collapse
-
#registered_tasks ⇒ Object
readonly
Returns the value of attribute registered_tasks.
Instance Method Summary collapse
- #add_error(error) ⇒ Object
- #add_note(notice) ⇒ Object
- #add_warning(warning) ⇒ Object
- #finish(main_caption = nil) ⇒ Object
- #get_caption ⇒ Object
- #get_percent ⇒ Object
- #get_task_size(key) ⇒ Object
- #inc ⇒ Object
-
#initialize(callback_object) ⇒ Progress
constructor
A new instance of Progress.
- #register_task(key, units) ⇒ Object
- #set_main_caption(caption) ⇒ Object
- #set_meta_data(key, data) ⇒ Object
- #set_task_caption(caption) ⇒ Object
- #set_task_progress(percent) ⇒ Object
- #set_task_steps(step_count) ⇒ Object
- #start(main_caption = nil) ⇒ Object
- #start_task(key, caption = nil) ⇒ Object
- #update_callback ⇒ Object
- #update_callback_meta(key, value) ⇒ Object
Constructor Details
#initialize(callback_object) ⇒ Progress
Returns a new instance of Progress.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/background_queue/worker/progress.rb', line 7 def initialize(callback_object) if callback_object.kind_of?(BackgroundQueue::Worker::Base) @worker = callback_object elsif callback_object.kind_of?(Proc) @callback = callback_object else @callback_object = callback_object end @main_caption = nil @sub_caption = nil @finished_sub_progress = 0 @current_sub_progress_size = 0 @current_sub_progress = 0 @sub_task_step_size = 0 end |
Instance Attribute Details
#registered_tasks ⇒ Object (readonly)
Returns the value of attribute registered_tasks.
5 6 7 |
# File 'lib/background_queue/worker/progress.rb', line 5 def registered_tasks @registered_tasks end |
Instance Method Details
#add_error(error) ⇒ Object
107 108 109 |
# File 'lib/background_queue/worker/progress.rb', line 107 def add_error(error) (:error, error) end |
#add_note(notice) ⇒ Object
99 100 101 |
# File 'lib/background_queue/worker/progress.rb', line 99 def add_note(notice) (:notice, notice) end |
#add_warning(warning) ⇒ Object
103 104 105 |
# File 'lib/background_queue/worker/progress.rb', line 103 def add_warning(warning) (:warning, warning) end |
#finish(main_caption = nil) ⇒ Object
42 43 44 45 46 47 48 |
# File 'lib/background_queue/worker/progress.rb', line 42 def finish( = nil) @main_caption = unless .nil? @sub_caption = nil unless .nil? @current_sub_progress = 0 @finished_sub_progress = 100 update_callback end |
#get_caption ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/background_queue/worker/progress.rb', line 115 def if @main_caption && @sub_caption "#{@main_caption}: #{@sub_caption }" elsif @main_caption @main_caption elsif @sub_caption @sub_caption else "" end end |
#get_percent ⇒ Object
127 128 129 |
# File 'lib/background_queue/worker/progress.rb', line 127 def get_percent @finished_sub_progress + (@current_sub_progress_size / 100.0 * @current_sub_progress / 100.0) end |
#get_task_size(key) ⇒ Object
70 71 72 73 |
# File 'lib/background_queue/worker/progress.rb', line 70 def get_task_size(key) raise "No registered sub task (#{key})" if @registered_tasks[key].nil? @registered_tasks[key].to_f / @registered_task_total * 100.0 end |
#inc ⇒ Object
83 84 85 86 |
# File 'lib/background_queue/worker/progress.rb', line 83 def inc @current_sub_progress += @sub_task_step_size update_callback end |
#register_task(key, units) ⇒ Object
51 52 53 54 55 56 |
# File 'lib/background_queue/worker/progress.rb', line 51 def register_task(key, units) @registered_tasks ||= {} @registered_task_total ||= 0 @registered_tasks[key] = units @registered_task_total += units end |
#set_main_caption(caption) ⇒ Object
37 38 39 40 |
# File 'lib/background_queue/worker/progress.rb', line 37 def () @main_caption = update_callback end |
#set_meta_data(key, data) ⇒ Object
111 112 113 |
# File 'lib/background_queue/worker/progress.rb', line 111 def (key, data) (:meta, {key=>data}) end |
#set_task_caption(caption) ⇒ Object
94 95 96 97 |
# File 'lib/background_queue/worker/progress.rb', line 94 def () @sub_caption = update_callback end |
#set_task_progress(percent) ⇒ Object
88 89 90 91 92 |
# File 'lib/background_queue/worker/progress.rb', line 88 def set_task_progress(percent) @current_sub_progress += percent @current_sub_progress = 100 if @current_sub_progress > 100 update_callback end |
#set_task_steps(step_count) ⇒ Object
75 76 77 78 79 80 81 |
# File 'lib/background_queue/worker/progress.rb', line 75 def set_task_steps(step_count) if step_count == 0 @sub_task_step_size = 100 else @sub_task_step_size = 100.0 / step_count.to_f * 100.0 end end |
#start(main_caption = nil) ⇒ Object
25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/background_queue/worker/progress.rb', line 25 def start(=nil) @main_caption = @sub_caption = nil @finished_sub_progress = 0 @current_sub_progress_size = 0 @current_sub_progress = 0 update_callback end |
#start_task(key, caption = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/background_queue/worker/progress.rb', line 58 def start_task(key, =nil) if @current_sub_progress_size != 0 @finished_sub_progress += @current_sub_progress_size @current_sub_progress_size = 0 end @current_sub_progress_size = get_task_size(key) @current_sub_progress = 0.0 @sub_task_step_size = 0 @sub_caption = update_callback end |
#update_callback ⇒ Object
131 132 133 134 135 136 137 138 139 |
# File 'lib/background_queue/worker/progress.rb', line 131 def update_callback if @worker @worker.set_progress(, get_percent) elsif @callback @callback.call(:progress, , get_percent) elsif @callback_object @callback_object.set_progress(, get_percent, self) end end |
#update_callback_meta(key, value) ⇒ Object
141 142 143 144 145 146 147 148 149 |
# File 'lib/background_queue/worker/progress.rb', line 141 def (key, value) if @worker @worker.(key, value) elsif @callback @callback.call(:meta, key, value) elsif @callback_object @callback_object.(key, value, self) end end |