about summary refs log tree commit diff
path: root/src/main.rs
diff options
context:
space:
mode:
authorVincent Ambo <tazjin@gmail.com>2018-06-16T18·47+0200
committerVincent Ambo <github@tazj.in>2018-06-16T19·38+0200
commit54b03a8dada6e3b8eeac5a7b7e84249127063088 (patch)
tree609c4a26da287a27ec1872e4a0edb9358463dfb2 /src/main.rs
parentb6c06102786fad1c3dfdcfeb92f6b104d948f185 (diff)
fix(main): Attempt to read with next_record before waiting
Without this fix new records are only "pushed out" when something
appends to the journal.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs14
1 files changed, 13 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs
index d740cc1785a2..75e0274c41fe 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -220,6 +220,18 @@ impl From<JournalRecord> for LogEntry {
     }
 }
 
+/// Attempt to read from the journal. If no new entry is present,
+/// await the next one up to the specified timeout.
+fn receive_next_record(timeout: Duration, journal: &mut Journal)
+                       -> Result<Option<JournalRecord>> {
+    let next_record = journal.next_record()?;
+    if next_record.is_some() {
+        return Ok(next_record);
+    }
+
+    Ok(journal.await_next_record(Some(timeout))?)
+}
+
 /// This function starts a double-looped, blocking receiver. It will
 /// buffer messages for half a second before flushing them to
 /// Stackdriver.
@@ -239,7 +251,7 @@ fn receiver_loop(mut journal: Journal) -> Result<()> {
                 break;
             }
 
-            if let Ok(Some(entry)) = journal.await_next_record(Some(iteration)) {
+            if let Ok(Some(entry)) = receive_next_record(iteration, &mut journal) {
                 trace!("Received a new entry");
                 buf.push(entry.into());
             }