sync

package
v1.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 24, 2026 License: BSD-3-Clause Imports: 30 Imported by: 0

Documentation

Overview

Package sync orchestrates data synchronization from PeeringDB into the local SQLite database using the ent ORM.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetCursor added in v1.2.0

func GetCursor(ctx context.Context, db *sql.DB, objType string) (time.Time, error)

GetCursor returns the last successful sync timestamp for a type. Returns zero time if no cursor exists or the last sync for the type failed.

func InitStatusTable

func InitStatusTable(ctx context.Context, db *sql.DB) error

InitStatusTable creates the sync_status and sync_cursors tables if they don't exist. These are not ent-managed entities; they store operational metadata via raw SQL.

func RecordSyncComplete

func RecordSyncComplete(ctx context.Context, db *sql.DB, id int64, status Status) error

RecordSyncComplete updates the sync status row with results.

func RecordSyncStart

func RecordSyncStart(ctx context.Context, db *sql.DB, startedAt time.Time) (int64, error)

RecordSyncStart inserts a new running sync status row and returns its ID.

func UpsertCursor added in v1.2.0

func UpsertCursor(ctx context.Context, db *sql.DB, objType string, lastSyncAt time.Time, status string) error

UpsertCursor updates or inserts the sync cursor for a type. Called AFTER the ent transaction commits successfully -- never inside the transaction.

Types

type Status added in v1.2.0

type Status struct {
	LastSyncAt   time.Time
	Duration     time.Duration
	ObjectCounts map[string]int // type -> count
	Status       string         // "success", "failed", "running"
	ErrorMessage string         // empty on success
}

Status represents the result of a sync operation.

func GetLastStatus added in v1.2.0

func GetLastStatus(ctx context.Context, db *sql.DB) (*Status, error)

GetLastStatus returns the most recent sync status. Returns nil if no sync has been recorded.

type Worker

type Worker struct {
	// contains filtered or unexported fields
}

Worker orchestrates PeeringDB data synchronization.

func NewWorker

func NewWorker(pdbClient *peeringdb.Client, entClient *ent.Client, db *sql.DB, cfg WorkerConfig, logger *slog.Logger) *Worker

NewWorker creates a new sync worker.

func (*Worker) HasCompletedSync

func (w *Worker) HasCompletedSync() bool

HasCompletedSync reports whether at least one successful sync has completed. Used for 503 behavior per D-30.

func (*Worker) SetRetryBackoffs

func (w *Worker) SetRetryBackoffs(backoffs []time.Duration)

SetRetryBackoffs overrides the default retry backoff durations. Intended for testing.

func (*Worker) StartScheduler

func (w *Worker) StartScheduler(ctx context.Context, interval time.Duration)

StartScheduler runs periodic sync via time.Ticker per D-22. It runs an initial sync immediately, then syncs on each tick. The scheduler uses the SyncMode from WorkerConfig. The scheduler stops when ctx is cancelled per CC-2.

func (*Worker) Sync

func (w *Worker) Sync(ctx context.Context, mode config.SyncMode) error

Sync executes a synchronization from PeeringDB to the local database. The mode parameter controls whether a full or incremental sync is performed. It acquires a mutex to prevent concurrent runs and wraps all changes in a single database transaction per D-19.

func (*Worker) SyncWithRetry

func (w *Worker) SyncWithRetry(ctx context.Context, mode config.SyncMode) error

SyncWithRetry calls Sync and retries on failure with exponential backoff per D-21.

type WorkerConfig

type WorkerConfig struct {
	IncludeDeleted bool
	IsPrimary      bool
	SyncMode       config.SyncMode
}

WorkerConfig holds configuration for the sync worker.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL