42 lines
2.0 KiB
Go
42 lines
2.0 KiB
Go
package model
|
|
|
|
import (
|
|
"encoding/json"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
BackgroundJobStatusQueued = "queued"
|
|
BackgroundJobStatusRunning = "running"
|
|
BackgroundJobStatusRetrying = "retrying"
|
|
BackgroundJobStatusCompleted = "completed"
|
|
BackgroundJobStatusDead = "dead"
|
|
)
|
|
|
|
const DefaultBackgroundJobQueue = "default"
|
|
|
|
// BackgroundJob stores durable background work in the database.
|
|
// Reference: Chatwoot ActiveJob/Sidekiq jobs with retry, delayed scheduling,
|
|
// mutex/idempotency, and observable failure state.
|
|
type BackgroundJob struct {
|
|
ID uint `gorm:"primaryKey" json:"id"`
|
|
Queue string `gorm:"size:100;not null;default:'default';index:idx_background_jobs_ready,priority:2" json:"queue"`
|
|
JobType string `gorm:"size:150;not null;index" json:"job_type"`
|
|
Payload json.RawMessage `gorm:"type:jsonb;not null;default:'{}'" json:"payload"`
|
|
Status string `gorm:"size:50;not null;default:'queued';index:idx_background_jobs_ready,priority:1" json:"status"`
|
|
Priority int `gorm:"not null;default:0" json:"priority"`
|
|
Attempts int `gorm:"not null;default:0" json:"attempts"`
|
|
MaxAttempts int `gorm:"not null;default:3" json:"max_attempts"`
|
|
ScheduledAt time.Time `gorm:"not null;index:idx_background_jobs_ready,priority:3" json:"scheduled_at"`
|
|
LockedAt *time.Time `gorm:"index" json:"locked_at,omitempty"`
|
|
LockedBy string `gorm:"size:150" json:"locked_by,omitempty"`
|
|
IdempotencyKey string `gorm:"size:255;index" json:"idempotency_key,omitempty"`
|
|
LastError string `gorm:"type:text" json:"last_error,omitempty"`
|
|
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
|
FailedAt *time.Time `json:"failed_at,omitempty"`
|
|
CreatedAt time.Time `gorm:"autoCreateTime" json:"created_at"`
|
|
UpdatedAt time.Time `gorm:"autoUpdateTime" json:"updated_at"`
|
|
}
|
|
|
|
func (BackgroundJob) TableName() string { return "background_jobs" }
|