class Kitcat::Framework

Attributes

last_item_processed[R]
logging[R]
migration_strategy[R]
number_of_items_processed[R]

Public Class Methods

new(migration_strategy, migration_name: nil, number_of_items_to_process: nil, progress_bar: true, progress_bar_output: STDOUT) click to toggle source

@params migration_strategy {Object}

  Instance implementing the methods of +Kitcat::Callbacks+

migration_name {String} Optional.
  The name of the migration. Used as tag in log file name. If not given, a random/unique one is used.

number_of_items_to_process {Integer} Optional.
  If given, the processing will stop after processing that many number of items.

progress_bar {Boolean} Optional.
  When +True+. it will instantiate a use a progress bar, incrementing that by 1 every time
  the +migration_strategy+ finishes processing an item. The total load will be calculated based on the
  result of +migration_strategy#criteria#count+.
  When +False+, progress bar will not be used

progress_bar_output Optional. Defaults to STDOUT. Anything that responds to
  #print, #flush, #tty? and #puts.
  It is taken into account only if progress bar is enabled.
# File lib/kitcat/framework.rb, line 38
def initialize(migration_strategy,
               migration_name: nil,
               number_of_items_to_process: nil,
               progress_bar: true,
               progress_bar_output: STDOUT)
  @migration_strategy         = migration_strategy
  @number_of_items_to_process = number_of_items_to_process
  @last_item_processed        = nil
  @progress_bar               = initialize_progress_bar(progress_bar, progress_bar_output)
  @logging                    = Kitcat::Logging.new(migration_strategy, migration_name)
end

Public Instance Methods

execute() click to toggle source
# File lib/kitcat/framework.rb, line 50
def execute
  trap_signals

  start_logging

  @number_of_items_processed = 0

  items.each do |item|
    break unless execute_for(item)
  end
ensure
  end_logging
end
number_of_items_to_process() click to toggle source
# File lib/kitcat/framework.rb, line 64
def number_of_items_to_process
  @number_of_items_to_process ||= migration_strategy.criteria.count
end
progress() click to toggle source
# File lib/kitcat/framework.rb, line 72
def progress
  return -1 unless progress_bar?
  @progress_bar.progress
end
progress_bar?() click to toggle source
# File lib/kitcat/framework.rb, line 68
def progress_bar?
  !@progress_bar.nil?
end

Private Instance Methods

commit_failure(item) click to toggle source
# File lib/kitcat/framework.rb, line 113
def commit_failure(item)
  log_failure(item)
end
commit_success(item) click to toggle source
# File lib/kitcat/framework.rb, line 103
def commit_success(item)
  log_success(item)

  @number_of_items_processed += 1

  increment_progress_bar

  @last_item_processed = item
end
create_progress_bar(output) click to toggle source
# File lib/kitcat/framework.rb, line 137
def create_progress_bar(output)
  @progress_bar = ProgressBar.create(total:  migration_strategy.criteria.count,
                                     output: output,
                                     progress_mark: ' ',
                                     remainder_mark: '-',
                                     length: terminal_width,
                                     format: '%a %bá—§%i %p%% %e')
end
execute_for(item) click to toggle source
# File lib/kitcat/framework.rb, line 79
def execute_for(item)
  begin
    if migration_strategy.process(item)

      commit_success(item)

      return false unless process_more?
    else
      commit_failure(item)

      return false
    end
    if @interrupted
      handle_user_interrupt
      return false
    end
  rescue StandardError
    commit_failure(item)
    raise
  end

  true
end
handle_user_interrupt() click to toggle source
# File lib/kitcat/framework.rb, line 155
def handle_user_interrupt
  log_interrupt_callback_start
  migration_strategy.interrupt_callback if migration_strategy.respond_to?(:interrupt_callback)
  log_interrupt_callback_finish
  true
end
increment_progress_bar() click to toggle source
# File lib/kitcat/framework.rb, line 150
def increment_progress_bar
  return unless progress_bar?
  @progress_bar.increment
end
initialize_progress_bar(progress_bar_flag, output) click to toggle source
# File lib/kitcat/framework.rb, line 133
def initialize_progress_bar(progress_bar_flag, output)
  create_progress_bar(output) if progress_bar_flag || progress_bar_flag.nil?
end
items() { |next| ... } click to toggle source
# File lib/kitcat/framework.rb, line 123
def items
  return enum_for(:items) unless block_given?

  enum = migration_strategy.criteria.each

  loop do
    yield enum.next
  end
end
process_more?() click to toggle source
# File lib/kitcat/framework.rb, line 146
def process_more?
  @number_of_items_to_process.nil? || @number_of_items_processed < @number_of_items_to_process
end
terminal_width() click to toggle source

The following is to correctly calculate the width of the terminal so that the progress bar occupies the whole width

# File lib/kitcat/framework.rb, line 165
def terminal_width
  TerminalWidthCalculator.calculate
end
trap_signals() click to toggle source
# File lib/kitcat/framework.rb, line 117
def trap_signals
  @interrupted = false
  Signal.trap('TERM') { @interrupted = true }
  Signal.trap('INT') { @interrupted = true }
end