Merge pull request 'develop' (#35) from develop into main

Reviewed-on: #35
This commit is contained in:
Zeni Kim 2026-06-23 20:29:34 -04:00
commit dc9a11fde3
4 changed files with 387 additions and 57 deletions

22
go.mod
View file

@ -16,8 +16,9 @@ require (
github.com/hibiken/asynq v0.26.0
github.com/joho/godotenv v1.5.1
github.com/julienschmidt/httprouter v1.3.0
github.com/redis/go-redis/v9 v9.19.0
golang.org/x/crypto v0.50.0
github.com/redis/go-redis/v9 v9.21.0
golang.org/x/crypto v0.53.0
golang.org/x/text v0.38.0
gorm.io/driver/mysql v1.6.0
gorm.io/driver/postgres v1.6.0
gorm.io/driver/sqlite v1.6.0
@ -29,17 +30,17 @@ require (
github.com/SparkPost/gosparkpost v0.2.0 // indirect
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/go-chi/chi/v5 v5.2.5 // indirect
github.com/go-chi/chi/v5 v5.3.0 // indirect
github.com/go-sql-driver/mysql v1.10.0 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.9.2 // indirect
github.com/jackc/pgx/v5 v5.10.0 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailgun/errors v0.5.0 // indirect
github.com/mailgun/errors v0.6.0 // indirect
github.com/mailgun/mailgun-go/v4 v4.23.0 // indirect
github.com/mattn/go-sqlite3 v1.14.44 // indirect
github.com/mattn/go-sqlite3 v1.14.47 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pkg/errors v0.9.1 // indirect
@ -48,11 +49,10 @@ require (
github.com/sendgrid/sendgrid-go v3.16.1+incompatible // indirect
github.com/spf13/cast v1.10.0 // indirect
go.uber.org/atomic v1.11.0 // indirect
golang.org/x/image v0.39.0 // indirect
golang.org/x/net v0.53.0 // indirect
golang.org/x/sync v0.20.0 // indirect
golang.org/x/sys v0.43.0 // indirect
golang.org/x/text v0.36.0 // indirect
golang.org/x/image v0.43.0 // indirect
golang.org/x/net v0.56.0 // indirect
golang.org/x/sync v0.21.0 // indirect
golang.org/x/sys v0.46.0 // indirect
golang.org/x/time v0.15.0 // indirect
google.golang.org/protobuf v1.36.11 // indirect
)

22
go.sum
View file

@ -27,6 +27,8 @@ github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHk
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/go-chi/chi/v5 v5.2.5 h1:Eg4myHZBjyvJmAFjFvWgrqDTXFyOzjj7YIm3L3mu6Ug=
github.com/go-chi/chi/v5 v5.2.5/go.mod h1:X7Gx4mteadT3eDOMTsXzmI4/rwUpOwBHLpAfupzFJP0=
github.com/go-chi/chi/v5 v5.3.0 h1:halUjDxhshgXHMrao5bB8eNBXo/rnzwr8m5m36glehM=
github.com/go-chi/chi/v5 v5.3.0/go.mod h1:R+tYY2hNuVUUjxoPtqUdgBqevM9s9njzkTLutVsOCto=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible h1:msy24VGS42fKO9K1vLz82/GeYW1cILu7Nuuj1N3BBkE=
github.com/go-ozzo/ozzo-validation v3.6.0+incompatible/go.mod h1:gsEKFIVnabGBt6mXmxK0MoFy+cZoTJY6mu5Ll3LVLBU=
github.com/go-sql-driver/mysql v1.10.0 h1:Q+1LV8DkHJvSYAdR83XzuhDaTykuDx0l6fkXxoWCWfw=
@ -52,6 +54,8 @@ github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7Ulw
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
github.com/jackc/pgx/v5 v5.9.2 h1:3ZhOzMWnR4yJ+RW1XImIPsD1aNSz4T4fyP7zlQb56hw=
github.com/jackc/pgx/v5 v5.9.2/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/pgx/v5 v5.10.0 h1:VhSvgU2jSli8o3AqIEOTJr7rZwAEUVo4E4XhR94Zfr0=
github.com/jackc/pgx/v5 v5.10.0/go.mod h1:mal1tBGAFfLHvZzaYh77YS/eC6IX9OWbRV1QIIM0Jn4=
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
github.com/jaytaylor/html2text v0.0.0-20190408195923-01ec452cbe43/go.mod h1:CVKlgaMiht+LXvHG173ujK6JUhZXKb2u/BQtjPDIvyk=
@ -76,11 +80,15 @@ github.com/kylelemons/godebug v1.1.0 h1:RPNrshWIDI6G2gRW9EHilWtl7Z6Sb1BR0xunSBf0
github.com/kylelemons/godebug v1.1.0/go.mod h1:9/0rRGxNHcop5bhtWyNeEfOS8JIWk580+fNqagV/RAw=
github.com/mailgun/errors v0.5.0 h1:pLQo8uhAdORsjN69mGixSr0pGs46z/BW/FQXd8HG1VM=
github.com/mailgun/errors v0.5.0/go.mod h1:+2nrgY77E0vDkG4ErehpcpbSkMLkseJzKbrva89WeSs=
github.com/mailgun/errors v0.6.0 h1:IWmzIGwXCSN/Q60JT/lXvam3xRAgTUJSX88KwKJ7hss=
github.com/mailgun/errors v0.6.0/go.mod h1:+2nrgY77E0vDkG4ErehpcpbSkMLkseJzKbrva89WeSs=
github.com/mailgun/mailgun-go/v4 v4.23.0 h1:jPEMJzzin2s7lvehcfv/0UkyBu18GvcURPr2+xtZRbk=
github.com/mailgun/mailgun-go/v4 v4.23.0/go.mod h1:imTtizoFtpfZqPqGP8vltVBB6q9yWcv6llBhfFeElZU=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
github.com/mattn/go-sqlite3 v1.14.44 h1:3VSe+xafpbzsLbdr2AWlAZk9yRHiBhTBakioXaCKTF8=
github.com/mattn/go-sqlite3 v1.14.44/go.mod h1:pjEuOr8IwzLJP2MfGeTb0A35jauH+C2kbHKBr7yXKVQ=
github.com/mattn/go-sqlite3 v1.14.47 h1:jOBI62gS7nKeZv+as1oGEy0+1qISgXwH/QBlR6KbfIo=
github.com/mattn/go-sqlite3 v1.14.47/go.mod h1:6JTjA44L93a0QCyJef5YvlPoKXntQPjzWv5gtm9sB6w=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
@ -94,6 +102,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/redis/go-redis/v9 v9.19.0 h1:XPVaaPSnG6RhYf7p+rmSa9zZfeVAnWsH5h3lxthOm/k=
github.com/redis/go-redis/v9 v9.19.0/go.mod h1:v/M13XI1PVCDcm01VtPFOADfZtHf8YW3baQf57KlIkA=
github.com/redis/go-redis/v9 v9.21.0 h1:FPBE4hhbAke+TLmcY3WkpbDffJEomdqPn3HYiqAtL9E=
github.com/redis/go-redis/v9 v9.21.0/go.mod h1:v/M13XI1PVCDcm01VtPFOADfZtHf8YW3baQf57KlIkA=
github.com/robfig/cron/v3 v3.0.1 h1:WdRxkvbJztn8LMz/QEvLN5sBU+xKpSqwwUO1Pjr4qDs=
github.com/robfig/cron/v3 v3.0.1/go.mod h1:eQICP3HwyT7UooqI/z+Ov+PtYAWygg1TEWWzGIFLtro=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
@ -122,20 +132,32 @@ go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.50.0 h1:zO47/JPrL6vsNkINmLoo/PH1gcxpls50DNogFvB5ZGI=
golang.org/x/crypto v0.50.0/go.mod h1:3muZ7vA7PBCE6xgPX7nkzzjiUq87kRItoJQM1Yo8S+Q=
golang.org/x/crypto v0.53.0 h1:QZ4Muo8THX6CizN2vPPd5fBGHyogrdK9fG4wLPFUsto=
golang.org/x/crypto v0.53.0/go.mod h1:DNLU434OwVakk9PzuwV8w62mAJpRJL3vsgcfp4Qnsio=
golang.org/x/image v0.39.0 h1:skVYidAEVKgn8lZ602XO75asgXBgLj9G/FE3RbuPFww=
golang.org/x/image v0.39.0/go.mod h1:sIbmppfU+xFLPIG0FoVUTvyBMmgng1/XAMhQ2ft0hpA=
golang.org/x/image v0.43.0 h1:FLxcP4ec2350nTfOC8ysKtqYSIFbk/QGjw1ZHNP4tsY=
golang.org/x/image v0.43.0/go.mod h1:rrpelvGFt+kLPAjPM4HeWPgrl0FtafueU//e5N0qk/Q=
golang.org/x/net v0.0.0-20190724013045-ca1201d0de80/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.53.0 h1:d+qAbo5L0orcWAr0a9JweQpjXF19LMXJE8Ey7hwOdUA=
golang.org/x/net v0.53.0/go.mod h1:JvMuJH7rrdiCfbeHoo3fCQU24Lf5JJwT9W3sJFulfgs=
golang.org/x/net v0.56.0 h1:Rw8j/hFzGvJUZwNBXnAtf5sVDVt+65SK2C7IxCxZt5o=
golang.org/x/net v0.56.0/go.mod h1:D3Ku6r+V6JROoZK144D2XfMHFcMq/0zSfLelVTCFKec=
golang.org/x/sync v0.20.0 h1:e0PTpb7pjO8GAtTs2dQ6jYa5BWYlMuX047Dco/pItO4=
golang.org/x/sync v0.20.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sync v0.21.0 h1:HLII4xRRTtCRkxYp4HNFF0Js/Og6q2i++KXbg0gHCwM=
golang.org/x/sync v0.21.0/go.mod h1:9xrNwdLfx4jkKbNva9FpL6vEN7evnE43NNNJQ2LF3+0=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.43.0 h1:Rlag2XtaFTxp19wS8MXlJwTvoh8ArU6ezoyFsMyCTNI=
golang.org/x/sys v0.43.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/sys v0.46.0 h1:noSf2Fq6F8DBgS+LysIkx7rIExoNHJsxOAtPp4rthXw=
golang.org/x/sys v0.46.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.36.0 h1:JfKh3XmcRPqZPKevfXVpI1wXPTqbkE5f7JA92a55Yxg=
golang.org/x/text v0.36.0/go.mod h1:NIdBknypM8iqVmPiuco0Dh6P5Jcdk8lJL0CUebqK164=
golang.org/x/text v0.38.0 h1:sXmwo9DwP3OK9EZ7PqAdaooSGozfl/3a6/xJcbzPRhE=
golang.org/x/text v0.38.0/go.mod h1:YXZt3QhHUKYT53r2lLKFIVi6Ao1jdzrTR/KQ09qyxF4=
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=

View file

@ -27,15 +27,15 @@ var globalSchedulerMux *Schedulermux
type Schedulermux struct {
registry *scheduler.Registry
store *scheduler.Store
semaphore *scheduler.Semaphore
semaphore *scheduler.PersistedSemaphore
sched *scheduler.Scheduler
}
// SchedulerInit initializes the scheduler internals (registry and semaphore).
// SchedulerInit initializes the scheduler internals (registry).
// The store must be set separately via SetStore before starting.
// The semaphore is created lazily during RunScheduler once the DB is available.
func (s *Schedulermux) SchedulerInit() {
s.registry = scheduler.NewRegistry()
s.semaphore = scheduler.NewSemaphore(true)
globalSchedulerMux = s
}
@ -56,7 +56,8 @@ func (s *Schedulermux) GetStore() *scheduler.Store {
}
// RunScheduler starts the scheduler loop with the given configuration.
// It creates the internal Scheduler instance and launches it in a goroutine.
// It auto-migrates the scheduler_meta table, recovers the persisted semaphore
// state, creates the in-memory semaphore, and launches the scheduler goroutine.
func (s *Schedulermux) RunScheduler(config SchedulerConfig) {
if s.store == nil {
log.Fatal("scheduler: store is not set — call SetStore before RunScheduler")
@ -72,6 +73,24 @@ func (s *Schedulermux) RunScheduler(config SchedulerConfig) {
rateLimit = DefaultSchedulerRateLimit
}
ctx := context.Background()
// Auto-migrate the scheduler_meta table for semaphore persistence.
_ = s.store.DB().AutoMigrate(&scheduler.SchedulerMeta{})
// Recover the persisted semaphore state (defaults to green).
wasGreen := s.store.GetSemaphore(ctx)
s.semaphore = scheduler.NewPersistedSemaphore(s.store, wasGreen)
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] semaphore recovered: %s", map[bool]string{true: "green", false: "red"}[wasGreen]))
// Recover stuck items from a previous crash before starting.
recovered, err := s.store.RecoverStuck(ctx)
if err != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] recover stuck error: %v", err))
} else if recovered > 0 {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] recovered %d stuck task(s) back to pending", recovered))
}
s.sched = scheduler.NewScheduler(
scheduler.SchedulerConfig{
Interval: interval,
@ -82,15 +101,6 @@ func (s *Schedulermux) RunScheduler(config SchedulerConfig) {
s.registry,
)
// Recover stuck items from a previous crash before starting.
ctx := context.Background()
recovered, err := s.store.RecoverStuck(ctx)
if err != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] recover stuck error: %v", err))
} else if recovered > 0 {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] recovered %d stuck task(s) back to pending", recovered))
}
s.sched.Start(ctx)
}

View file

@ -7,6 +7,7 @@ package scheduler
import (
"context"
"fmt"
"sync"
"time"
"git.smarteching.com/goffee/core/logger"
@ -37,13 +38,15 @@ const (
// QueueItem is a task waiting to be executed.
// Any external program can insert a row into this table to enqueue work.
//
// INSERT INTO queue_items (task_type, payload, priority) VALUES (?, ?, ?);
// INSERT INTO queue_items (task_type, payload, priority, max_runs, thread) VALUES (?, ?, ?, ?, ?);
type QueueItem struct {
ID uint `gorm:"primaryKey;autoIncrement"`
TaskType string `gorm:"not null;index"` // e.g. "send_email", "sync_records"
Payload string `gorm:"type:text"` // JSON or plain string, interpreted by the handler
Status QueueStatus `gorm:"not null;default:'pending';index"`
Priority int `gorm:"not null;default:0;index"` // higher = runs first within a tick
MaxRuns int `gorm:"not null;default:1"` // 1 = run once, -1 = repeat indefinitely, N = run N times
Thread int `gorm:"not null;default:0"` // 0 = sequential, 1 = concurrent per type
CreatedAt time.Time
UpdatedAt time.Time
}
@ -69,6 +72,17 @@ type ProcessedItem struct {
// TableName overrides the GORM default.
func (ProcessedItem) TableName() string { return "processed_items" }
// SchedulerMeta stores key-value metadata for the scheduler, such as the
// semaphore state. This table persists across restarts so the scheduler can
// recover its previous state.
type SchedulerMeta struct {
Key string `gorm:"primaryKey;size:64"`
Value string `gorm:"size:16;not null"`
}
// TableName overrides the GORM default.
func (SchedulerMeta) TableName() string { return "scheduler_meta" }
// ─── Handler ─────────────────────────────────────────────────────────────────
// HandlerFunc processes a single queue item.
@ -121,13 +135,26 @@ func NewStore(db *gorm.DB) (*Store, error) {
return &Store{db: db}, nil
}
// DB returns the underlying *gorm.DB used by the store.
func (s *Store) DB() *gorm.DB {
return s.db
}
// Enqueue inserts a new pending task.
func (s *Store) Enqueue(ctx context.Context, taskType, payload string, priority int) (*QueueItem, error) {
// maxRuns controls how many times the task executes: 1 = once (default), -1 = repeat indefinitely.
// thread controls concurrency: 0 = sequential per type, 1 = concurrent per type.
// Any value of maxRuns < -1 is clamped to 1.
func (s *Store) Enqueue(ctx context.Context, taskType, payload string, priority, maxRuns, thread int) (*QueueItem, error) {
if maxRuns < -1 {
maxRuns = 1
}
item := &QueueItem{
TaskType: taskType,
Payload: payload,
Status: StatusPending,
Priority: priority,
MaxRuns: maxRuns,
Thread: thread,
}
if err := s.db.WithContext(ctx).Create(item).Error; err != nil {
return nil, fmt.Errorf("enqueue %s: %w", taskType, err)
@ -172,8 +199,9 @@ func (s *Store) ClaimBatch(ctx context.Context, limit int) ([]*QueueItem, error)
return items, nil
}
// RecordResult writes a ProcessedItem and deletes the original QueueItem
// in a single transaction.
// RecordResult writes a ProcessedItem, then either deletes the QueueItem (if
// MaxRuns is exhausted) or decrements MaxRuns and resets the status to pending
// for re-execution on the next tick. A maxRuns of -1 means repeat indefinitely.
func (s *Store) RecordResult(ctx context.Context, item *QueueItem, startedAt time.Time, execErr error) error {
finishedAt := time.Now()
@ -196,16 +224,78 @@ func (s *Store) RecordResult(ctx context.Context, item *QueueItem, startedAt tim
}
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
// 1. Insert the immutable result record.
if err := tx.Create(processed).Error; err != nil {
return fmt.Errorf("insert processed_item: %w", err)
}
if err := tx.Delete(&QueueItem{}, item.ID).Error; err != nil {
return fmt.Errorf("delete queue_item %d: %w", item.ID, err)
// 2. Decide: delete the queue item or re-queue it.
// MaxRuns == 1 means "run once" — delete.
// MaxRuns == -1 means "repeat indefinitely" — always re-queue.
// MaxRuns > 1 means "run N times" — decrement and re-queue.
if item.MaxRuns == 1 {
// Exhausted — delete.
if err := tx.Delete(&QueueItem{}, item.ID).Error; err != nil {
return fmt.Errorf("delete queue_item %d: %w", item.ID, err)
}
} else {
// Decrement MaxRuns (if not -1/infinite) and set back to pending.
newMaxRuns := item.MaxRuns
if newMaxRuns > 1 {
newMaxRuns--
}
if err := tx.Model(&QueueItem{}).
Where("id = ?", item.ID).
Updates(map[string]interface{}{
"status": StatusPending,
"max_runs": newMaxRuns,
}).Error; err != nil {
return fmt.Errorf("re-queue queue_item %d: %w", item.ID, err)
}
}
return nil
})
}
// SemaphoreKey is the key used in scheduler_meta for the semaphore state.
const SemaphoreKey = "semaphore"
// SemaphoreValueGreen is the stored value for a green semaphore.
const SemaphoreValueGreen = "green"
// SemaphoreValueRed is the stored value for a red semaphore.
const SemaphoreValueRed = "red"
// SetSemaphore persists the semaphore state to the database.
func (s *Store) SetSemaphore(ctx context.Context, green bool) error {
value := SemaphoreValueRed
if green {
value = SemaphoreValueGreen
}
return s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
var count int64
tx.Model(&SchedulerMeta{}).Where("key = ?", SemaphoreKey).Count(&count)
if count == 0 {
return tx.Create(&SchedulerMeta{Key: SemaphoreKey, Value: value}).Error
}
return tx.Model(&SchedulerMeta{}).
Where("key = ?", SemaphoreKey).
Update("value", value).Error
})
}
// GetSemaphore reads the persisted semaphore state from the database.
// Returns true if green, false if red or not found.
func (s *Store) GetSemaphore(ctx context.Context) bool {
var meta SchedulerMeta
err := s.db.WithContext(ctx).Where("key = ?", SemaphoreKey).First(&meta).Error
if err != nil || meta.Value != SemaphoreValueGreen {
return false
}
return true
}
// PendingCount returns the number of tasks currently in pending or processing state.
func (s *Store) PendingCount(ctx context.Context) (int64, error) {
var count int64
@ -239,45 +329,83 @@ func (s *Store) RecoverStuck(ctx context.Context) (int64, error) {
// ─── Semaphore ───────────────────────────────────────────────────────────────
// Semaphore controls whether the scheduler is allowed to execute tasks.
// Semaphore is the interface for the scheduler kill switch.
// Green (true) means tasks can run; Red (false) means tasks are blocked.
type Semaphore struct {
type Semaphore interface {
SetGreen()
SetRed()
IsGreen() bool
}
// inMemorySemaphore is the default channel-based implementation.
type inMemorySemaphore struct {
green chan bool
}
// NewSemaphore creates a Semaphore. Pass true to start green, false to start red.
func NewSemaphore(startGreen bool) *Semaphore {
s := &Semaphore{green: make(chan bool, 1)}
func NewSemaphore(startGreen bool) Semaphore {
s := &inMemorySemaphore{green: make(chan bool, 1)}
s.green <- startGreen
return s
}
// SetGreen allows task execution to proceed.
func (s *Semaphore) SetGreen() {
func (s *inMemorySemaphore) SetGreen() {
<-s.green
s.green <- true
}
// SetRed blocks task execution.
func (s *Semaphore) SetRed() {
func (s *inMemorySemaphore) SetRed() {
<-s.green
s.green <- false
}
// IsGreen reports whether execution is currently allowed.
func (s *Semaphore) IsGreen() bool {
func (s *inMemorySemaphore) IsGreen() bool {
val := <-s.green
s.green <- val
return val
}
// PersistedSemaphore implements Semaphore and persists every state change to
// the database via the Store, so the state survives application restarts.
type PersistedSemaphore struct {
inner Semaphore
store *Store
ctx context.Context
}
// NewPersistedSemaphore creates a PersistedSemaphore with an initial state.
func NewPersistedSemaphore(store *Store, startGreen bool) *PersistedSemaphore {
return &PersistedSemaphore{
inner: NewSemaphore(startGreen),
store: store,
ctx: context.Background(),
}
}
// SetGreen allows task execution and persists the state.
func (ps *PersistedSemaphore) SetGreen() {
ps.inner.SetGreen()
_ = ps.store.SetSemaphore(ps.ctx, true)
}
// SetRed blocks task execution and persists the state.
func (ps *PersistedSemaphore) SetRed() {
ps.inner.SetRed()
_ = ps.store.SetSemaphore(ps.ctx, false)
}
// IsGreen reports whether execution is currently allowed.
func (ps *PersistedSemaphore) IsGreen() bool {
return ps.inner.IsGreen()
}
// ─── Scheduler ───────────────────────────────────────────────────────────────
// Scheduler polls the database every interval and executes pending tasks.
type Scheduler struct {
interval time.Duration
rateLimit int // max tasks per tick; 0 = unlimited
semaphore *Semaphore
semaphore Semaphore
store *Store
registry *Registry
@ -292,7 +420,7 @@ type SchedulerConfig struct {
}
// NewScheduler creates a Scheduler.
func NewScheduler(cfg SchedulerConfig, sem *Semaphore, st *Store, reg *Registry) *Scheduler {
func NewScheduler(cfg SchedulerConfig, sem Semaphore, st *Store, reg *Registry) *Scheduler {
return &Scheduler{
interval: cfg.Interval,
rateLimit: cfg.RateLimit,
@ -361,31 +489,201 @@ func (s *Scheduler) tick(ctx context.Context, t time.Time) {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] tick %s — executing %d task(s)", ts, len(items)))
for i, item := range items {
// Split items into sequential vs concurrent groups.
var sequential []*QueueItem
concurrentGroups := make(map[string][]*QueueItem) // taskType -> items
for _, item := range items {
if item.Thread != 0 {
concurrentGroups[item.TaskType] = append(concurrentGroups[item.TaskType], item)
} else {
sequential = append(sequential, item)
}
}
// Execute sequential items one at a time.
for i, item := range sequential {
s.execute(ctx, item, i+1, len(items))
}
// Execute concurrent groups in parallel per type.
for _, group := range concurrentGroups {
s.executeGroup(ctx, group, len(items))
}
if remaining, err := s.store.PendingCount(ctx); err == nil {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] tick %s — done (%d task(s) still queued)", ts, remaining))
}
}
// execute runs one task and records the result regardless of outcome.
// execute runs a task's handler one or more times within the same tick.
// When Thread=0 (sequential), runs execute one after another in a loop.
// When Thread=1 (concurrent), runs fire simultaneously in goroutines.
// The number of runs per tick is capped by the rate limit, and the item
// is re-queued with remaining runs or deleted when exhausted.
func (s *Scheduler) execute(ctx context.Context, item *QueueItem, pos, total int) {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] starting task id=%d type=%s", pos, total, item.ID, item.TaskType))
startedAt := time.Now()
handler := s.registry.get(item.TaskType)
execErr := handler(ctx, item)
if execErr != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] task id=%d FAILED: %v", pos, total, item.ID, execErr))
} else {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] task id=%d OK (%.0fms)",
pos, total, item.ID, float64(time.Since(startedAt).Milliseconds())))
// Determine how many runs to fire this tick.
desiredRuns := item.MaxRuns
if desiredRuns < 1 {
desiredRuns = 1 // -1 (infinite): one batch per tick
}
if err := s.store.RecordResult(ctx, item, startedAt, execErr); err != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] failed to record result for id=%d: %v", pos, total, item.ID, err))
// Cap by the rate limit so one item cannot exhaust the tick budget.
budget := s.rateLimit
if budget <= 0 {
budget = 1000
}
if desiredRuns > budget {
desiredRuns = budget
}
remainingRuns := 0
if item.MaxRuns == -1 {
remainingRuns = -1
} else {
remainingRuns = item.MaxRuns - desiredRuns
if remainingRuns < 0 {
remainingRuns = 0
}
}
isConcurrent := item.Thread != 0
if isConcurrent {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] task id=%d type=%s firing %d/%d concurrent run(s)", pos, total, item.ID, item.TaskType, desiredRuns, item.MaxRuns))
} else {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] task id=%d type=%s firing %d/%d sequential run(s)", pos, total, item.ID, item.TaskType, desiredRuns, item.MaxRuns))
}
if isConcurrent {
// ── Concurrent: all runs in parallel goroutines ──
var wg sync.WaitGroup
wg.Add(desiredRuns)
for i := 0; i < desiredRuns; i++ {
go func(runIdx int) {
defer wg.Done()
startedAt := time.Now()
handler := s.registry.get(item.TaskType)
execErr := handler(ctx, item)
finishedAt := time.Now()
elapsed := finishedAt.Sub(startedAt).Milliseconds()
status := ProcessedOK
errMsg := ""
if execErr != nil {
status = ProcessedError
errMsg = execErr.Error()
}
processed := &ProcessedItem{
QueueItemID: item.ID,
TaskType: item.TaskType,
Payload: item.Payload,
Status: status,
ErrorMsg: errMsg,
StartedAt: startedAt,
FinishedAt: finishedAt,
DurationMs: elapsed,
}
if err := s.store.db.WithContext(ctx).Create(processed).Error; err != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] failed to record processed_item for id=%d run=%d: %v", pos, total, item.ID, runIdx+1, err))
}
if execErr != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] task id=%d run=%d FAILED after %dms: %v", pos, total, item.ID, runIdx+1, elapsed, execErr))
} else {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] task id=%d run=%d OK (%dms)", pos, total, item.ID, runIdx+1, elapsed))
}
}(i)
}
wg.Wait()
} else {
// ── Sequential: all runs one after another ──
for i := 0; i < desiredRuns; i++ {
startedAt := time.Now()
handler := s.registry.get(item.TaskType)
execErr := handler(ctx, item)
finishedAt := time.Now()
elapsed := finishedAt.Sub(startedAt).Milliseconds()
status := ProcessedOK
errMsg := ""
if execErr != nil {
status = ProcessedError
errMsg = execErr.Error()
}
processed := &ProcessedItem{
QueueItemID: item.ID,
TaskType: item.TaskType,
Payload: item.Payload,
Status: status,
ErrorMsg: errMsg,
StartedAt: startedAt,
FinishedAt: finishedAt,
DurationMs: elapsed,
}
if err := s.store.db.WithContext(ctx).Create(processed).Error; err != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] failed to record processed_item for id=%d run=%d: %v", pos, total, item.ID, i+1, err))
}
if execErr != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] task id=%d run=%d FAILED after %dms: %v", pos, total, item.ID, i+1, elapsed, execErr))
} else {
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] task id=%d run=%d OK (%dms)", pos, total, item.ID, i+1, elapsed))
}
}
}
// ── After all runs: decide the queue item's fate ──
if remainingRuns == -1 {
if err := s.store.db.WithContext(ctx).
Model(&QueueItem{}).
Where("id = ?", item.ID).
Updates(map[string]interface{}{
"status": StatusPending,
"max_runs": -1,
}).Error; err != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] failed to re-queue infinite item id=%d: %v", pos, total, item.ID, err))
}
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] task id=%d re-queued for next tick (infinite)", pos, total, item.ID))
} else if remainingRuns > 0 {
if err := s.store.db.WithContext(ctx).
Model(&QueueItem{}).
Where("id = ?", item.ID).
Updates(map[string]interface{}{
"status": StatusPending,
"max_runs": remainingRuns,
}).Error; err != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] failed to re-queue item id=%d with %d remaining runs: %v", pos, total, item.ID, remainingRuns, err))
}
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] task id=%d re-queued with %d run(s) remaining", pos, total, item.ID, remainingRuns))
} else {
if err := s.store.db.WithContext(ctx).Delete(&QueueItem{}, item.ID).Error; err != nil {
logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] [%d/%d] failed to delete finished item id=%d: %v", pos, total, item.ID, err))
}
logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] [%d/%d] task id=%d all runs exhausted, deleted", pos, total, item.ID))
}
}
// executeGroup runs all items in a group concurrently using goroutines.
// Each item's result is recorded independently.
func (s *Scheduler) executeGroup(ctx context.Context, items []*QueueItem, total int) {
var wg sync.WaitGroup
wg.Add(len(items))
for _, item := range items {
item := item // capture range variable
go func() {
defer wg.Done()
// Find the item's position among all items for logging context.
// Since we don't have global position here, use a local index.
s.execute(ctx, item, 0, total)
}()
}
wg.Wait()
}