Sha256: 1f3630eff83557163ed76f1ac70662a83ee50533d95a3d4515b0074302bdf99a

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 KB

Contents

module Qwirk
  module Batch
    class AcquireFileStrategy
      def initialize(options)
        @glob         = options[:glob]
        raise "file_options glob value not set" unless @glob
        @poll_time    = (options[:poll_time] || 10.0).to_f
        # Ftp's could be in progress, make sure the file is at least 60 seconds old by default before processing
        @age          = (options[:age] || 60).to_i
        @stopped      = false
      end

      # Returns the next file or nil if stopped
      def next_file
        until @stopped
          Dir.glob(@glob).each do |file|
            unless file.match /\.(processing|completed)$/
              return file if (Time.now - File.mtime(file) > @age)
            end
          end
          @sleep_thread = Thread.current
          sleep @poll_time
        end
        return nil
      end

      def mark_file_as_processing(file)
        new_file = file + '.processing'
        File.rename(file, new_file)
        return new_file
      end

      def complete_file(file)
        file.match(/(.*)\.processing$/) || raise("#{file} is not currently being processed")
        new_file = $1 + '.completed'
        File.rename(file, new_file)
        return new_file
      end

      def stop
        @stopped = true
        @sleep_thread.wakeup if @sleep_thread
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
qwirk-0.2.4 lib/qwirk/batch/acquire_file_strategy.rb
qwirk-0.2.3 lib/qwirk/batch/acquire_file_strategy.rb
qwirk-0.2.2 lib/qwirk/batch/acquire_file_strategy.rb
qwirk-0.2.1 lib/qwirk/batch/acquire_file_strategy.rb
qwirk-0.2.0 lib/qwirk/batch/acquire_file_strategy.rb
qwirk-0.1.0 lib/qwirk/batch/acquire_file_strategy.rb
qwirk-0.0.1 lib/qwirk/batch/acquire_file_strategy.rb