jobstore

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 4 Imported by: 0

Documentation

Overview

Package jobstore holds the small, genuinely-shared slice of agent job persistence. The agents' job *schemas* differ substantially (duplicacy tracks chunk/dedup progress; gdrive tracks file counts and bytes), so there is no generic job table — only the boot-time orphan sweep is identical, and that is shared here.

MarkOrphanedFailed flips any job row still recorded as running/pending after a fresh boot to a terminal failed state. A JobHandle/goroutine is tied to a runtime instance, so a row still "running" after the process restarts is by definition an orphan from a prior crash/redeploy. Operating on the agent's LOCAL SQLite (not a remote query) means the sweep can't be defeated by the controller being unreachable at boot.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarkOrphanedFailed

func MarkOrphanedFailed(ctx context.Context, db *sql.DB, opts SweepOptions) ([]string, error)

MarkOrphanedFailed flips rows in RunningStates to FailedState and returns the affected job IDs (so the caller can enqueue synthetic terminal events for the controller). Returns (nil, nil) when there is nothing to sweep.

Types

type JobRef

type JobRef struct {
	ID       string `json:"id"`
	State    string `json:"state"`
	ExitCode int    `json:"exit_code"`
}

JobRef is the minimal job identity + state the reconcile snapshot carries to the controller. ExitCode is 0 when the column is NULL or absent. The controller derives any missing terminal detail (completed_at, default exit_code) itself — reconcile is a fallback for lost terminal events, not the primary data path, so a minimal, schema-agnostic shape is deliberate.

func ListForReconcile

func ListForReconcile(ctx context.Context, db *sql.DB, opts ListOptions) ([]JobRef, error)

ListForReconcile returns every non-terminal job plus the recent-terminal jobs from the agent's LOCAL store — the agent's authoritative declaration of "what I am running and what I just finished". The controller converges its view to this set (stuck-running rows the agent no longer reports get resolved). Shared here so both agents issue an identical query shape rather than copy-pasting it.

type ListOptions

type ListOptions struct {
	Table          string   // required
	IDColumn       string   // default "id"
	StateColumn    string   // default "state"
	ExitCodeColumn string   // optional; "" → ExitCode always 0
	RunningStates  []string // default {"running","pending"}
	TerminalStates []string // default {"completed","failed","cancelled"}

	// RecentTerminalPredicate is an SQL boolean fragment selecting recently
	// finished jobs, e.g. "completed_at_ns >= ?" (duplicacy) or
	// "completed_at >= ?" (gdrive). RecentTerminalArg is bound to its single
	// placeholder. When the predicate is empty, ALL terminal rows are returned
	// (caller is responsible for keeping the table bounded).
	RecentTerminalPredicate string
	RecentTerminalArg       any
}

ListOptions configures ListForReconcile. Table/column names are caller-supplied trusted identifiers (interpolated into SQL); values are bound parameters. The recent-terminal window predicate is caller-supplied because the agents' completion-time columns differ in type (duplicacy stores Unix nanoseconds as INTEGER, gdrive stores TIMESTAMP), so a single generic comparison expression can't be hardcoded here.

type SweepOptions

type SweepOptions struct {
	Table         string   // required
	IDColumn      string   // default "id"
	StateColumn   string   // default "state"
	RunningStates []string // default {"running","pending"}
	FailedState   string   // default "failed"

	// Optional terminal columns to set on swept rows.
	ErrorColumn     string // if set, set to ErrorMessage
	ErrorMessage    string
	ExitCodeColumn  string // if set, set to ExitCode
	ExitCode        int
	CompletedColumn string // if set, set to CURRENT_TIMESTAMP
}

SweepOptions configures MarkOrphanedFailed. Table/column names are caller-supplied trusted identifiers (interpolated into SQL); values are always bound parameters.

Jump to

Keyboard shortcuts

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