Class: TimeFile

Inherits:
Object
  • Object
show all
Defined in:
lib/vcseif/utils/time_file.rb

Constant Summary collapse

ISO8601_FORMAT =

Store the last time the connector was run - just in case the connector stops and restarts Time (as UTC) is stored as a string in YYYY-MM-DD HH:MM:SS UTC“ format

"%Y-%m-%dT%H:%M:%S.%L%z"
STD_TS_FORMAT =
"%Y-%m-%d %H:%M:%S Z"
STD_STRPTIME_FMT =
"%Y-%m-%d %H:%M:%S %z"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, logger) ⇒ TimeFile

Returns a new instance of TimeFile.



21
22
23
24
# File 'lib/vcseif/utils/time_file.rb', line 21

def initialize(filename, logger)
  @filename = filename
  @log      = logger
end

Instance Attribute Details

#filenameObject (readonly)

Returns the value of attribute filename.



18
19
20
# File 'lib/vcseif/utils/time_file.rb', line 18

def filename
  @filename
end

#logObject (readonly)

Returns the value of attribute log.



19
20
21
# File 'lib/vcseif/utils/time_file.rb', line 19

def log
  @log
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/vcseif/utils/time_file.rb', line 26

def exists?
  return File.exists?(@filename)
end

#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

#write(timehack = nil) ⇒ Object



70
71
72
73
74
75
76
77
78
# File 'lib/vcseif/utils/time_file.rb', line 70

def write(timehack=nil)
  %{
     Writes the time (expected as a Ruby Time object)
     to the file in ISO 8601 format
   }
  timehack = Time.now if timehack.nil?
  stamp    = timehack.utc.strftime(STD_TS_FORMAT)
  File.open(@filename, 'w') { |file| file.puts "%s\n" % stamp }
end