Documentation
¶
Overview ¶
Package queue runs GoHead's background jobs (collection events, agent runs, webhook deliveries) behind one interface, with two drivers:
- redis: asynq on Redis, for deployments with several processes or replicas (the default);
- database: a jobs table in GoHead's own database, polled by in-process workers. No extra service: suited to a single process (gohead standalone) or a few, on small machines.
Index ¶
- Constants
- Variables
- func Backoff(n int) time.Duration
- func Counts(ctx context.Context, db *gorm.DB) (map[string]int64, error)
- func GetRetryCount(ctx context.Context) (int, bool)
- func GetTaskID(ctx context.Context) (string, bool)
- func WithTaskInfo(ctx context.Context, id string, retry int) context.Context
- type Client
- type DatabaseClient
- type DatabaseServer
- type DatabaseServerConfig
- type HandlerFunc
- type Job
- type Mux
- type Option
- type Options
- type RedisClient
- type RedisServer
- type Server
- type ServerConfig
Constants ¶
const ( StatusPending = "pending" StatusRunning = "running" StatusDone = "done" StatusFailed = "failed" )
Job statuses of the database driver.
const ( DriverRedis = "redis" DriverDatabase = "database" )
Driver names.
const ( DefaultMaxRetry = 25 DefaultTimeout = 30 * time.Minute )
Defaults shared by the drivers (asynq's).
Variables ¶
var ErrDuplicateTask = errors.New("a task with this ID already exists")
ErrDuplicateTask is returned when a task with the same TaskID is queued, running, or kept for its retention period.
var FailedRetention = 7 * 24 * time.Hour
FailedRetention is how long the database driver keeps tasks that failed for good (asynq's archive keeps them 90 days; a small machine keeps less).
var SkipRetry = errors.New("skip retry")
SkipRetry, wrapped in a handler's error, fails the task without retrying.
Functions ¶
func Backoff ¶
Backoff is the delay before retry n (1-based), asynq's default: n⁴ + 15 + rand(0..30)·(n+1) seconds.
func GetRetryCount ¶
GetRetryCount returns how many times the task was retried (0 on the first attempt).
Types ¶
type Client ¶
type Client interface {
// Enqueue queues a task and returns its ID.
Enqueue(ctx context.Context, taskType string, payload []byte, opts ...Option) (string, error)
Close() error
}
Client enqueues tasks.
type DatabaseClient ¶
type DatabaseClient struct {
// contains filtered or unexported fields
}
DatabaseClient queues tasks in the jobs table.
func NewDatabaseClient ¶
func NewDatabaseClient(db *gorm.DB) *DatabaseClient
NewDatabaseClient returns a client of the jobs table of db.
type DatabaseServer ¶
type DatabaseServer struct {
// contains filtered or unexported fields
}
DatabaseServer processes tasks of the jobs table.
func NewDatabaseServer ¶
func NewDatabaseServer(db *gorm.DB, cfg DatabaseServerConfig) *DatabaseServer
NewDatabaseServer returns a server of the jobs table of db.
type DatabaseServerConfig ¶
type DatabaseServerConfig struct {
ServerConfig
// PollInterval is how often other processes' tasks and retries are
// looked for (tasks queued by this process start at once). Default 1s.
PollInterval time.Duration
// ShutdownTimeout is how long running tasks may finish on shutdown
// before they are cancelled and queued again. Default 8s.
ShutdownTimeout time.Duration
}
DatabaseServerConfig tunes the database driver.
type HandlerFunc ¶
HandlerFunc processes a task. Returning an error retries it later (with backoff) unless it wraps SkipRetry or the retries are exhausted.
type Job ¶
type Job struct {
ID uint64 `gorm:"primaryKey"`
TaskID *string `gorm:"size:191;uniqueIndex"`
Queue string `gorm:"size:64;not null;index:idx_jobs_claim,priority:2"`
Type string `gorm:"size:128;not null"`
Payload []byte
Status string `gorm:"size:16;not null;index:idx_jobs_claim,priority:1"`
RunAt time.Time `gorm:"not null;index:idx_jobs_claim,priority:3"`
Retried int `gorm:"not null;default:0"`
MaxRetry int `gorm:"not null;default:0"`
TimeoutMS int64 `gorm:"column:timeout_ms;not null;default:0"`
RetentionMS int64 `gorm:"column:retention_ms;not null;default:0"`
LockedBy string `gorm:"size:64"`
LockedUntil *time.Time `gorm:"index"`
RetentionUntil *time.Time `gorm:"index"`
LastError string `gorm:"type:text"`
CreatedAt time.Time
UpdatedAt time.Time
}
Job is a task of the database driver (table jobs).
type Mux ¶
type Mux struct {
// contains filtered or unexported fields
}
Mux maps task types to handlers.
func (*Mux) Handle ¶
func (m *Mux) Handle(taskType string, h HandlerFunc)
Handle registers the handler of a task type.
type Options ¶
type Options struct {
Queue string
TaskID string // deduplication key; empty: none
MaxRetry int // retries after the first attempt; <0: DefaultMaxRetry
Timeout time.Duration // per attempt; 0: DefaultTimeout
Retention time.Duration // keep the finished task (and its ID) this long
}
Options of an enqueued task.
type RedisClient ¶
type RedisClient struct {
// contains filtered or unexported fields
}
RedisClient enqueues tasks on Redis with asynq.
func NewRedisClient ¶
func NewRedisClient(opt asynq.RedisConnOpt) *RedisClient
NewRedisClient returns a client of the Redis described by opt.
type RedisServer ¶
type RedisServer struct {
// contains filtered or unexported fields
}
RedisServer processes tasks from Redis with asynq.
func NewRedisServer ¶
func NewRedisServer(opt asynq.RedisConnOpt, cfg ServerConfig) *RedisServer
NewRedisServer returns a server of the Redis described by opt.
type ServerConfig ¶
type ServerConfig struct {
// Concurrency is the number of tasks processed at once.
Concurrency int
// Queues to process, with their priority: queues are looked at in a
// random order weighted by priority, so busy queues do not starve the
// others.
Queues map[string]int
// StrictPriority always empties higher priority queues first.
StrictPriority bool
}
ServerConfig configures a Server.