Class: JekyllImgFlow::BatchManager
- Inherits:
-
Object
- Object
- JekyllImgFlow::BatchManager
- Defined in:
- lib/jekyll-imgflow/batch_manager.rb
Overview
BatchManager - manages batch processing of image operations Queues tasks and processes them efficiently
Instance Attribute Summary collapse
-
#completed ⇒ Object
readonly
Returns the value of attribute completed.
-
#failed ⇒ Object
readonly
Returns the value of attribute failed.
-
#queue ⇒ Object
readonly
Returns the value of attribute queue.
Class Method Summary collapse
-
.build_default_tasks(original_name, input_path, config, _site, file_digest: nil) ⇒ Array<Hash>
Build batch tasks for default image versions.
-
.build_specialized_task(original_name, input_path, operations, output_path, page_path) ⇒ Hash
Build batch tasks for specialized image version.
Instance Method Summary collapse
-
#add_task(task) ⇒ Object
Add a task to the batch queue.
-
#add_tasks(tasks) ⇒ Object
Add multiple tasks to the queue.
-
#clear ⇒ Object
Clear all queues.
-
#failed_tasks ⇒ Array<Hash>
Get failed tasks for retry.
-
#initialize(operation_processor) ⇒ BatchManager
constructor
A new instance of BatchManager.
-
#mark_completed(task, status, result = nil) ⇒ Object
Mark a task as completed.
-
#mark_failed(task, error) ⇒ Object
Mark a task as failed.
-
#next_task ⇒ Hash?
Get the next task from the queue.
-
#process_all(parallel: false) ⇒ Hash
Process all queued tasks.
-
#process_parallel ⇒ Object
Process tasks in parallel (future enhancement).
-
#process_sequential ⇒ Object
Process tasks sequentially.
-
#process_task(task) ⇒ Object
Process a single task.
-
#retry_failed ⇒ Integer
Retry failed tasks.
-
#status ⇒ Hash
Get queue status.
Constructor Details
#initialize(operation_processor) ⇒ BatchManager
Returns a new instance of BatchManager.
11 12 13 14 15 16 17 18 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 11 def initialize(operation_processor) @operation_processor = operation_processor @filename_generator = FilenameGenerator.new @queue = [] @completed = [] @failed = [] @mutex = Mutex.new end |
Instance Attribute Details
#completed ⇒ Object (readonly)
Returns the value of attribute completed.
9 10 11 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 9 def completed @completed end |
#failed ⇒ Object (readonly)
Returns the value of attribute failed.
9 10 11 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 9 def failed @failed end |
#queue ⇒ Object (readonly)
Returns the value of attribute queue.
9 10 11 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 9 def queue @queue end |
Class Method Details
.build_default_tasks(original_name, input_path, config, _site, file_digest: nil) ⇒ Array<Hash>
Build batch tasks for default image versions
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 206 def self.build_default_tasks(original_name, input_path, config, _site, file_digest: nil) tasks = [] # Use FilenameGenerator for consistent naming filename_generator = JekyllImgFlow::FilenameGenerator.new path_resolver = JekyllImgFlow::PathResolver.new(config) config.sizes.each_value do |width| config.formats.each do |format| # Use FilenameGenerator to generate proper filename operations = { width: width, format: format, quality: config.quality } filename = filename_generator.generate_filename(original_name, operations) # Preserve original directory structure under output subdir = File.dirname(original_name) subdir = nil if subdir == "." # Write to source directory so Jekyll copies files to _site during write phase output_path = path_resolver.resolve_source_output_path(filename, subdir) tasks << { original_name: original_name, operation_type: :resize, input_path: input_path, output_path: output_path, params: { width: width, format: format, quality: config.quality }, version_type: :default, page_path: nil, # Default versions not tied to specific page file_digest: file_digest, skip_if_exists: true } end end tasks end |
.build_specialized_task(original_name, input_path, operations, output_path, page_path) ⇒ Hash
Build batch tasks for specialized image version
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 254 def self.build_specialized_task(original_name, input_path, operations, output_path, page_path) # For now, assume single operation # Future: support chaining multiple operations operation = operations.first { original_name: original_name, operation_type: operation[:type], input_path: input_path, output_path: output_path, params: operation[:params], version_type: :specialized, page_path: page_path, skip_if_exists: false } end |
Instance Method Details
#add_task(task) ⇒ Object
Add a task to the batch queue
29 30 31 32 33 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 29 def add_task(task) @mutex.synchronize do @queue << task end end |
#add_tasks(tasks) ⇒ Object
Add multiple tasks to the queue
37 38 39 40 41 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 37 def add_tasks(tasks) @mutex.synchronize do @queue.concat(tasks) end end |
#clear ⇒ Object
Clear all queues
173 174 175 176 177 178 179 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 173 def clear @mutex.synchronize do @queue.clear @completed.clear @failed.clear end end |
#failed_tasks ⇒ Array<Hash>
Get failed tasks for retry
183 184 185 186 187 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 183 def failed_tasks @mutex.synchronize do @failed.map { |f| f[:task] } end end |
#mark_completed(task, status, result = nil) ⇒ Object
Mark a task as completed
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 134 def mark_completed(task, status, result = nil) @mutex.synchronize do @completed << { task: task, status: status, result: result, completed_at: Time.now } end end |
#mark_failed(task, error) ⇒ Object
Mark a task as failed
148 149 150 151 152 153 154 155 156 157 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 148 def mark_failed(task, error) @mutex.synchronize do @failed << { task: task, error: error., backtrace: error.backtrace&.first(5), failed_at: Time.now } end end |
#next_task ⇒ Hash?
Get the next task from the queue
83 84 85 86 87 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 83 def next_task @mutex.synchronize do @queue.shift end end |
#process_all(parallel: false) ⇒ Hash
Process all queued tasks
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 46 def process_all(parallel: false) start_time = Time.now if parallel # Future: implement parallel processing process_parallel else process_sequential end end_time = Time.now duration = end_time - start_time { total: @queue.length + @completed.length + @failed.length, completed: @completed.length, failed: @failed.length, duration: duration } end |
#process_parallel ⇒ Object
Process tasks in parallel (future enhancement)
75 76 77 78 79 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 75 def process_parallel # Future: implement thread pool for parallel processing # For now, fall back to sequential process_sequential end |
#process_sequential ⇒ Object
Process tasks sequentially
68 69 70 71 72 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 68 def process_sequential while (task = next_task) process_task(task) end end |
#process_task(task) ⇒ Object
Process a single task
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 122 123 124 125 126 127 128 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 91 def process_task(task) # Check if processing is needed # Check if output is up-to-date if !task[:force_processing] && task[:skip_if_exists] && File.exist?(task[:output_path]) && !@operation_processor.needs_processing?( task[:input_path], task[:output_path], task[:params] ) Jekyll.logger.debug "⏭️ Skipping #{task[:original_name]} - already up-to-date" mark_completed(task, :skipped) return end # Process the operation operation = { type: task[:operation_type], params: task[:params], file_digest: task[:file_digest] } Jekyll.logger.debug "🔍 BatchManager: Processing task - input: #{task[:input_path]}, original: #{task[:original_name]}" result = @operation_processor.process_operation( task[:original_name], operation, task[:input_path] ) Jekyll.logger.debug "🔍 BatchManager: OperationProcessor returned - result: #{result}" # Store result for manifest registration task[:result_path] = result mark_completed(task, :processed, result) Jekyll.logger.debug "✅ Processed: #{task[:original_name]} → #{File.basename(result)}" rescue StandardError => e mark_failed(task, e) Jekyll.logger.error "❌ Failed: #{task[:original_name]} - #{e.}" end |
#retry_failed ⇒ Integer
Retry failed tasks
191 192 193 194 195 196 197 198 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 191 def retry_failed tasks = failed_tasks @mutex.synchronize do @failed.clear @queue.concat(tasks) end tasks.length end |
#status ⇒ Hash
Get queue status
161 162 163 164 165 166 167 168 169 170 |
# File 'lib/jekyll-imgflow/batch_manager.rb', line 161 def status @mutex.synchronize do { queued: @queue.length, completed: @completed.length, failed: @failed.length, total: @queue.length + @completed.length + @failed.length } end end |