Files
lincodex/backend/config/jobs.go
T

100 lines
4.4 KiB
Go

package config
import (
"errors"
"fmt"
"time"
)
const (
// The scheduled job worker runs by default. It starts only when a job
// handler is registered and DATABASE_URL is set.
defaultJobsEnabled = true
// defaultJobsPollInterval is the wait after a pass that claimed less
// than a batch. It is also the usual lateness of a job after its due
// time.
defaultJobsPollInterval = time.Second
minJobsPollInterval = 10 * time.Millisecond
maxJobsPollInterval = 5 * time.Minute
// defaultJobsBatchSize bounds the jobs one claim leases. A worker runs
// them one after another under leases taken together, so a batch stays
// small next to the lease.
defaultJobsBatchSize = 10
maxJobsBatchSize = 100
// defaultJobsLeaseDuration is how long a claimed job belongs to one
// worker. It bounds one handler attempt, and a worker that crashed
// delays its jobs by at most this long.
defaultJobsLeaseDuration = time.Minute
minJobsLeaseDuration = time.Second
maxJobsLeaseDuration = time.Hour
// defaultJobsMaxAttempts bounds the attempts of a job scheduled without
// its own limit before it is dead.
defaultJobsMaxAttempts = 5
// maxJobsMaxAttempts matches the database bound on a per-job limit.
maxJobsMaxAttempts = 100
// A failed job is retried after a delay that starts at the initial
// value, doubles per failed attempt, and stops growing at the maximum.
defaultJobsBackoffInitial = time.Second
defaultJobsBackoffMax = 5 * time.Minute
maxJobsBackoff = 24 * time.Hour
// defaultJobsShutdownGrace is how long the job in progress may continue
// after shutdown begins.
defaultJobsShutdownGrace = 5 * time.Second
)
func (config *Config) loadJobs(lookup LookupEnv) error {
var err error
if config.JobsEnabled, err = boolOrDefault(lookup, "JOBS_ENABLED", defaultJobsEnabled); err != nil {
return err
}
if config.JobsPollInterval, err = durationOrDefault(lookup, "JOBS_POLL_INTERVAL", defaultJobsPollInterval); err != nil {
return err
}
if config.JobsBatchSize, err = intOrDefault(lookup, "JOBS_BATCH_SIZE", defaultJobsBatchSize); err != nil {
return err
}
if config.JobsLeaseDuration, err = durationOrDefault(lookup, "JOBS_LEASE_DURATION", defaultJobsLeaseDuration); err != nil {
return err
}
if config.JobsMaxAttempts, err = intOrDefault(lookup, "JOBS_MAX_ATTEMPTS", defaultJobsMaxAttempts); err != nil {
return err
}
if config.JobsBackoffInitial, err = durationOrDefault(lookup, "JOBS_BACKOFF_INITIAL", defaultJobsBackoffInitial); err != nil {
return err
}
if config.JobsBackoffMax, err = durationOrDefault(lookup, "JOBS_BACKOFF_MAX", defaultJobsBackoffMax); err != nil {
return err
}
config.JobsShutdownGrace, err = durationOrDefault(lookup, "JOBS_SHUTDOWN_GRACE", defaultJobsShutdownGrace)
return err
}
// ValidateJobs checks the scheduled job settings even when the worker is
// disabled, so enabling it later cannot surface an old mistake.
func (config Config) ValidateJobs() error {
if config.JobsPollInterval < minJobsPollInterval || config.JobsPollInterval > maxJobsPollInterval {
return fmt.Errorf("Config.ValidateJobs - JOBS_POLL_INTERVAL must be between %s and %s", minJobsPollInterval, maxJobsPollInterval)
}
if config.JobsBatchSize < 1 || config.JobsBatchSize > maxJobsBatchSize {
return fmt.Errorf("Config.ValidateJobs - JOBS_BATCH_SIZE must be between 1 and %d", maxJobsBatchSize)
}
if config.JobsLeaseDuration < minJobsLeaseDuration || config.JobsLeaseDuration > maxJobsLeaseDuration {
return fmt.Errorf("Config.ValidateJobs - JOBS_LEASE_DURATION must be between %s and %s", minJobsLeaseDuration, maxJobsLeaseDuration)
}
if config.JobsMaxAttempts < 1 || config.JobsMaxAttempts > maxJobsMaxAttempts {
return fmt.Errorf("Config.ValidateJobs - JOBS_MAX_ATTEMPTS must be between 1 and %d", maxJobsMaxAttempts)
}
if config.JobsBackoffInitial <= 0 {
return errors.New("Config.ValidateJobs - JOBS_BACKOFF_INITIAL must be positive")
}
if config.JobsBackoffMax < config.JobsBackoffInitial || config.JobsBackoffMax > maxJobsBackoff {
return fmt.Errorf("Config.ValidateJobs - JOBS_BACKOFF_MAX must be between JOBS_BACKOFF_INITIAL and %s", maxJobsBackoff)
}
// The grace runs while the HTTP servers drain, so it must fit in the
// shutdown budget of the process.
if config.JobsShutdownGrace <= 0 || config.JobsShutdownGrace > config.ShutdownTimeout {
return errors.New("Config.ValidateJobs - JOBS_SHUTDOWN_GRACE must be positive and at most SHUTDOWN_TIMEOUT")
}
return nil
}