cron

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package cron provides cron job scheduling using the robfig/cron library

Package cron provides service integration for cron scheduling

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CleanupServiceInterface

type CleanupServiceInterface interface {
	RunOnce(ctx context.Context) error
}

CleanupServiceInterface defines the interface for cleanup services

type Config

type Config struct {
	// PasswordResetCleanupCron is the cron expression for password reset token cleanup
	// Default: "*/5 * * * *" (every 5 minutes)
	PasswordResetCleanupCron string
	// TimeZone for cron jobs (default: UTC)
	TimeZone string
}

Config holds configuration for the cron scheduler

func DefaultConfig

func DefaultConfig() Config

DefaultConfig returns default configuration

type InstrumentedScheduler added in v0.4.0

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

InstrumentedScheduler wraps the cron scheduler with metrics collection

func NewInstrumentedScheduler added in v0.4.0

func NewInstrumentedScheduler(config Config, logger *slog.Logger, systemMetrics *metrics.SystemHealthMetrics) (*InstrumentedScheduler, error)

NewInstrumentedScheduler creates a new instrumented cron scheduler

func (*InstrumentedScheduler) AddJob added in v0.4.0

func (is *InstrumentedScheduler) AddJob(cronExpr string, jobName string, job func()) error

AddJob adds an instrumented generic cron job

func (*InstrumentedScheduler) AddJobWithError added in v0.4.0

func (is *InstrumentedScheduler) AddJobWithError(cronExpr string, jobName string, job func() error) error

AddJobWithError adds an instrumented cron job that can return an error

func (*InstrumentedScheduler) AddPasswordResetCleanupJob added in v0.4.0

func (is *InstrumentedScheduler) AddPasswordResetCleanupJob(cronExpr string, cleanupService CleanupServiceInterface) error

AddPasswordResetCleanupJob adds an instrumented password reset cleanup job

func (*InstrumentedScheduler) AddPasswordResetCleanupJobWithService added in v0.4.0

func (is *InstrumentedScheduler) AddPasswordResetCleanupJobWithService(cronExpr string, cleanupService *reset.CleanupService) error

AddPasswordResetCleanupJobWithService adds an instrumented password reset cleanup job with *reset.CleanupService

func (*InstrumentedScheduler) GetActiveJobCount added in v0.4.0

func (is *InstrumentedScheduler) GetActiveJobCount() int64

GetActiveJobCount returns the current number of running cron jobs

func (*InstrumentedScheduler) GetEntries added in v0.4.0

func (is *InstrumentedScheduler) GetEntries() []cron.Entry

GetEntries returns information about scheduled jobs

func (*InstrumentedScheduler) GetSystemHealthCallbacks added in v0.4.0

func (is *InstrumentedScheduler) GetSystemHealthCallbacks() (
	getActiveJobCount func() int64,
	getTotalJobCount func() int64,
)

GetSystemHealthCallbacks returns callback functions for system health metrics

func (*InstrumentedScheduler) GetSystemStatus added in v0.4.0

func (is *InstrumentedScheduler) GetSystemStatus() int64

GetSystemStatus returns the overall cron system health status Returns 1 if healthy, 0 if unhealthy

func (*InstrumentedScheduler) GetTotalJobCount added in v0.4.0

func (is *InstrumentedScheduler) GetTotalJobCount() int64

GetTotalJobCount returns the total number of scheduled cron jobs

func (*InstrumentedScheduler) Start added in v0.4.0

func (is *InstrumentedScheduler) Start()

Start starts the instrumented cron scheduler

func (*InstrumentedScheduler) Stop added in v0.4.0

func (is *InstrumentedScheduler) Stop()

Stop stops the instrumented cron scheduler

type JobInfo

type JobInfo struct {
	ID       cron.EntryID
	Next     time.Time
	Prev     time.Time
	Valid    bool
	Schedule string
}

JobInfo represents information about a scheduled job

type Scheduler

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

Scheduler manages cron jobs using the robfig/cron library

func NewScheduler

func NewScheduler(config Config, logger *slog.Logger) (*Scheduler, error)

NewScheduler creates a new cron scheduler

func (*Scheduler) AddJob

func (s *Scheduler) AddJob(cronExpr string, jobName string, job func()) error

AddJob adds a generic cron job

func (*Scheduler) AddPasswordResetCleanupJob

func (s *Scheduler) AddPasswordResetCleanupJob(cronExpr string, cleanupService CleanupServiceInterface) error

AddPasswordResetCleanupJob adds a job to clean up expired password reset tokens

func (*Scheduler) AddPasswordResetCleanupJobWithService

func (s *Scheduler) AddPasswordResetCleanupJobWithService(cronExpr string, cleanupService *reset.CleanupService) error

AddPasswordResetCleanupJobWithService is a convenience method that accepts *reset.CleanupService directly

func (*Scheduler) GetEntries

func (s *Scheduler) GetEntries() []cron.Entry

GetEntries returns information about scheduled jobs

func (*Scheduler) Start

func (s *Scheduler) Start()

Start starts the cron scheduler

func (*Scheduler) Stop

func (s *Scheduler) Stop()

Stop stops the cron scheduler gracefully

type Service

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

Service manages cron jobs for the application

func NewService

func NewService(config ServiceConfig, logger *slog.Logger) (*Service, error)

NewService creates a new cron service

func (*Service) AddCustomJob

func (s *Service) AddCustomJob(cronExpr string, jobName string, job func()) error

AddCustomJob adds a custom cron job

func (*Service) GetJobEntries

func (s *Service) GetJobEntries() []JobInfo

GetJobEntries returns information about scheduled jobs

func (*Service) IsEnabled

func (s *Service) IsEnabled() bool

IsEnabled returns whether the cron service is enabled

func (*Service) RunPasswordResetCleanupOnce

func (s *Service) RunPasswordResetCleanupOnce(ctx context.Context, queries models.Querier) error

RunPasswordResetCleanupOnce runs the password reset cleanup job once (useful for testing)

func (*Service) SetupPasswordResetCleanup

func (s *Service) SetupPasswordResetCleanup(queries models.Querier, config ServiceConfig) error

SetupPasswordResetCleanup configures password reset token cleanup job

func (*Service) Start

func (s *Service) Start() error

Start starts the cron service and all scheduled jobs

func (*Service) Stop

func (s *Service) Stop()

Stop stops the cron service and all scheduled jobs

type ServiceConfig

type ServiceConfig struct {
	// PasswordResetCleanupCron is the cron expression for password reset token cleanup
	// Default: "*/5 * * * *" (every 5 minutes)
	PasswordResetCleanupCron string
	// TimeZone for cron jobs (default: UTC)
	TimeZone string
	// Enabled determines if cron jobs should run
	Enabled bool
}

ServiceConfig holds configuration for the cron service

func LoadServiceConfigFromViper

func LoadServiceConfigFromViper() ServiceConfig

LoadServiceConfigFromViper loads cron service configuration from viper

Jump to

Keyboard shortcuts

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