maintenance

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 3, 2026 License: MPL-2.0 Imports: 33 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	JobRescuerRescueAfterDefault = time.Hour
	JobRescuerIntervalDefault    = 30 * time.Second
)
View Source
const (
	SQLiteNotificationCleanerIntervalDefault        = time.Minute
	SQLiteNotificationCleanerRetentionPeriodDefault = 5 * time.Minute
)
View Source
const (
	JobSchedulerIntervalDefault = 5 * time.Second
)
View Source
const (
	QueueRetentionPeriodDefault = 24 * time.Hour
)
View Source
const (
	// ReindexerTimeoutDefault is the default timeout of the reindexer.
	//
	// We've had user reports of builds taking 45 seconds on large tables, so
	// set a timeout of that plus a little margin. Use of `CONCURRENTLY` should
	// prevent index operations that run a little long from impacting work from
	// an operational standpoint.
	//
	// https://github.com/kumo-group/river/issues/909#issuecomment-2909949466
	ReindexerTimeoutDefault = 1 * time.Minute
)

Variables

View Source
var ErrNoJobToInsert = errors.New("a nil job was returned, nothing to insert")

ErrNoJobToInsert can be returned by a PeriodicJob's JobToInsertFunc to signal that there's no job to insert at this time.

Functions

func GetService

func GetService[T startstop.Service](maintainer *QueueMaintainer) T

GetService is a convenience method for getting a service by name and casting it to the desired type. It should only be used in tests due to its use of reflection and potential for panics.

Types

type DefaultReindexerSchedule

type DefaultReindexerSchedule struct{}

DefaultReindexerSchedule is a default schedule for the reindexer job which runs at midnight UTC daily.

func (*DefaultReindexerSchedule) Next

Next returns the next scheduled time for the reindexer job.

type InsertFunc

type InsertFunc func(ctx context.Context, tx riverdriver.ExecutorTx, insertParams []*rivertype.JobInsertParams) ([]*rivertype.JobInsertResult, error)

type JobCleaner

type JobCleaner struct {
	riversharedmaintenance.QueueMaintainerServiceBase
	startstop.BaseStartStop

	// exported for test purposes
	Config      *JobCleanerConfig
	TestSignals JobCleanerTestSignals
	// contains filtered or unexported fields
}

JobCleaner periodically removes finalized jobs that are cancelled, completed, or discarded. Each state's retention time can be configured individually.

func NewJobCleaner

func NewJobCleaner(archetype *baseservice.Archetype, config *JobCleanerConfig, exec riverdriver.Executor) *JobCleaner

func (*JobCleaner) Start

func (s *JobCleaner) Start(ctx context.Context) error

type JobCleanerConfig

type JobCleanerConfig struct {
	riversharedmaintenance.BatchSizes

	// CancelledJobRetentionPeriod is the amount of time to keep cancelled jobs
	// around before they're removed permanently.
	//
	// The special value -1 disables deletion of cancelled jobs.
	CancelledJobRetentionPeriod time.Duration

	// CompletedJobRetentionPeriod is the amount of time to keep completed jobs
	// around before they're removed permanently.
	//
	// The special value -1 disables deletion of completed jobs.
	CompletedJobRetentionPeriod time.Duration

	// DiscardedJobRetentionPeriod is the amount of time to keep cancelled jobs
	// around before they're removed permanently.
	//
	// The special value -1 disables deletion of discarded jobs.
	DiscardedJobRetentionPeriod time.Duration

	// Interval is the amount of time to wait between runs of the cleaner.
	Interval time.Duration

	// Pilot controls driver-level behavior that can be customized by plugins.
	Pilot riverpilot.Pilot

	// QueuesExcluded are queues that'll be excluded from cleaning.
	QueuesExcluded []string

	// Schema where River tables are located. Empty string omits schema, causing
	// Postgres to default to `search_path`.
	Schema string

	// Timeout of the individual queries in the job cleaner.
	Timeout time.Duration
}

type JobCleanerTestSignals

type JobCleanerTestSignals struct {
	DeletedBatch testsignal.TestSignal[struct{}] // notifies when runOnce finishes a pass
}

JobCleanerTestSignals are internal signals used exclusively in tests.

func (*JobCleanerTestSignals) Init

type JobRescuer

type JobRescuer struct {
	riversharedmaintenance.QueueMaintainerServiceBase
	startstop.BaseStartStop

	// exported for test purposes
	Config      *JobRescuerConfig
	TestSignals JobRescuerTestSignals
	// contains filtered or unexported fields
}

JobRescuer periodically rescues jobs that have been executing for too long and are considered to be "stuck".

func NewRescuer

func NewRescuer(archetype *baseservice.Archetype, config *JobRescuerConfig, exec riverdriver.Executor) *JobRescuer

func (*JobRescuer) Start

func (s *JobRescuer) Start(ctx context.Context) error

type JobRescuerConfig

type JobRescuerConfig struct {
	riversharedmaintenance.BatchSizes

	// ClientJobTimeout is the default job timeout used when a worker returns a
	// timeout of zero.
	ClientJobTimeout time.Duration

	// ClientRetryPolicy is the default retry policy to use for workers that don't
	// override NextRetry.
	ClientRetryPolicy jobexecutor.ClientRetryPolicy

	// Interval is the amount of time to wait between runs of the rescuer.
	Interval time.Duration

	// Pilot controls driver-level behavior that can be customized by plugins.
	Pilot riverpilot.Pilot

	// RescueAfter is the amount of time for a job to be active before it is
	// considered stuck and should be rescued.
	RescueAfter time.Duration

	// Schema where River tables are located. Empty string omits schema, causing
	// Postgres to default to `search_path`.
	Schema string

	WorkUnitFactoryFunc func(kind string) workunit.WorkUnitFactory
}

type JobRescuerTestSignals

type JobRescuerTestSignals struct {
	FetchedBatch testsignal.TestSignal[struct{}] // notifies when runOnce has fetched a batch of jobs
	UpdatedBatch testsignal.TestSignal[struct{}] // notifies when runOnce has updated rescued jobs from a batch
}

JobRescuerTestSignals are internal signals used exclusively in tests.

func (*JobRescuerTestSignals) Init

type JobScheduler

type JobScheduler struct {
	riversharedmaintenance.QueueMaintainerServiceBase
	startstop.BaseStartStop

	// exported for test purposes
	TestSignals JobSchedulerTestSignals
	// contains filtered or unexported fields
}

JobScheduler periodically moves jobs in `scheduled` or `retryable` state and which are ready to run over to `available` so that they're eligible to be worked.

func NewJobScheduler

func NewJobScheduler(archetype *baseservice.Archetype, config *JobSchedulerConfig, exec riverdriver.Executor) *JobScheduler

func (*JobScheduler) Start

func (s *JobScheduler) Start(ctx context.Context) error

type JobSchedulerConfig

type JobSchedulerConfig struct {
	riversharedmaintenance.BatchSizes

	// Interval is the amount of time between periodic checks for jobs to
	// be moved from "scheduled" to "available".
	Interval time.Duration

	// NotifyInsert is a function to call to emit notifications for queues
	// where jobs were scheduled.
	NotifyInsert NotifyInsertFunc

	// Schema where River tables are located. Empty string omits schema, causing
	// Postgres to default to `search_path`.
	Schema string
}

type JobSchedulerTestSignals

type JobSchedulerTestSignals struct {
	NotifiedQueues testsignal.TestSignal[[]string] // notifies when queues are sent an insert notification
	ScheduledBatch testsignal.TestSignal[struct{}] // notifies when runOnce finishes a pass
}

JobSchedulerTestSignals are internal signals used exclusively in tests.

func (*JobSchedulerTestSignals) Init

type NotifyInsertFunc

type NotifyInsertFunc func(ctx context.Context, execTx riverdriver.ExecutorTx, queues []string) error

NotifyInsertFunc is a function to call to emit notifications for queues where jobs were scheduled.

type PeriodicJob

type PeriodicJob struct {
	ID              string
	ConstructorFunc func() (*rivertype.JobInsertParams, error)
	RunOnStart      bool
	ScheduleFunc    func(time.Time) time.Time
	// contains filtered or unexported fields
}

PeriodicJob is a periodic job to be run. It's similar to the top-level river.PeriodicJobArgs, but needs a separate type because the enqueuer is in a subpackage.

type PeriodicJobEnqueuer

type PeriodicJobEnqueuer struct {
	riversharedmaintenance.QueueMaintainerServiceBase
	startstop.BaseStartStop

	// exported for test purposes
	Config      *PeriodicJobEnqueuerConfig
	TestSignals PeriodicJobEnqueuerTestSignals
	// contains filtered or unexported fields
}

PeriodicJobEnqueuer inserts jobs configured to run periodically as unique jobs to make sure they'll run as frequently as their period dictates.

func (*PeriodicJobEnqueuer) AddManySafely

func (s *PeriodicJobEnqueuer) AddManySafely(periodicJobs []*PeriodicJob) ([]rivertype.PeriodicJobHandle, error)

AddManySafely adds many new periodic job to the enqueuer. The service's run loop is woken immediately so that the job is scheduled appropriately, and inserted if any RunOnStart flags are set to true.

func (*PeriodicJobEnqueuer) AddSafely

func (s *PeriodicJobEnqueuer) AddSafely(periodicJob *PeriodicJob) (rivertype.PeriodicJobHandle, error)

AddSafely adds a new periodic job to the enqueuer. The service's run loop is woken immediately so that the job is scheduled appropriately, and inserted if its RunOnStart flag is set to true.

func (*PeriodicJobEnqueuer) Clear

func (s *PeriodicJobEnqueuer) Clear()

Clear clears all periodic jobs from the enqueuer.

func (*PeriodicJobEnqueuer) Remove

func (s *PeriodicJobEnqueuer) Remove(periodicJobHandle rivertype.PeriodicJobHandle)

Remove removes a periodic job from the enqueuer. Its current target run time and all future runs are cancelled.

func (*PeriodicJobEnqueuer) RemoveByID

func (s *PeriodicJobEnqueuer) RemoveByID(id string) bool

RemoveByID removes a periodic job from the enqueuer by ID. Its current target run time and all future runs are cancelled.

func (*PeriodicJobEnqueuer) RemoveMany

func (s *PeriodicJobEnqueuer) RemoveMany(periodicJobHandles []rivertype.PeriodicJobHandle)

RemoveMany removes many periodic jobs from the enqueuer. Their current target run time and all future runs are cancelled.

func (*PeriodicJobEnqueuer) RemoveManyByID

func (s *PeriodicJobEnqueuer) RemoveManyByID(ids []string)

RemoveManyByID removes many periodic jobs from the enqueuer by ID. Their current target run time and all future runs are cancelled.

func (*PeriodicJobEnqueuer) Start

func (s *PeriodicJobEnqueuer) Start(ctx context.Context) error

type PeriodicJobEnqueuerConfig

type PeriodicJobEnqueuerConfig struct {
	AdvisoryLockPrefix int32

	PluginLookupGlobal *pluginlookup.PluginLookup

	// Insert is the function to call to insert jobs into the database.
	Insert InsertFunc

	// PeriodicJobs are the periodic jobs with which to configure the enqueuer.
	PeriodicJobs []*PeriodicJob

	// Pilot is a plugin module providing additional non-standard functionality.
	Pilot riverpilot.PilotPeriodicJob

	// Schema where River tables are located. Empty string omits schema, causing
	// Postgres to default to `search_path`.
	Schema string
}

type PeriodicJobEnqueuerTestSignals

type PeriodicJobEnqueuerTestSignals struct {
	EnteredLoop                 testsignal.TestSignal[struct{}] // notifies when the enqueuer finishes start up and enters its initial run loop
	InsertedJobs                testsignal.TestSignal[struct{}] // notifies when a batch of jobs is inserted
	PeriodicJobKeepAliveAndReap testsignal.TestSignal[struct{}] // notifies when the background services that runs keep alive and reap on periodic jobs ticks
	PeriodicJobUpserted         testsignal.TestSignal[struct{}] // notifies when a batch of periodic job records are upserted to pilot
	SkippedJob                  testsignal.TestSignal[struct{}] // notifies when a job is skipped because of nil JobInsertParams
}

PeriodicJobEnqueuerTestSignals are internal signals used exclusively in tests.

func (*PeriodicJobEnqueuerTestSignals) Init

type QueueCleaner

type QueueCleaner struct {
	riversharedmaintenance.QueueMaintainerServiceBase
	startstop.BaseStartStop

	// exported for test purposes
	Config      *QueueCleanerConfig
	TestSignals QueueCleanerTestSignals
	// contains filtered or unexported fields
}

QueueCleaner periodically removes queues from the river_queue table that have not been updated in a while, indicating that they are no longer active.

func NewQueueCleaner

func NewQueueCleaner(archetype *baseservice.Archetype, config *QueueCleanerConfig, exec riverdriver.Executor) *QueueCleaner

func (*QueueCleaner) Start

func (s *QueueCleaner) Start(ctx context.Context) error

type QueueCleanerConfig

type QueueCleanerConfig struct {
	riversharedmaintenance.BatchSizes

	// Interval is the amount of time to wait between runs of the cleaner.
	Interval time.Duration

	// RetentionPeriod is the amount of time to keep queues around before they're
	// removed.
	RetentionPeriod time.Duration

	// Schema where River tables are located. Empty string omits schema, causing
	// Postgres to default to `search_path`.
	Schema string
}

type QueueCleanerTestSignals

type QueueCleanerTestSignals struct {
	DeletedBatch testsignal.TestSignal[struct{}] // notifies when runOnce finishes a pass
}

QueueCleanerTestSignals are internal signals used exclusively in tests.

func (*QueueCleanerTestSignals) Init

type QueueMaintainer

type QueueMaintainer struct {
	baseservice.BaseService
	startstop.BaseStartStop
	// contains filtered or unexported fields
}

QueueMaintainer runs regular maintenance operations against job queues, like pruning completed jobs. It runs only on the client which has been elected leader at any given time.

Its methods are not safe for concurrent usage.

func NewQueueMaintainer

func NewQueueMaintainer(archetype *baseservice.Archetype, services []startstop.Service) *QueueMaintainer

func (*QueueMaintainer) StaggerStartupDisable

func (m *QueueMaintainer) StaggerStartupDisable(disabled bool)

StaggerStartupDisable sets whether the short staggered sleep on start up is disabled. This is useful in tests where the extra sleep involved in a staggered start up is not helpful for test run time.

func (*QueueMaintainer) Start

func (m *QueueMaintainer) Start(ctx context.Context) error

type QueueMaintainerLeader

type QueueMaintainerLeader struct {
	baseservice.BaseService
	startstop.BaseStartStop

	// exported for test purposes
	TestSignals QueueMaintainerLeaderTestSignals
	// contains filtered or unexported fields
}

QueueMaintainerLeader listens for leadership changes and starts/stops the queue maintainer accordingly. It handles retries with exponential backoff on start failures, and requests leader resignation when all retries are exhausted. This is extracted to a separate struct because to get all the edge cases right, it ends up being a fair bit of code that would otherwise make Client fairly heavy.

func NewQueueMaintainerLeader

func NewQueueMaintainerLeader(archetype *baseservice.Archetype, config *QueueMaintainerLeaderConfig) *QueueMaintainerLeader

func (*QueueMaintainerLeader) Start

type QueueMaintainerLeaderConfig

type QueueMaintainerLeaderConfig struct {
	// ClientID is used for logging on leadership changes.
	ClientID string

	// Elector provides leadership change notifications.
	Elector *leadership.Elector

	// QueueMaintainer is the underlying maintainer to start/stop on leadership
	// changes.
	QueueMaintainer *QueueMaintainer
}

QueueMaintainerLeaderConfig is the configuration for QueueMaintainerLeader.

type QueueMaintainerLeaderTestSignals

type QueueMaintainerLeaderTestSignals struct {
	ElectedLeader         testsignal.TestSignal[struct{}] // notifies when elected leader
	StartError            testsignal.TestSignal[error]    // notifies on each failed queue maintainer start attempt
	StartRetriesExhausted testsignal.TestSignal[struct{}] // notifies when all start retries have been exhausted
}

QueueMaintainerLeaderTestSignals are internal signals used exclusively in tests.

func (*QueueMaintainerLeaderTestSignals) Init

type Reindexer

type Reindexer struct {
	riversharedmaintenance.QueueMaintainerServiceBase
	startstop.BaseStartStop

	// exported for test purposes
	Config      *ReindexerConfig
	TestSignals ReindexerTestSignals
	// contains filtered or unexported fields
}

Reindexer periodically executes a REINDEX command on the important job indexes to rebuild them and fix bloat issues.

func NewReindexer

func NewReindexer(archetype *baseservice.Archetype, config *ReindexerConfig, exec riverdriver.Executor) *Reindexer

func (*Reindexer) Start

func (s *Reindexer) Start(ctx context.Context) error

type ReindexerConfig

type ReindexerConfig struct {
	// IndexNames is the exact list of indexes to reindex on each run. It must
	// be non-nil. An empty slice disables reindex work.
	IndexNames []string

	// ScheduleFunc returns the next scheduled run time for the reindexer given the
	// current time.
	ScheduleFunc func(time.Time) time.Time

	// Schema where River tables are located. Empty string omits schema, causing
	// Postgres to default to `search_path`.
	Schema string

	// Timeout is the amount of time to wait for a single reindex query to run
	// before cancelling it via context.
	Timeout time.Duration
}

type ReindexerTestSignals

type ReindexerTestSignals struct {
	Reindexed testsignal.TestSignal[struct{}] // notifies when a run finishes executing reindexes for all indexes
}

ReindexerTestSignals are internal signals used exclusively in tests.

func (*ReindexerTestSignals) Init

type SQLiteNotificationCleaner

type SQLiteNotificationCleaner struct {
	riversharedmaintenance.QueueMaintainerServiceBase
	startstop.BaseStartStop

	// exported for test purposes
	Config      *SQLiteNotificationCleanerConfig
	TestSignals SQLiteNotificationCleanerTestSignals
	// contains filtered or unexported fields
}

SQLiteNotificationCleaner periodically removes old rows from SQLite's notification outbox. It is only needed for the SQLite driver's emulated listen/notify support.

func NewSQLiteNotificationCleaner

func NewSQLiteNotificationCleaner(archetype *baseservice.Archetype, config *SQLiteNotificationCleanerConfig, exec riverdriver.Executor) *SQLiteNotificationCleaner

NewSQLiteNotificationCleaner returns a SQLite notification cleaner.

func (*SQLiteNotificationCleaner) Start

type SQLiteNotificationCleanerConfig

type SQLiteNotificationCleanerConfig struct {
	riversharedmaintenance.BatchSizes

	// Interval is the amount of time to wait between cleaner runs.
	Interval time.Duration

	// RetentionPeriod is the amount of time to keep notification rows around
	// before they're removed.
	RetentionPeriod time.Duration

	// Schema where River tables are located. Empty string omits schema.
	Schema string

	// Timeout is the timeout for each delete query.
	Timeout time.Duration
}

type SQLiteNotificationCleanerTestSignals

type SQLiteNotificationCleanerTestSignals struct {
	DeletedBatch testsignal.TestSignal[struct{}] // notifies when a delete batch finishes
}

SQLiteNotificationCleanerTestSignals are internal signals used exclusively in tests.

func (*SQLiteNotificationCleanerTestSignals) Init

Jump to

Keyboard shortcuts

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