fix: while connected to MySQL if internet was lost no qso would be logged anymore
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
// Package offlineq is OpsLog's offline safety net.
|
||||
//
|
||||
// When the shared MySQL logbook is unreachable, a QSO must never be lost just
|
||||
// because the network blinked. Instead of failing, the QSO is appended to a
|
||||
// local ADIF file (the "outbox") and replayed into the database as soon as it
|
||||
// comes back — then the file is ARCHIVED, never deleted.
|
||||
//
|
||||
// Deliberately NOT a sync engine: it only ever PUSHES the operator's own QSOs.
|
||||
// There is no mirror, no pull, no merge, no tombstones — which is exactly why it
|
||||
// stays small. The cost, accepted by design: during an outage you don't see other
|
||||
// operators' QSOs and the worked-before check doesn't know about your pending
|
||||
// ones. See the queue view in the UI for what's waiting.
|
||||
//
|
||||
// The file lives in OpsLog's data directory — NEVER in a cloud-synced folder
|
||||
// (Seafile/OneDrive): replicating a live file byte-by-byte is what we're
|
||||
// escaping in the first place.
|
||||
package offlineq
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"hamlog/internal/adif"
|
||||
"hamlog/internal/qso"
|
||||
)
|
||||
|
||||
// FileName is the outbox. Pending QSOs accumulate here while the DB is down.
|
||||
const FileName = "opslog-pending.adi"
|
||||
|
||||
// Queue owns the outbox file. All operations are serialised: the logging path
|
||||
// (append) and the replay loop (read/rewrite/archive) run on different
|
||||
// goroutines.
|
||||
type Queue struct {
|
||||
dir string
|
||||
mu sync.Mutex
|
||||
}
|
||||
|
||||
// New returns a queue backed by <dir>/opslog-pending.adi.
|
||||
func New(dir string) *Queue { return &Queue{dir: strings.TrimSpace(dir)} }
|
||||
|
||||
// Path is the outbox file's location (shown in the UI so the operator always
|
||||
// knows where their QSOs physically are).
|
||||
func (q *Queue) Path() string { return filepath.Join(q.dir, FileName) }
|
||||
|
||||
// newQueueID mints the id that makes replay idempotent.
|
||||
func newQueueID() string {
|
||||
var b [12]byte
|
||||
if _, err := rand.Read(b[:]); err != nil {
|
||||
return fmt.Sprintf("t%d", time.Now().UnixNano())
|
||||
}
|
||||
return hex.EncodeToString(b[:])
|
||||
}
|
||||
|
||||
// Append parks a QSO in the outbox, stamping it with a queue id (returned) so a
|
||||
// repeated replay can recognise it. The file is created with an ADIF header on
|
||||
// first use, then appended to — an append can't corrupt what's already there.
|
||||
func (q *Queue) Append(rec qso.QSO) (string, error) {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
if rec.Extras == nil {
|
||||
rec.Extras = map[string]string{}
|
||||
}
|
||||
qid := strings.TrimSpace(rec.Extras[qso.OfflineQueueKey])
|
||||
if qid == "" {
|
||||
qid = newQueueID()
|
||||
rec.Extras[qso.OfflineQueueKey] = qid
|
||||
}
|
||||
|
||||
if err := os.MkdirAll(q.dir, 0o755); err != nil {
|
||||
return "", fmt.Errorf("offlineq: create dir: %w", err)
|
||||
}
|
||||
path := q.Path()
|
||||
_, statErr := os.Stat(path)
|
||||
f, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0o644)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("offlineq: open %s: %w", path, err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var b strings.Builder
|
||||
if os.IsNotExist(statErr) { // brand-new file → write the ADIF header once
|
||||
b.WriteString("OpsLog offline queue — QSOs logged while the database was unreachable.\n")
|
||||
b.WriteString("<ADIF_VER:5>3.1.4 <PROGRAMID:6>OpsLog <EOH>\n\n")
|
||||
}
|
||||
b.WriteString(strings.TrimRight(adif.FullRecordADIF(rec), "\r\n"))
|
||||
b.WriteString("\n")
|
||||
|
||||
if _, err := f.WriteString(b.String()); err != nil {
|
||||
return "", fmt.Errorf("offlineq: write: %w", err)
|
||||
}
|
||||
// Flush to disk: the whole point is surviving a crash/power cut.
|
||||
if err := f.Sync(); err != nil {
|
||||
return "", fmt.Errorf("offlineq: sync: %w", err)
|
||||
}
|
||||
return qid, nil
|
||||
}
|
||||
|
||||
// Pending parses the outbox into QSOs (each carrying its queue id in Extras).
|
||||
// A missing file simply means nothing is pending.
|
||||
func (q *Queue) Pending() ([]qso.QSO, error) {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
return q.pendingLocked()
|
||||
}
|
||||
|
||||
func (q *Queue) pendingLocked() ([]qso.QSO, error) {
|
||||
f, err := os.Open(q.Path())
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
var out []qso.QSO
|
||||
err = adif.Parse(f, func(rec adif.Record) error {
|
||||
if v, ok := adif.RecordToQSO(rec); ok {
|
||||
out = append(out, v)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
return out, fmt.Errorf("offlineq: parse %s: %w", q.Path(), err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Count is how many QSOs are waiting (0 when the file is absent).
|
||||
func (q *Queue) Count() int {
|
||||
p, err := q.Pending()
|
||||
if err != nil {
|
||||
return 0
|
||||
}
|
||||
return len(p)
|
||||
}
|
||||
|
||||
// Rewrite replaces the outbox with exactly these QSOs — used after a replay to
|
||||
// keep only the ones that FAILED. Written to a temp file and renamed, so a crash
|
||||
// mid-write can't truncate the queue.
|
||||
func (q *Queue) Rewrite(keep []qso.QSO) error {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
return q.rewriteLocked(keep)
|
||||
}
|
||||
|
||||
func (q *Queue) rewriteLocked(keep []qso.QSO) error {
|
||||
path := q.Path()
|
||||
if len(keep) == 0 {
|
||||
// Nothing left: remove the (now empty) outbox. The caller archives the
|
||||
// original content first, so this never destroys the only copy.
|
||||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("offlineq: remove: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("OpsLog offline queue — QSOs logged while the database was unreachable.\n")
|
||||
b.WriteString("<ADIF_VER:5>3.1.4 <PROGRAMID:6>OpsLog <EOH>\n\n")
|
||||
for _, v := range keep {
|
||||
b.WriteString(strings.TrimRight(adif.FullRecordADIF(v), "\r\n"))
|
||||
b.WriteString("\n")
|
||||
}
|
||||
tmp := path + ".tmp"
|
||||
if err := os.WriteFile(tmp, []byte(b.String()), 0o644); err != nil {
|
||||
return fmt.Errorf("offlineq: write temp: %w", err)
|
||||
}
|
||||
if err := os.Rename(tmp, path); err != nil {
|
||||
return fmt.Errorf("offlineq: replace: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Archive copies the outbox to a timestamped file BEFORE it is cleared, so the
|
||||
// QSOs always exist somewhere on disk even if the replay later turns out to have
|
||||
// gone wrong. Deleting the only copy of someone's contacts is the one mistake
|
||||
// you don't get to undo.
|
||||
func (q *Queue) Archive() (string, error) {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
|
||||
data, err := os.ReadFile(q.Path())
|
||||
if err != nil {
|
||||
if os.IsNotExist(err) {
|
||||
return "", nil
|
||||
}
|
||||
return "", err
|
||||
}
|
||||
name := fmt.Sprintf("opslog-pending-%s.adi", time.Now().Format("2006-01-02-1504"))
|
||||
dst := filepath.Join(q.dir, name)
|
||||
if err := os.WriteFile(dst, data, 0o644); err != nil {
|
||||
return "", fmt.Errorf("offlineq: archive: %w", err)
|
||||
}
|
||||
return dst, nil
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package offlineq
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"hamlog/internal/qso"
|
||||
)
|
||||
|
||||
// The outbox is the ONLY copy of a QSO logged while the database was down, so the
|
||||
// round-trip must be lossless: append → read back identical (callsign, band, mode,
|
||||
// date) and each record must carry a queue id (what makes the replay idempotent).
|
||||
func TestQueueRoundTrip(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
q := New(dir)
|
||||
|
||||
if n := q.Count(); n != 0 {
|
||||
t.Fatalf("fresh queue: count = %d, want 0", n)
|
||||
}
|
||||
|
||||
mk := func(call, band, mode string) qso.QSO {
|
||||
return qso.QSO{
|
||||
Callsign: call, Band: band, Mode: mode,
|
||||
QSODate: time.Date(2026, 7, 10, 12, 34, 0, 0, time.UTC),
|
||||
}
|
||||
}
|
||||
id1, err := q.Append(mk("K1ABC", "20m", "SSB"))
|
||||
if err != nil {
|
||||
t.Fatalf("append 1: %v", err)
|
||||
}
|
||||
id2, err := q.Append(mk("DL1XYZ", "40m", "CW"))
|
||||
if err != nil {
|
||||
t.Fatalf("append 2: %v", err)
|
||||
}
|
||||
if id1 == "" || id2 == "" || id1 == id2 {
|
||||
t.Fatalf("queue ids must be non-empty and distinct: %q / %q", id1, id2)
|
||||
}
|
||||
|
||||
pending, err := q.Pending()
|
||||
if err != nil {
|
||||
t.Fatalf("pending: %v", err)
|
||||
}
|
||||
if len(pending) != 2 {
|
||||
t.Fatalf("pending = %d, want 2", len(pending))
|
||||
}
|
||||
if pending[0].Callsign != "K1ABC" || pending[0].Band != "20m" || pending[0].Mode != "SSB" {
|
||||
t.Errorf("record 1 round-tripped wrong: %+v", pending[0])
|
||||
}
|
||||
if pending[1].Callsign != "DL1XYZ" || pending[1].Mode != "CW" {
|
||||
t.Errorf("record 2 round-tripped wrong: %+v", pending[1])
|
||||
}
|
||||
// The queue id must survive the ADIF round-trip — without it the replay
|
||||
// can't be idempotent and a crash would duplicate contacts.
|
||||
for i, p := range pending {
|
||||
if p.Extras[qso.OfflineQueueKey] == "" {
|
||||
t.Errorf("record %d lost its %s extra", i, qso.OfflineQueueKey)
|
||||
}
|
||||
}
|
||||
|
||||
// Archive keeps a copy BEFORE we clear anything.
|
||||
arch, err := q.Archive()
|
||||
if err != nil || arch == "" {
|
||||
t.Fatalf("archive: %v (path %q)", err, arch)
|
||||
}
|
||||
if _, err := os.Stat(arch); err != nil {
|
||||
t.Fatalf("archive file missing: %v", err)
|
||||
}
|
||||
|
||||
// Partial replay: the first QSO went in, the second failed → keep only it.
|
||||
if err := q.Rewrite(pending[1:]); err != nil {
|
||||
t.Fatalf("rewrite: %v", err)
|
||||
}
|
||||
left, err := q.Pending()
|
||||
if err != nil {
|
||||
t.Fatalf("pending after rewrite: %v", err)
|
||||
}
|
||||
if len(left) != 1 || left[0].Callsign != "DL1XYZ" {
|
||||
t.Fatalf("after rewrite: got %d records (%v), want just DL1XYZ", len(left), left)
|
||||
}
|
||||
|
||||
// Everything replayed → the outbox is removed (the archive still holds it).
|
||||
if err := q.Rewrite(nil); err != nil {
|
||||
t.Fatalf("rewrite empty: %v", err)
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(dir, FileName)); !os.IsNotExist(err) {
|
||||
t.Errorf("outbox should be gone once fully replayed, stat err = %v", err)
|
||||
}
|
||||
if n := q.Count(); n != 0 {
|
||||
t.Errorf("count after full replay = %d, want 0", n)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user