Class: FtpFetcher

Inherits:
Net::FTP
  • Object
show all
Defined in:
lib/ftp_fetcher.rb

Instance Method Summary collapse

Instance Method Details

#connect_ftp_session(log) ⇒ Object

Connect to the ftp server



4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/ftp_fetcher.rb', line 4

def connect_ftp_session log
  begin
    #ftp_session = Net::FTP.new($config["echi_host"])
    ftp_session = Net::FTP.new
    ftp_session.connect($config["echi_host"], $config["echi_port"])
    ftp_session. $config["echi_username"], $config["echi_password"]
    log.info "Successfully connected to the ECHI FTP server"
  rescue => err
    log.info "Could not connect with the FTP server - " + err
    return -1
  end
  return ftp_session
end

#fetch_ftp_files(filequeue, log) ⇒ Object

Connect to the ftp server and fetch the files each time



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ftp_fetcher.rb', line 59

def fetch_ftp_files filequeue, log
  begin
    ftp_session = connect_ftp_session log
    if $config["echi_ftp_directory"] != nil
      ftp_session.chdir($config["echi_ftp_directory"])
    end
    while filequeue.empty? != true
      filename = filequeue.pop
      local_filename = $workingdir + '/../files/to_process/' + filename
      ftp_session.getbinaryfile(filename, local_filename)
      if $config["echi_ftp_delete"] == 'Y'
        ftp_session.delete(filename)
      end
    end
    ftp_session.close
    log.info "Closed ftp session."
    return true
  rescue => err
    log.info "Could not fetch from ftp server - " + err
    return false
  end
end

#fetch_list(log) ⇒ Object

Obtain the list of files available on the server



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ftp_fetcher.rb', line 19

def fetch_list log
  attempts = 0
  ftp_session = -1
  while ftp_session == -1 do
    ftp_session = connect_ftp_session log
    if ftp_session == -1
      sleep 5
    end
    attempts += 1
    if $config["echi_ftp_retry"] == attempts
      ftp_session = 0
    end
  end
  if ftp_session != 0
    begin
      if $config["echi_ftp_directory"] != nil
        ftp_session.chdir($config["echi_ftp_directory"])
      end
      files = ftp_session.list('chr*')

      #Also fetch the *.dat files if it is configured to be processed
      if $config["echi_process_dat_files"] == "Y"
        files = files + ftp_session.list("*.dat")
      end
      #Build the queue of files to be fetched for the threads
      filequeue = Queue.new
      files.each do |file|
        file_data = file.split(' ')
        filequeue.push(file_data[8])
      end
      ftp_session.close
      return filequeue
    rescue => err
      log.info "Unable to fetch list from ftp server - " + err
      return nil
    end
  end
end