Module: Droonga::Timestamp

Defined in:
lib/droonga/timestamp.rb

Constant Summary collapse

MICRO_SECONDS_DECIMAL_PLACE =
6

Class Method Summary collapse

Class Method Details

.last_message_timestampObject



50
51
52
53
54
55
56
# File 'lib/droonga/timestamp.rb', line 50

def last_message_timestamp
  file = Path.last_message_timestamp
  return nil unless file.exist?
  timestamp = file.read.strip
  return nil if timestamp.nil? or timestamp.empty?
  Time.parse(timestamp)
end

.last_message_timestamp=(timestamp) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/droonga/timestamp.rb', line 31

def last_message_timestamp=(timestamp)
  if timestamp.is_a?(String)
    timestamp = timestamp.strip
    if timestamp.empty?
      timestamp = nil
    else
      timestamp = Time.parse(timestamp)
    end
  end
  if timestamp
    timestamp = stringify(timestamp)
  else
    timestamp = ""
  end
  SafeFileWriter.write(Path.last_message_timestamp) do |output, file|
    output.puts(timestamp)
  end
end

.run_last_message_timestamp_observer(loop, &block) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/droonga/timestamp.rb', line 58

def run_last_message_timestamp_observer(loop, &block)
  path = Path.last_message_timestamp
  observer = FileObserver.new(loop, path)
  observer.on_change = lambda do
    yield(last_message_timestamp)
  end
  observer.start
  observer
end

.stringify(timestamp) ⇒ Object



27
28
29
# File 'lib/droonga/timestamp.rb', line 27

def stringify(timestamp)
  timestamp.utc.iso8601(MICRO_SECONDS_DECIMAL_PLACE)
end