64 lines
2.1 KiB
Go
64 lines
2.1 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
)
|
|
|
|
// PurgeLeases löscht Leases, die vor mehr als olderThan abgelaufen sind.
|
|
func (s *Store) PurgeLeases(ctx context.Context, olderThan time.Duration) (int64, error) {
|
|
res, err := s.db.ExecContext(ctx, `DELETE FROM leases WHERE valid_until <= ?`, formatTime(s.now().Add(-olderThan)))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.RowsAffected()
|
|
}
|
|
|
|
// PurgeNodeCommands löscht zugestellte Befehle, die älter als olderThan sind.
|
|
func (s *Store) PurgeNodeCommands(ctx context.Context, olderThan time.Duration) (int64, error) {
|
|
res, err := s.db.ExecContext(ctx, `DELETE FROM node_commands WHERE sent_at IS NOT NULL AND sent_at <= ?`,
|
|
formatTime(s.now().Add(-olderThan)))
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.RowsAffected()
|
|
}
|
|
|
|
// Notified meldet, ob zum Ereignis key schon benachrichtigt wurde.
|
|
func (s *Store) Notified(ctx context.Context, key string) bool {
|
|
var one int
|
|
return s.db.QueryRowContext(ctx, `SELECT 1 FROM notifications WHERE key = ?`, key).Scan(&one) == nil
|
|
}
|
|
|
|
// RecordNotification vermerkt den Versand zu key (mehrfach unschädlich).
|
|
func (s *Store) RecordNotification(ctx context.Context, key string) error {
|
|
_, err := s.db.ExecContext(ctx, `INSERT OR IGNORE INTO notifications (key, sent_at) VALUES (?, ?)`,
|
|
key, formatTime(s.now()))
|
|
return err
|
|
}
|
|
|
|
// MarkNodeSeen setzt last_seen auf jetzt (z. B. beim Trennen: "offline seit").
|
|
func (s *Store) MarkNodeSeen(ctx context.Context, nodeID string) error {
|
|
res, err := s.db.ExecContext(ctx, `UPDATE nodes SET last_seen = ? WHERE id = ?`, formatTime(s.now()), nodeID)
|
|
return requireAffected(res, err, "Knoten "+nodeID)
|
|
}
|
|
|
|
// NotificationKeys liefert die vermerkten Ereignisse mit dem Präfix prefix.
|
|
func (s *Store) NotificationKeys(ctx context.Context, prefix string) ([]string, error) {
|
|
rows, err := s.db.QueryContext(ctx, `SELECT key FROM notifications WHERE substr(key, 1, length(?)) = ? ORDER BY key`,
|
|
prefix, prefix)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var keys []string
|
|
for rows.Next() {
|
|
var k string
|
|
if err := rows.Scan(&k); err != nil {
|
|
return nil, err
|
|
}
|
|
keys = append(keys, k)
|
|
}
|
|
return keys, rows.Err()
|
|
}
|