module Systemd::Journal::Waitable

Constants

IS_JRUBY

Public Instance Methods

wait(timeout_usec = -1, opts = {}) click to toggle source

Block until the journal is changed. @param timeout_usec [Integer] maximum number of microseconds to wait

or `-1` to wait indefinitely.

@example Wait for an event for a maximum of 3 seconds

j = Systemd::Journal.new
j.seek(:tail)
if j.wait(3 * 1_000_000)
  # event occurred
end

@return [Nil] the wait time was reached (no events occured). @return [Symbol] :append new entries were appened to the journal. @return [Symbol] :invalidate journal files were added/removed/rotated.

# File lib/systemd/journal/waitable.rb, line 18
def wait(timeout_usec = -1, opts = {})
  if opts[:select] && !IS_JRUBY
    wait_select(timeout_usec)
  else
    rc = Native.sd_journal_wait(@ptr, timeout_usec)
    raise JournalError, rc if rc.is_a?(Integer) && rc < 0
    rc == :nop ? nil : rc
  end
end
wait_select_reliable?() click to toggle source

Determine if calls to {#wait} with `select: true` will reliably wake when a change occurs. If false, there might be some (unknown) latency involved between when an change occurs and when {#wait} returns. @return [Boolean]

# File lib/systemd/journal/waitable.rb, line 32
def wait_select_reliable?
  Native.sd_journal_reliable_fd(@ptr) > 0
end
watch() { |current_entry while move_next)| ... } click to toggle source

Block and wait for new entries to be appended to the journal. When new entries are written, yields them in turn. Note that this function does not automatically seek to the end of the journal prior to waiting. This method Does not return. @example Print out events as they happen

j = Systemd::Journal.new
j.seek(:tail)
j.watch do |event|
  puts event.message
end
# File lib/systemd/journal/waitable.rb, line 46
def watch
  loop { (yield current_entry while move_next) if wait }
end

Private Instance Methods

file_descriptor() click to toggle source
# File lib/systemd/journal/waitable.rb, line 62
def file_descriptor
  fd = Native.sd_journal_get_fd(@ptr)
  raise JournalError, fd if fd < 0
  fd
end
io_object() click to toggle source
# File lib/systemd/journal/waitable.rb, line 58
def io_object
  @io ||= IO.new(file_descriptor, autoclose: false)
end
reason_for_wakeup() click to toggle source
# File lib/systemd/journal/waitable.rb, line 68
def reason_for_wakeup
  rc = Native.sd_journal_process(@ptr)
  raise JournalError, rc if rc.is_a?(Integer) && rc < 0
  rc == :nop ? nil : rc
end
wait_select(timeout_usec) click to toggle source
# File lib/systemd/journal/waitable.rb, line 52
def wait_select(timeout_usec)
  timeout_sec = (timeout_usec == -1 ? nil : timeout_usec / 1e6)
  r, *_ = IO.select([io_object], [], [], timeout_sec)
  r ? reason_for_wakeup : nil
end