archive

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 23 Imported by: 0

Documentation

Index

Constants

View Source
const MaxArchiveAfterWeeks = int((1<<63 - 1) / (7 * 24 * time.Hour))

MaxArchiveAfterWeeks bounds the cutoff calculation to a time.Duration.

Variables

View Source
var (
	ErrInvalidInput       = errors.New("invalid archive input")
	ErrClientNotFound     = errors.New("client not found in project")
	ErrArchiveNotFound    = errors.New("archive not found")
	ErrNotConfigured      = errors.New("archiving is not configured")
	ErrArchiveUnavailable = errors.New("archive content is unavailable")
)

Functions

func RunScheduler

func RunScheduler(ctx context.Context, service *Service)

Types

type Archive

type Archive struct {
	ID        uuid.UUID
	ClientID  uuid.UUID
	FileName  string
	SizeBytes int64
	SHA256    [32]byte
	LogCount  int64
	CutoffAt  time.Time
	FromAt    time.Time
	ToAt      time.Time
	CreatedAt time.Time
}

Archive describes a completed ZIP without exposing its storage location.

type Archiver

type Archiver interface {
	// GetPolicy returns a disabled policy when the client has no rule.
	GetPolicy(ctx context.Context, scope Scope) (Policy, error)
	// SetPolicy replaces the rule idempotently. Enabling or changing its age
	// schedules the first automatic run that many weeks from now; an unchanged
	// rule preserves its schedule. Disabling preserves archives and does not
	// cancel an in-progress Create; changes apply to the next build.
	SetPolicy(ctx context.Context, scope Scope, policy Policy) error
	// Create synchronously archives the fixed set of eligible logs. Only logs
	// safely written to the completed archive are deleted. An error does not
	// imply rollback: an interrupted commit can have an uncertain outcome.
	// Missing rules, empty cohorts and concurrent builds return skip outcomes,
	// not errors. Recovery runs under exclusive client ownership. Calling Create
	// directly neither waits for nor changes the automatic schedule.
	Create(ctx context.Context, scope Scope) (CreateResult, error)
	// List returns completed archives ordered by CreatedAt DESC, ID DESC.
	List(ctx context.Context, scope Scope, query ListQuery) (Page, error)
	// Open returns completed archive content. The caller must close it and
	// handle read errors; the stream does not promise seeking or Range support.
	Open(ctx context.Context, scope Scope, archiveID uuid.UUID) (Download, error)
}

Archiver manages client policies and completed archives. It validates client ownership, but callers are responsible for user authorization.

type CreateOutcome

type CreateOutcome string
const (
	Created         CreateOutcome = "created"
	SkippedNoLogs   CreateOutcome = "skipped_no_logs"
	SkippedDisabled CreateOutcome = "skipped_disabled"
	SkippedBusy     CreateOutcome = "skipped_busy"
)

type CreateResult

type CreateResult struct {
	Outcome CreateOutcome
	Archive *Archive // non-nil only for Created
}

type Cursor

type Cursor struct {
	CreatedAt time.Time
	ID        uuid.UUID
}

type DiskStore

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

func (*DiskStore) Backend

func (s *DiskStore) Backend() string

func (*DiskStore) Delete

func (s *DiskStore) Delete(ctx context.Context, key string) error

func (*DiskStore) Open

func (s *DiskStore) Open(ctx context.Context, key string, sizeBytes int64) (io.ReadCloser, error)

func (*DiskStore) Put

func (s *DiskStore) Put(ctx context.Context, key string, write func(io.Writer) error) (StoredObject, error)

type Download

type Download struct {
	Archive Archive
	Content io.ReadCloser
}

type ListQuery

type ListQuery struct {
	Limit  int     // zero defaults to 20; otherwise 1..100
	Before *Cursor // exclusive; nil starts at the newest archive
}

type Page

type Page struct {
	Items []Archive
	Next  *Cursor // nil when there are no more archives
}

type Policy

type Policy struct {
	Enabled           bool
	ArchiveAfterWeeks int       // age cutoff and automatic interval; 1..MaxArchiveAfterWeeks when enabled
	NextRunAt         time.Time // read-only persisted schedule; zero when disabled, ignored by SetPolicy
}

type S3Store

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

func (*S3Store) Backend

func (s *S3Store) Backend() string

func (*S3Store) Delete

func (s *S3Store) Delete(ctx context.Context, key string) error

func (*S3Store) Open

func (s *S3Store) Open(ctx context.Context, key string, sizeBytes int64) (io.ReadCloser, error)

func (*S3Store) Put

func (s *S3Store) Put(ctx context.Context, key string, write func(io.Writer) error) (StoredObject, error)

type Scope

type Scope struct {
	ProjectID uuid.UUID
	ClientID  uuid.UUID
}

type Service

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

func NewArchiveService

func NewArchiveService(pool *pgxpool.Pool, store Store) *Service

NewArchiveService uses store for archive content. A nil store permits metadata reads and policy disabling, but not enabling, creating or opening archives.

func (*Service) Create

func (a *Service) Create(ctx context.Context, scope Scope) (result CreateResult, err error)

func (*Service) GetPolicy

func (a *Service) GetPolicy(ctx context.Context, scope Scope) (Policy, error)

func (*Service) List

func (a *Service) List(ctx context.Context, scope Scope, query ListQuery) (Page, error)

func (*Service) Open

func (a *Service) Open(ctx context.Context, scope Scope, archiveID uuid.UUID) (Download, error)

func (*Service) RunDue

func (a *Service) RunDue(ctx context.Context) (processed int, locked bool, err error)

RunDue archives every due client while holding the scheduler lock, then schedules each one its policy's number of weeks later. locked is false when another instance is running a pass. Per-client failures are logged and retried after 15 minutes instead of waiting for the next full policy interval.

func (*Service) SetPolicy

func (a *Service) SetPolicy(ctx context.Context, scope Scope, policy Policy) error

type Store

type Store interface {
	// Backend is the stable storage_backend value persisted with each archive.
	Backend() string
	// Put calls write synchronously, at most once, with a streaming writer.
	// Success means write succeeded and all bytes are durably stored and available
	// through Open; metadata describes those exact bytes. An error may leave
	// content behind: callers must confirm database rollback before Delete.
	Put(ctx context.Context, key string, write func(io.Writer) error) (StoredObject, error)
	// Open rejects missing content or a size mismatch. The caller closes the
	// stream; seeking and Range support are not required.
	Open(ctx context.Context, key string, sizeBytes int64) (io.ReadCloser, error)
	// Delete removes both completed content and any staging artifacts for key.
	// It is idempotent for missing content. Callers must hold exclusive build
	// ownership and confirm the archive is neither ready nor possibly committed.
	Delete(ctx context.Context, key string) error
}

Store persists archive content independently of policies, ZIP generation and PostgreSQL. Keys are slash-separated relative object names, not public paths.

func NewDiskStore

func NewDiskStore(archiveDir string) Store

NewDiskStore returns nil for an empty directory, leaving storage unconfigured.

func NewS3Store

func NewS3Store(client *s3.Client, bucket string) (Store, error)

NewS3Store stores archives in an existing bucket. The client owns endpoint, credentials and retry configuration.

type StoredObject

type StoredObject struct {
	SizeBytes int64
	SHA256    [32]byte
}

Jump to

Keyboard shortcuts

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