Files
gochat/backend/internal/model/background_job.go
T
rogee aeddedf2a3 Reorganize repo: backend/, deploy/, docs/ layout + AGENTS.md
Restructure the monorepo into clear top-level directories:
- backend/: Go module root (cmd, internal, pkg, configs, migrations,
  docs/swagger, scripts, tests, go.mod, Makefile, .air.toml)
- deploy/: Docker (Dockerfile, docker-compose*), quickstart, fluentd
- docs/: project documentation + reports/ (moved from repo root)
- AGENTS.md: new AI coding-agent guide at repo root

Update all references to the new layout:
- Dockerfile: COPY backend/go.mod, COPY backend/ (context = repo root)
- docker-compose files: context ../.., dockerfile deploy/docker/Dockerfile,
  env_file ../../.env, volume mounts ../../backend:/app
- deploy/quickstart/compose.yaml: dockerfile deploy/docker/Dockerfile
- CI: working-directory: backend for go commands, file deploy/docker/Dockerfile,
  coverage path backend/coverage.out, health_check backend/scripts/
- backend/Makefile: docker target uses -f ../deploy/docker/Dockerfile ../
- README: architecture tree, quickstart, config paths updated

Move root stray scripts (rename_models.*, run_m11_tests.sh, verify_build.sh,
gorm_bool_main.go) to backend/scripts/legacy/. All moves via git mv to
preserve history. Build, vet, SQLite tests, and docker compose config verified.
2026-07-07 14:44:12 +08:00

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" }