Sha256: 7a5d5cc22a8eda1fc46f0df039197b451ac2ad79d1c5705ca31a8b29036405a3

Contents?: true

Size: 1.43 KB

Versions: 4

Compression:

Stored size: 1.43 KB

Contents

module BatchManager
  class Executor
    include BatchManager::Utils

    class << self
      def exec(batch_file, options = {})
        batch_file_path = batch_full_path(batch_file)
        if File.exist?(batch_file_path)
          batch_status = BatchManager::BatchStatus.new(batch_file_path)
          if options[:force] || !options[:wet] || batch_status.can_run?
            exec_batch_script(batch_file_path, batch_status, options[:wet])
          else
            raise "Cannot run this batch."
          end
        else
          raise "File not exist."
        end
      end

      protected

      def exec_batch_script(batch_file_path, batch_status, is_wet)
        logger = BatchManager::Logger.new(batch_status.name, is_wet)
        write_log_header(is_wet)
        begin
          @wet = is_wet
          eval(File.read(batch_file_path))
          batch_status.update_schema if is_wet
        rescue => e
          logger.error e
          logger.error "Failed."
        ensure
          puts "Log saved at: #{BatchManager.logger.log_file}" if logger.log_file
          logger.close
        end
      end

      def write_log_header(is_wet)
        BatchManager.logger.info "=============================="
        BatchManager.logger.info "= #{is_wet ? 'WET' : 'DRY'} RUN"
        BatchManager.logger.info "= Ran at: #{Time.now.strftime("%Y-%m-%d %H:%M:%S")}"
        BatchManager.logger.info "=============================="
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
batch_manager-0.3.0 lib/batch_manager/executor.rb
batch_manager-0.2.8 lib/batch_manager/executor.rb
batch_manager-0.2.7 lib/batch_manager/executor.rb
batch_manager-0.2.6 lib/batch_manager/executor.rb