diff --git a/config.go b/config.go index aeffbd7..921cb24 100644 --- a/config.go +++ b/config.go @@ -1,13 +1,10 @@ // Copyright 2021 Harran Ali . All rights reserved. // Copyright (c) 2024 Zeni Kim -// Copyright (c) 2026 Jose Cely // Use of this source code is governed by MIT-style // license that can be found in the LICENSE file. package core -import "time" - type EnvFileConfig struct { UseDotEnvFile bool } @@ -31,12 +28,6 @@ type QueueConfig struct { Queues map[string]int } -type SchedulerConfig struct { - EnableScheduler bool - SchedulerInterval time.Duration - SchedulerRateLimit int -} - type CacheConfig struct { EnableCache bool } diff --git a/go.mod b/go.mod index 6f47488..793079f 100644 --- a/go.mod +++ b/go.mod @@ -16,9 +16,8 @@ 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.21.0 - golang.org/x/crypto v0.53.0 - golang.org/x/text v0.38.0 + github.com/redis/go-redis/v9 v9.19.0 + golang.org/x/crypto v0.50.0 gorm.io/driver/mysql v1.6.0 gorm.io/driver/postgres v1.6.0 gorm.io/driver/sqlite v1.6.0 @@ -30,17 +29,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.3.0 // indirect + github.com/go-chi/chi/v5 v5.2.5 // 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.10.0 // indirect + github.com/jackc/pgx/v5 v5.9.2 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/mailgun/errors v0.6.0 // indirect + github.com/mailgun/errors v0.5.0 // indirect github.com/mailgun/mailgun-go/v4 v4.23.0 // indirect - github.com/mattn/go-sqlite3 v1.14.47 // indirect + github.com/mattn/go-sqlite3 v1.14.44 // 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 @@ -49,10 +48,11 @@ 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.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/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/time v0.15.0 // indirect google.golang.org/protobuf v1.36.11 // indirect ) diff --git a/go.sum b/go.sum index 6155305..441135c 100644 --- a/go.sum +++ b/go.sum @@ -27,8 +27,6 @@ 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= @@ -54,8 +52,6 @@ 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= @@ -80,15 +76,11 @@ 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= @@ -102,8 +94,6 @@ 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= @@ -132,32 +122,20 @@ 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= diff --git a/scheduler.go b/scheduler.go deleted file mode 100644 index dd81035..0000000 --- a/scheduler.go +++ /dev/null @@ -1,157 +0,0 @@ -// Copyright (c) 2026 Jose Cely -// Use of this source code is governed by MIT-style -// license that can be found in the LICENSE file. - -package core - -import ( - "context" - "fmt" - "log" - "time" - - "git.smarteching.com/goffee/core/logger" - "git.smarteching.com/goffee/core/scheduler" -) - -// Default values for SchedulerConfig -var DefaultSchedulerInterval = 10 * time.Second -var DefaultSchedulerRateLimit = 10 - -// globalSchedulerMux holds the active Schedulermux instance so controllers -// can access it without requiring a direct import chain. -var globalSchedulerMux *Schedulermux - -// Schedulermux wraps the scheduler Registry and manages the lifecycle -// of a simple database-backed task scheduler. -type Schedulermux struct { - registry *scheduler.Registry - store *scheduler.Store - semaphore *scheduler.PersistedSemaphore - sched *scheduler.Scheduler -} - -// 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() - globalSchedulerMux = s -} - -// SetStore sets the store (backed by *gorm.DB) on the scheduler. -// This also runs AutoMigrate for the queue_items and processed_items tables. -func (s *Schedulermux) SetStore(st *scheduler.Store) { - s.store = st -} - -// AddWork registers a task handler function for a given task type. -func (s *Schedulermux) AddWork(taskType string, handler scheduler.HandlerFunc) { - s.registry.Register(taskType, handler) -} - -// GetStore returns the scheduler store, or nil if not initialized. -func (s *Schedulermux) GetStore() *scheduler.Store { - return s.store -} - -// RunScheduler starts the scheduler loop with the given configuration. -// 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") - } - - interval := config.SchedulerInterval - if interval <= 0 { - interval = DefaultSchedulerInterval - } - - rateLimit := config.SchedulerRateLimit - if rateLimit < 0 { - 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, - RateLimit: rateLimit, - }, - s.semaphore, - s.store, - s.registry, - ) - - s.sched.Start(ctx) -} - -// StopScheduler gracefully stops the scheduler loop. -func (s *Schedulermux) StopScheduler() { - if s.sched != nil { - s.sched.Stop() - } -} - -// SemaphoreSetGreen allows task execution to proceed. -func (s *Schedulermux) SemaphoreSetGreen() { - if s.semaphore != nil { - s.semaphore.SetGreen() - } -} - -// SemaphoreSetRed blocks task execution. -func (s *Schedulermux) SemaphoreSetRed() { - if s.semaphore != nil { - s.semaphore.SetRed() - } -} - -// ResolveSchedulerStore returns the store from the global scheduler mux instance. -// Returns nil if the scheduler was never initialized. -func ResolveSchedulerStore() *scheduler.Store { - if globalSchedulerMux == nil { - return nil - } - return globalSchedulerMux.GetStore() -} - -// SchedulerSemaphoreSetGreen enables task execution by setting the semaphore to green. -func SchedulerSemaphoreSetGreen() { - if globalSchedulerMux != nil { - globalSchedulerMux.SemaphoreSetGreen() - } -} - -// SchedulerSemaphoreSetRed blocks task execution by setting the semaphore to red. -func SchedulerSemaphoreSetRed() { - if globalSchedulerMux != nil { - globalSchedulerMux.SemaphoreSetRed() - } -} - -// SchedulerSemaphoreIsGreen returns whether the scheduler semaphore is currently green. -func SchedulerSemaphoreIsGreen() bool { - if globalSchedulerMux == nil || globalSchedulerMux.semaphore == nil { - return false - } - return globalSchedulerMux.semaphore.IsGreen() -} diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go deleted file mode 100644 index c314009..0000000 --- a/scheduler/scheduler.go +++ /dev/null @@ -1,689 +0,0 @@ -// Copyright (c) 2026 Jose Cely -// Use of this source code is governed by MIT-style -// license that can be found in the LICENSE file. - -package scheduler - -import ( - "context" - "fmt" - "sync" - "time" - - "git.smarteching.com/goffee/core/logger" - "gorm.io/gorm" - "gorm.io/gorm/clause" -) - -// ─── Status types ──────────────────────────────────────────────────────────── - -// QueueStatus represents the lifecycle state of a queued item. -type QueueStatus string - -const ( - StatusPending QueueStatus = "pending" - StatusProcessing QueueStatus = "processing" // claimed by a scheduler tick -) - -// ProcessedStatus is the final outcome recorded in processed_items. -type ProcessedStatus string - -const ( - ProcessedOK ProcessedStatus = "ok" - ProcessedError ProcessedStatus = "error" -) - -// ─── Models ────────────────────────────────────────────────────────────────── - -// 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, 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 -} - -// TableName overrides the GORM default. -func (QueueItem) TableName() string { return "queue_items" } - -// ProcessedItem is an immutable record of a completed execution. -// Rows are never updated — only inserted. -type ProcessedItem struct { - ID uint `gorm:"primaryKey;autoIncrement"` - QueueItemID uint `gorm:"not null;index"` // FK → queue_items.id (soft ref) - TaskType string `gorm:"not null;index"` - Payload string `gorm:"type:text"` - Status ProcessedStatus `gorm:"not null;index"` - ErrorMsg string `gorm:"type:text"` // empty when Status == "ok" - StartedAt time.Time `gorm:"not null"` - FinishedAt time.Time `gorm:"not null"` - DurationMs int64 `gorm:"not null"` // FinishedAt - StartedAt in ms - CreatedAt time.Time -} - -// 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. -// Return a non-nil error to mark the item as processed with error status. -type HandlerFunc func(ctx context.Context, item *QueueItem) error - -// Registry maps task types to their handlers. -// Register all handlers before calling Scheduler.Start. -type Registry struct { - handlers map[string]HandlerFunc -} - -// NewRegistry creates an empty handler registry. -func NewRegistry() *Registry { - return &Registry{handlers: make(map[string]HandlerFunc)} -} - -// Register associates a task type with a handler. -// Panics on duplicate registration to catch misconfiguration at startup. -func (r *Registry) Register(taskType string, h HandlerFunc) { - if _, exists := r.handlers[taskType]; exists { - panic(fmt.Sprintf("scheduler: handler already registered for task type %q", taskType)) - } - r.handlers[taskType] = h -} - -// get returns the handler for a task type, or a fallback error handler. -func (r *Registry) get(taskType string) HandlerFunc { - if h, ok := r.handlers[taskType]; ok { - return h - } - return func(_ context.Context, item *QueueItem) error { - return fmt.Errorf("no handler registered for task type %q", item.TaskType) - } -} - -// ─── Store ─────────────────────────────────────────────────────────────────── - -// Store handles all database operations for the scheduler. -type Store struct { - db *gorm.DB -} - -// NewStore creates a Store. -// Tables (queue_items, processed_items) -func NewStore(db *gorm.DB) (*Store, error) { - if db == nil { - return nil, fmt.Errorf("scheduler: db is nil") - } - 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. -// 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) - } - return item, nil -} - -// ClaimBatch atomically claims up to `limit` pending tasks and marks them -// as "processing" so that concurrent scheduler instances never double-execute. -func (s *Store) ClaimBatch(ctx context.Context, limit int) ([]*QueueItem, error) { - var items []*QueueItem - - err := s.db.WithContext(ctx).Transaction(func(tx *gorm.DB) error { - query := tx. - Clauses(clause.Locking{Strength: "UPDATE", Options: "SKIP LOCKED"}). - Where("status = ?", StatusPending). - Order("priority DESC, id ASC"). - Limit(limit). - Find(&items) - - if query.Error != nil { - return query.Error - } - if len(items) == 0 { - return nil - } - - ids := make([]uint, len(items)) - for i, it := range items { - ids[i] = it.ID - } - - return tx.Model(&QueueItem{}). - Where("id IN ?", ids). - Update("status", StatusProcessing). - Error - }) - - if err != nil { - return nil, fmt.Errorf("claim batch: %w", err) - } - return items, nil -} - -// 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() - - 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: finishedAt.Sub(startedAt).Milliseconds(), - } - - 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) - } - - // 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 - err := s.db.WithContext(ctx). - Model(&QueueItem{}). - Where("status IN ?", []QueueStatus{StatusPending, StatusProcessing}). - Count(&count).Error - return count, err -} - -// RecentProcessed returns the last `n` processed items ordered by newest first. -func (s *Store) RecentProcessed(ctx context.Context, n int) ([]*ProcessedItem, error) { - var items []*ProcessedItem - err := s.db.WithContext(ctx). - Order("id DESC"). - Limit(n). - Find(&items).Error - return items, err -} - -// RecoverStuck resets any items stuck in "processing" state (e.g. after a -// crash) back to "pending" so they can be re-claimed on the next tick. -// Call this once at startup. -func (s *Store) RecoverStuck(ctx context.Context) (int64, error) { - result := s.db.WithContext(ctx). - Model(&QueueItem{}). - Where("status = ?", StatusProcessing). - Update("status", StatusPending) - return result.RowsAffected, result.Error -} - -// ─── Semaphore ─────────────────────────────────────────────────────────────── - -// Semaphore is the interface for the scheduler kill switch. -// Green (true) means tasks can run; Red (false) means tasks are blocked. -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 := &inMemorySemaphore{green: make(chan bool, 1)} - s.green <- startGreen - return s -} - -func (s *inMemorySemaphore) SetGreen() { - <-s.green - s.green <- true -} - -func (s *inMemorySemaphore) SetRed() { - <-s.green - s.green <- false -} - -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 - store *Store - registry *Registry - - stop chan struct{} - done chan struct{} -} - -// SchedulerConfig holds constructor options. -type SchedulerConfig struct { - Interval time.Duration - RateLimit int // 0 = unlimited -} - -// NewScheduler creates a Scheduler. -func NewScheduler(cfg SchedulerConfig, sem Semaphore, st *Store, reg *Registry) *Scheduler { - return &Scheduler{ - interval: cfg.Interval, - rateLimit: cfg.RateLimit, - semaphore: sem, - store: st, - registry: reg, - stop: make(chan struct{}), - done: make(chan struct{}), - } -} - -// Start launches the scheduler goroutine. -func (s *Scheduler) Start(ctx context.Context) { - go s.run(ctx) -} - -// Stop signals the scheduler and waits for it to exit cleanly. -func (s *Scheduler) Stop() { - close(s.stop) - <-s.done -} - -// run is the main event loop. -func (s *Scheduler) run(ctx context.Context) { - defer close(s.done) - - ticker := time.NewTicker(s.interval) - defer ticker.Stop() - - for { - select { - case <-ctx.Done(): - logger.ResolveLogger().Info("[scheduler] context cancelled, shutting down") - return - case <-s.stop: - logger.ResolveLogger().Info("[scheduler] stop requested, shutting down") - return - case t := <-ticker.C: - s.tick(ctx, t) - } - } -} - -// tick runs on every ticker event. -func (s *Scheduler) tick(ctx context.Context, t time.Time) { - ts := t.Format(time.TimeOnly) - - if !s.semaphore.IsGreen() { - logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] tick %s — semaphore RED, skipping", ts)) - return - } - - limit := s.rateLimit - if limit == 0 { - limit = 1000 - } - - items, err := s.store.ClaimBatch(ctx, limit) - if err != nil { - logger.ResolveLogger().Error(fmt.Sprintf("[scheduler] tick %s — claim error: %v", ts, err)) - return - } - if len(items) == 0 { - return - } - - logger.ResolveLogger().Info(fmt.Sprintf("[scheduler] tick %s — executing %d task(s)", ts, len(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 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) { - // Determine how many runs to fire this tick. - desiredRuns := item.MaxRuns - if desiredRuns < 1 { - desiredRuns = 1 // -1 (infinite): one batch per tick - } - - // 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() -}