mirror of
https://github.com/containers/podman.git
synced 2025-06-23 02:18:13 +08:00
Merge pull request #8164 from Luap99/journal-events
Improve the journal event reading
This commit is contained in:
@ -69,35 +69,39 @@ func (e EventJournalD) Read(ctx context.Context, options ReadOptions) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return errors.Wrapf(err, "failed to generate event options")
|
return errors.Wrapf(err, "failed to generate event options")
|
||||||
}
|
}
|
||||||
j, err := sdjournal.NewJournal() //nolint
|
j, err := sdjournal.NewJournal()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
// TODO AddMatch and Seek seem to conflict
|
|
||||||
// Issue filed upstream -> https://github.com/coreos/go-systemd/issues/315
|
// match only podman journal entries
|
||||||
// Leaving commented code in case upstream fixes things
|
podmanJournal := sdjournal.Match{Field: "SYSLOG_IDENTIFIER", Value: "podman"}
|
||||||
//podmanJournal := sdjournal.Match{Field: "SYSLOG_IDENTIFIER", Value: "podman"} //nolint
|
if err := j.AddMatch(podmanJournal.String()); err != nil {
|
||||||
//if err := j.AddMatch(podmanJournal.String()); err != nil {
|
return errors.Wrap(err, "failed to add journal filter for event log")
|
||||||
// return errors.Wrap(err, "failed to add filter for event log")
|
}
|
||||||
//}
|
|
||||||
if len(options.Since) == 0 && len(options.Until) == 0 && options.Stream {
|
if len(options.Since) == 0 && len(options.Until) == 0 && options.Stream {
|
||||||
if err := j.SeekTail(); err != nil {
|
if err := j.SeekTail(); err != nil {
|
||||||
return errors.Wrap(err, "failed to seek end of journal")
|
return errors.Wrap(err, "failed to seek end of journal")
|
||||||
}
|
}
|
||||||
} else {
|
// After SeekTail calling Next moves to a random entry.
|
||||||
podmanJournal := sdjournal.Match{Field: "SYSLOG_IDENTIFIER", Value: "podman"} //nolint
|
// To prevent this we have to call Previous first.
|
||||||
if err := j.AddMatch(podmanJournal.String()); err != nil {
|
// see: https://bugs.freedesktop.org/show_bug.cgi?id=64614
|
||||||
return errors.Wrap(err, "failed to add filter for event log")
|
if _, err := j.Previous(); err != nil {
|
||||||
|
return errors.Wrap(err, "failed to move journal cursor to previous entry")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// the api requires a next|prev before getting a cursor
|
// the api requires a next|prev before getting a cursor
|
||||||
if _, err := j.Next(); err != nil {
|
if _, err := j.Next(); err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to move journal cursor to next entry")
|
||||||
}
|
}
|
||||||
|
|
||||||
prevCursor, err := j.GetCursor()
|
prevCursor, err := j.GetCursor()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to get journal cursor")
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
@ -106,30 +110,26 @@ func (e EventJournalD) Read(ctx context.Context, options ReadOptions) error {
|
|||||||
default:
|
default:
|
||||||
// fallthrough
|
// fallthrough
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := j.Next(); err != nil {
|
if _, err := j.Next(); err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to move journal cursor to next entry")
|
||||||
}
|
}
|
||||||
newCursor, err := j.GetCursor()
|
newCursor, err := j.GetCursor()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to get journal cursor")
|
||||||
}
|
}
|
||||||
if prevCursor == newCursor {
|
if prevCursor == newCursor {
|
||||||
if len(options.Until) > 0 || !options.Stream {
|
if len(options.Until) > 0 || !options.Stream {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
_ = j.Wait(sdjournal.IndefiniteWait) //nolint
|
_ = j.Wait(sdjournal.IndefiniteWait)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
prevCursor = newCursor
|
prevCursor = newCursor
|
||||||
|
|
||||||
entry, err := j.GetEntry()
|
entry, err := j.GetEntry()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to read journal entry")
|
||||||
}
|
|
||||||
// TODO this keeps us from feeding the podman event parser with
|
|
||||||
// with regular journal content; it can be removed if the above
|
|
||||||
// problem with AddMatch is resolved.
|
|
||||||
if entry.Fields["PODMAN_EVENT"] == "" {
|
|
||||||
continue
|
|
||||||
}
|
}
|
||||||
newEvent, err := newEventFromJournalEntry(entry)
|
newEvent, err := newEventFromJournalEntry(entry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
Reference in New Issue
Block a user