Method: TimeFile#read

Defined in:
lib/vcseif/utils/time_file.rb

#readObject



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
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/vcseif/utils/time_file.rb', line 30

def read()
  %{
     Returns the time stored in the file as an epoch seconds value
     or a default value of the time 5 minutes ago (again in epoch seconds form).
   }
  # default to 5 minutes ago if file non-existent or empty
  default = Time.now - (5 * 60)
  last_run_timestamp = nil

  if exists?
    begin
      File.open(@filename, "r") do |file|
        line = file.gets
        if !line.nil? and !line.empty?
          line = line.strip
          if line =~ /\d+.*T\d+:\d+:\d+.\d+/  # in old-style ISO8601 format
            last_run_timestamp = Time.strptime(line, ISO8601_FORMAT)
          else
            begin
              last_run_timestamp = Time.strptime(line, STD_STRPTIME_FMT)
            rescue => ex
              problem = "Invalid format for timefile entry: #{line}"
              action  = "reverting to default of #{last_run_timestamp}"
              @log.error("%s, %s" % [problem, action])
            end
          end
        end
      end
    rescue => ex
      problem = "Could not read time entry from #{@filename}"
      action  = "rewriting time file with default value"
      @log.error("%s, %s" % [problem, action])
      write(last_run_timestamp)
    end
  end

  last_run_timestamp = default if last_run_timestamp.nil?
  return last_run_timestamp
end