github.com/rotari-dev/rotari/internal/notificationlog

Overview

package notificationlog // import "github.com/rotari-dev/rotari/internal/notificationlog"

Package notificationlog keeps the notifications that runs produced, so the Web
inbox can show those that happened while no page was open.

The master directory holds one file per run, <masterdir>/notifications/
<run-id>.jsonl, with one record per notification batch. Only the run's own
process appends to a file while it runs, so concurrent runs never write the same
file, even on NFS; once it has stopped, rotari web may add one run.interrupted
record through AppendOnce. Records are kept for Retention; inbox-state.json
holds the shared read and cleared cursors of each basedir.

Constants

const Retention = 10 * 24 * time.Hour
    Retention is how long records are kept after their run's last record.

Types

type Cursor struct {
	CreatedAt string `json:"created_at"`
	ID        string `json:"id"`
}
    Cursor is a position in the record order: by creation time, then ID.

func (cursor Cursor) After(other Cursor) bool
    After reports whether cursor comes after other. The zero cursor comes before
    every record.

func (cursor Cursor) Valid() bool
    Valid reports whether cursor is the zero cursor or a record position.

type Cursors struct {
	Read    Cursor `json:"read"`
	Cleared Cursor `json:"cleared"`
}
    Cursors is one basedir's inbox state: its records up to Read are read,
    and its records up to Cleared are no longer shown.

func (cursors Cursors) Valid() bool
    Valid reports whether both cursors are valid.

type Entry struct {
	Cursor
	BaseDir string
	Project string
	RunID   string
	notification.BrowserMessage
}
    Entry is one record as the Web inbox and `rotari notifications` show it.

func Entries(records []Record, limit int) []Entry
    Entries renders records, in their order, with each project's current browser
    settings, up to limit entries (no limit when limit is 0). A record whose
    events the settings all leave out gives no entry; a project whose settings
    cannot be read gives an entry that says so.

type InboxState struct {
	// BaseDirs maps an absolute basedir to its cursors.
	BaseDirs map[string]Cursors `json:"basedirs"`
}
    InboxState is the inbox's shared state, kept per basedir so that reading or
    clearing the records of the basedirs a page shows leaves the others alone.

func (inbox InboxState) For(baseDir string) Cursors
    For returns baseDir's cursors; a basedir never read has zero cursors.

type Log struct {
	// Has unexported fields.
}
    Log is the record store under one master directory.

func Default() (Log, error)
    Default returns the log under the resolved master directory; see
    state.ResolveMasterDir.

func Open(masterDir string) Log
    Open returns the log under masterDir.

func (log Log) Advance(changes map[string]Cursors) (InboxState, error)
    Advance moves the cursors of each basedir in changes forward; other basedirs
    keep theirs.

func (log Log) Append(baseDir string, events []notification.Event) error
    Append records events, all of one run under baseDir, as one record, then
    removes run files past Retention. The record is written with a single write,
    so a reader sees it whole or not at all.

func (log Log) AppendOnce(baseDir string, event notification.Event) (bool, error)
    AppendOnce appends event, of one run under baseDir, unless the run's file
    already records an event of its kind. Callers of AppendOnce, in this and
    other processes, take turns, so the event is recorded once. It reports
    whether it appended.

func (log Log) Records() (records []Record, problems []string, err error)
    Records returns the kept records, newest first. A record that cannot be read
    is left out and described in problems; an incomplete last line, a record
    still being written, is not a problem.

func (log Log) State() (InboxState, error)
    State returns the shared inbox state.

type Record struct {
	SchemaVersion int                  `json:"schema_version"`
	ID            string               `json:"id"`
	CreatedAt     string               `json:"created_at"`
	BaseDir       string               `json:"base_dir"`
	Project       string               `json:"project"`
	RunID         string               `json:"run_id"`
	RunName       string               `json:"run_name,omitempty"`
	Events        []notification.Event `json:"events"`
}
    Record is one notification batch of one run, with every event the run
    produced in it before any channel's selection.

func (record Record) Cursor() Cursor
    Cursor returns the record's position.