Class: XmlConv::Util::SftpMission

Inherits:
Mission
  • Object
show all
Defined in:
lib/xmlconv/util/polling_manager.rb

Instance Attribute Summary collapse

Attributes inherited from Mission

#arguments, #backup_dir, #debug_recipients, #destination, #error_recipients, #filter, #partner, #postprocs, #reader, #tmp_destination, #writer

Instance Method Summary collapse

Methods inherited from Mission

#create_transaction, #filtered_transaction

Instance Attribute Details

#glob_patternObject

Returns the value of attribute glob_pattern.



134
135
136
# File 'lib/xmlconv/util/polling_manager.rb', line 134

def glob_pattern
  @glob_pattern
end

#originObject

Returns the value of attribute origin.



134
135
136
# File 'lib/xmlconv/util/polling_manager.rb', line 134

def origin
  @origin
end

Instance Method Details

#file_names(sftp, uri) ⇒ Object



135
136
137
138
139
140
141
# File 'lib/xmlconv/util/polling_manager.rb', line 135

def file_names(sftp, uri)
  pattern = @glob_pattern || '*'
  sftp.dir.entries(uri.path).collect do |entry|
    name = entry.name
    name if File.fnmatch pattern, name
  end.compact
end

#poll(&block) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/xmlconv/util/polling_manager.rb', line 142

def poll(&block)
  uri = URI.parse(@origin)
  require 'net/sftp'
  Net::SFTP.start(uri.host, uri.user,
                  :user_known_hosts_file => CONFIG.ssh_known_hosts_file,
                  :keys => CONFIG.ssh_identities) do |sftp|
    file_names(sftp, uri).each do |name|
      begin
        path = File.join uri.path, name
        origin = File.join @origin, name
        source = sftp.file.open path do |fh| fh.read end
        filtered_transaction source, origin do |trans|
          block.call trans
        end
      rescue Exception => e
        puts e
        puts e.backtrace
      ensure
        FileUtils.mkdir_p(@backup_dir)
        File.open File.join(@backup_dir, name), 'w' do |fh|
          fh.puts source
        end
        sftp.remove! path
      end
    end
  end
rescue NoMethodError
  ## prevent polling error notification for intermittent connection problems
end